From f238c65da15dc26b1cc5d9caf5b1b31caf7e6a8e Mon Sep 17 00:00:00 2001 From: James Collins Date: Mon, 14 Oct 2024 18:48:38 +1300 Subject: [PATCH] chore: format with dart format --- example/lib/main.dart | 45 ++++++++++++++++++++++--------------------- lib/src/dotenv.dart | 30 ++++++++++++++++++++--------- lib/src/errors.dart | 2 -- lib/src/parser.dart | 9 ++++++--- test/dotenv_test.dart | 16 +++++++++++---- 5 files changed, 62 insertions(+), 40 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 4535351..82597d7 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -19,31 +19,32 @@ class MyApp extends StatelessWidget { return MaterialApp( title: 'flutter_dotenv Demo', home: Scaffold( - appBar: AppBar( - title: const Text('Dotenv Demo'), - ), - body: SingleChildScrollView( - child: FutureBuilder( - future: rootBundle.loadString('assets/.env'), - initialData: '', - builder: (context, snapshot) => Container( - padding: const EdgeInsets.all(50), - child: Column( - children: [ - Text( - 'Env map: ${dotenv.env.toString()}', - ), - const Divider(thickness: 5), - const Text('Original'), - const Divider(), - Text(snapshot.data ?? ''), - Text(dotenv.get('MISSING', fallback: 'Default fallback value')), - ], - ), + appBar: AppBar( + title: const Text('Dotenv Demo'), + ), + body: SingleChildScrollView( + child: FutureBuilder( + future: rootBundle.loadString('assets/.env'), + initialData: '', + builder: (context, snapshot) => Container( + padding: const EdgeInsets.all(50), + child: Column( + children: [ + Text( + 'Env map: ${dotenv.env.toString()}', + ), + const Divider(thickness: 5), + const Text('Original'), + const Divider(), + Text(snapshot.data ?? ''), + Text(dotenv.get('MISSING', + fallback: 'Default fallback value')), + ], ), ), ), ), + ), ); } -} \ No newline at end of file +} diff --git a/lib/src/dotenv.dart b/lib/src/dotenv.dart index 8fc4140..03ee2cc 100644 --- a/lib/src/dotenv.dart +++ b/lib/src/dotenv.dart @@ -50,8 +50,9 @@ class DotEnv { String get(String name, {String? fallback}) { final value = maybeGet(name, fallback: fallback); - if(value == null){ - throw AssertionError('$name variable not found. A non-null fallback is required for missing entries'); + if (value == null) { + throw AssertionError( + '$name variable not found. A non-null fallback is required for missing entries'); } return value; } @@ -65,7 +66,8 @@ class DotEnv { /// 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'); + assert(value != null || fallback != null, + 'A non-null fallback is required for missing entries'); return value != null ? int.parse(value) : fallback!; } @@ -78,7 +80,8 @@ class DotEnv { /// 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'); + assert(value != null || fallback != null, + 'A non-null fallback is required for missing entries'); return value != null ? double.parse(value) : fallback!; } @@ -91,7 +94,8 @@ class DotEnv { /// 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'); + 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; @@ -110,7 +114,10 @@ class DotEnv { /// Loads environment variables from the env file into a map /// Merge with any entries defined in [mergeWith] Future load( - {String fileName = '.env',Parser parser = const Parser(),Map mergeWith = const {}, bool isOptional = false}) async { + {String fileName = '.env', + Parser parser = const Parser(), + Map mergeWith = const {}, + bool isOptional = false}) async { clean(); List linesFromFile; try { @@ -123,7 +130,9 @@ class DotEnv { } } - final linesFromMergeWith = mergeWith.entries.map((entry) => "${entry.key}=${entry.value}").toList(); + final linesFromMergeWith = mergeWith.entries + .map((entry) => "${entry.key}=${entry.value}") + .toList(); final allLines = linesFromMergeWith..addAll(linesFromFile); final envEntries = parser.parse(allLines); _envMap.addAll(envEntries); @@ -136,7 +145,9 @@ class DotEnv { Map mergeWith = const {}}) { clean(); final linesFromFile = fileInput.split('\n'); - final linesFromMergeWith = mergeWith.entries.map((entry) => "${entry.key}=${entry.value}").toList(); + final linesFromMergeWith = mergeWith.entries + .map((entry) => "${entry.key}=${entry.value}") + .toList(); final allLines = linesFromMergeWith..addAll(linesFromFile); final envEntries = parser.parse(allLines); _envMap.addAll(envEntries); @@ -146,7 +157,8 @@ class DotEnv { /// True if all supplied variables have nonempty value; false otherwise. /// Differs from [containsKey](dart:core) by excluding null values. /// Note [load] should be called first. - bool isEveryDefined(Iterable vars) => vars.every((k) => _envMap[k]?.isNotEmpty ?? false); + bool isEveryDefined(Iterable vars) => + vars.every((k) => _envMap[k]?.isNotEmpty ?? false); Future> _getEntriesFromFile(String filename) async { try { diff --git a/lib/src/errors.dart b/lib/src/errors.dart index 17a1355..b4d3610 100644 --- a/lib/src/errors.dart +++ b/lib/src/errors.dart @@ -3,5 +3,3 @@ class NotInitializedError extends Error {} class FileNotFoundError extends Error {} class EmptyEnvFileError extends Error {} - - diff --git a/lib/src/parser.dart b/lib/src/parser.dart index 1740065..92ee5b6 100644 --- a/lib/src/parser.dart +++ b/lib/src/parser.dart @@ -24,7 +24,8 @@ class Parser { } /// Parses a single line into a key-value pair. - Map parseOne(String line, {Map env = const {}}) { + Map parseOne(String line, + {Map env = const {}}) { var stripped = strip(line); if (!_isValid(stripped)) return {}; @@ -48,7 +49,8 @@ class Parser { } /// Substitutes $bash_vars in [val] with values from [env]. - String interpolate(String val, Map env) => val.replaceAllMapped(_bashVar, (m) { + String interpolate(String val, Map env) => + val.replaceAllMapped(_bashVar, (m) { if ((m.group(1) ?? "") == "\\") { return m.input.substring(m.start, m.end); } else { @@ -84,5 +86,6 @@ class Parser { bool _isValid(String s) => s.isNotEmpty && s.contains('='); /// [ null ] is a valid value in a Dart map, but the env var representation is empty string, not the string 'null' - bool _has(Map map, String key) => map.containsKey(key) && map[key] != null; + bool _has(Map map, String key) => + map.containsKey(key) && map[key] != null; } diff --git a/test/dotenv_test.dart b/test/dotenv_test.dart index bee2845..1867eaf 100644 --- a/test/dotenv_test.dart +++ b/test/dotenv_test.dart @@ -6,7 +6,9 @@ void main() { group('dotenv', () { setUp(() { print(Directory.current.toString()); - dotenv.testLoad(fileInput: File('test/.env').readAsStringSync()); // mergeWith: Platform.environment + dotenv.testLoad( + fileInput: File('test/.env') + .readAsStringSync()); // mergeWith: Platform.environment }); test('when .env is loaded we should be able to get the .env variables', () { expect(dotenv.env['FOO'], 'foo'); @@ -46,15 +48,21 @@ void main() { expect(dotenv.env['USERNAME'], 'therealnerdybeast@example.tld'); expect(dotenv.env['SPACED_KEY'], 'parsed'); }); - test('when getting a vairable that is not in .env, we should get the fallback we defined', () { + test( + 'when getting a vairable that is not in .env, we should get the fallback we defined', + () { expect(dotenv.get('FOO', fallback: 'bar'), 'foo'); expect(dotenv.get('COMMENTS', fallback: 'sample'), 'sample'); expect(dotenv.get('EQUAL_SIGNS', fallback: 'sample'), 'equals=='); }); - test('when getting a vairable that is not in .env, we should get an error thrown', () { + test( + 'when getting a vairable that is not in .env, we should get an error thrown', + () { expect(() => dotenv.get('COMMENTS'), throwsAssertionError); }); - test('when getting a vairable using the nullable getter, we should get null if no fallback is defined', () { + test( + 'when getting a vairable using the nullable getter, we should get null if no fallback is defined', + () { expect(dotenv.maybeGet('COMMENTS'), null); expect(dotenv.maybeGet('COMMENTS', fallback: 'sample'), 'sample'); expect(dotenv.maybeGet('EQUAL_SIGNS', fallback: 'sample'), 'equals==');