Skip to content

Get variable values as int, double and bool #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lib/src/dotenv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,55 @@ class DotEnv {
return value;
}

/// Load the enviroment variable value as an [int]
///
/// If variable with [name] does not exist then [fallback] will be used.
/// However if also no [fallback] is supplied an error will occur.
///
/// Furthermore an [FormatException] will be thrown if the variable with [name]
/// exists but can not be parsed as an [int].
int getInt(String name, {int? fallback}) {
final value = maybeGet(name);
assert(value != null || fallback != null, 'A non-null fallback is required for missing entries');
return value != null ? int.parse(value) : fallback!;
}

/// Load the enviroment variable value as a [double]
///
/// If variable with [name] does not exist then [fallback] will be used.
/// However if also no [fallback] is supplied an error will occur.
///
/// Furthermore an [FormatException] will be thrown if the variable with [name]
/// exists but can not be parsed as a [double].
double getDouble(String name, {double? fallback}) {
final value = maybeGet(name);
assert(value != null || fallback != null, 'A non-null fallback is required for missing entries');
return value != null ? double.parse(value) : fallback!;
}

/// Load the enviroment variable value as a [bool]
///
/// If variable with [name] does not exist then [fallback] will be used.
/// However if also no [fallback] is supplied an error will occur.
///
/// Furthermore an [FormatException] will be thrown if the variable with [name]
/// exists but can not be parsed as a [bool].
bool getBool(String name, {bool? fallback}) {
final value = maybeGet(name);
assert(value != null || fallback != null, 'A non-null fallback is required for missing entries');
if (value != null) {
if (['true', '1'].contains(value.toLowerCase())) {
return true;
} else if (['false', '0'].contains(value.toLowerCase())) {
return false;
} else {
throw FormatException('Could not parse as a bool');
}
}

return fallback!;
}

String? maybeGet(String name, {String? fallback}) => env[name] ?? fallback;

/// Loads environment variables from the env file into a map
Expand Down
13 changes: 13 additions & 0 deletions test/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ FOOBAR=\$FOO${FOO}$BAR
ESCAPED_DOLLAR_SIGN="\$1000"
ESCAPED_QUOTE='\''

BOOL_TRUE=true
BOOL_1=1
BOOL_FALSE=false
BOOL_0=0

INT_42=42
INT_42_NEGATIVE=-42

DOUBLE_13_37=13.37
DOUBLE_13_37_NEGATIVE=-13.37
DOUBLE_1e3=1.e3
DOUBLE_POINT_3=.3

BASIC=basic

BASIC=basic1
Expand Down
36 changes: 36 additions & 0 deletions test/dotenv_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ void main() {
expect(dotenv.env['FOOBAR'], '\$FOOfoobar');
expect(dotenv.env['ESCAPED_DOLLAR_SIGN'], '\$1000');
expect(dotenv.env['ESCAPED_QUOTE'], "'");
expect(dotenv.env['BOOL_TRUE'], "true");
expect(dotenv.env['BOOL_1'], "1");
expect(dotenv.env['BOOL_FALSE'], "false");
expect(dotenv.env['BOOL_0'], "0");
expect(dotenv.env['INT_42'], "42");
expect(dotenv.env['INT_42_NEGATIVE'], "-42");
expect(dotenv.env['DOUBLE_13_37'], "13.37");
expect(dotenv.env['DOUBLE_13_37_NEGATIVE'], "-13.37");
expect(dotenv.env['DOUBLE_1e3'], "1.e3");
expect(dotenv.env['DOUBLE_POINT_3'], ".3");
expect(dotenv.env['BASIC'], 'basic');
expect(dotenv.env['AFTER_LINE'], 'after_line');
expect(dotenv.env['EMPTY'], '');
Expand Down Expand Up @@ -46,5 +56,31 @@ void main() {
expect(dotenv.maybeGet('COMMENTS'), null);
expect(dotenv.maybeGet('EQUAL_SIGNS', fallback: 'sample'), 'equals==');
});
test('int getting works', () {
expect(dotenv.getInt('INT_42'), 42);
expect(dotenv.getInt('INT_42_NEGATIVE'), -42);
expect(() => dotenv.getInt('COMMENTS'), throwsAssertionError);
expect(dotenv.getInt('COMMENTS', fallback: 42), 42);
expect(() => dotenv.getInt('FOO'), throwsFormatException);
});
test('double getting works', () {
expect(dotenv.getDouble('DOUBLE_13_37'), 13.37);
expect(dotenv.getDouble('DOUBLE_13_37_NEGATIVE'), -13.37);
expect(dotenv.getDouble('DOUBLE_1e3'), 1e3);
expect(dotenv.getDouble('DOUBLE_POINT_3'), .3);
expect(() => dotenv.getDouble('COMMENTS'), throwsAssertionError);
expect(dotenv.getDouble('COMMENTS', fallback: .3), .3);
expect(() => dotenv.getDouble('FOO'), throwsFormatException);
});
test('bool getting works', () {
expect(dotenv.getBool('BOOL_TRUE'), true);
expect(dotenv.getBool('BOOL_1'), true);
expect(dotenv.getBool('BOOL_FALSE'), false);
expect(dotenv.getBool('BOOL_0'), false);
expect(() => dotenv.getBool('COMMENTS'), throwsAssertionError);
expect(dotenv.getBool('COMMENTS', fallback: true), true);
expect(dotenv.getBool('COMMENTS', fallback: false), false);
expect(() => dotenv.getBool('FOO'), throwsFormatException);
});
});
}