diff --git a/packages/envied_generator/lib/src/extensions.dart b/packages/envied_generator/lib/src/extensions.dart index c5f016a..d83c623 100644 --- a/packages/envied_generator/lib/src/extensions.dart +++ b/packages/envied_generator/lib/src/extensions.dart @@ -25,3 +25,45 @@ extension EnumElementExtension on EnumElement { /// Return the names of the values defined by this enum. Iterable get valueNames => values.map((FieldElement fe) => fe.name); } + +/// Taken from https://stackoverflow.com/questions/76038472/limit-string-split-to-a-maximum-number-of-elements#answer-76039017 +extension PartialSplit on String { + /// A version of [String.split] that limits splitting to return a [List] + /// of at most [count] items. + /// + /// [count] must be non-negative. If [count] is 0, returns an empty + /// [List]. + /// + /// If splitting this [String] would result in more than [count] items, + /// the final element will contain the unsplit remainder of this [String]. + /// + /// If splitting this [String] would result in fewer than [count] items, + /// returns a [List] with only the split substrings. + List partialSplit(Pattern pattern, int count) { + assert(count >= 0); + + final List result = []; + + if (count == 0) { + return result; + } + + int offset = 0; + final Iterable matches = pattern.allMatches(this); + for (var match in matches) { + if (result.length + 1 == count) { + break; + } + + if (match.end - match.start == 0 && match.start == offset) { + continue; + } + + result.add(substring(offset, match.start)); + offset = match.end; + } + result.add(substring(offset)); + + return result; + } +} diff --git a/packages/envied_generator/lib/src/parser.dart b/packages/envied_generator/lib/src/parser.dart index 0682365..7dea1c9 100644 --- a/packages/envied_generator/lib/src/parser.dart +++ b/packages/envied_generator/lib/src/parser.dart @@ -1,4 +1,5 @@ import 'package:envied_generator/src/env_val.dart'; +import 'package:envied_generator/src/extensions.dart'; /// Creates key-value pairs from strings formatted as environment /// variable definitions. @@ -39,7 +40,7 @@ final class Parser { if (!_isValid(stripped)) return {}; /// Split the line into key and value. - final [String lhs, String rhs] = stripped.split('='); + final [String lhs, String rhs] = stripped.partialSplit('=', 2); /// Remove the 'export' keyword. final String key = swallow(lhs); diff --git a/packages/envied_generator/test/.env.example b/packages/envied_generator/test/.env.example index 5829db7..47e71b7 100644 --- a/packages/envied_generator/test/.env.example +++ b/packages/envied_generator/test/.env.example @@ -23,4 +23,5 @@ invalidTestDateTime=2023-11-06X22:32:55.287Z testDate=2023-11-06 invalidTestDate=2023 testEnum=ipsum -invalidTestEnum=foo \ No newline at end of file +invalidTestEnum=foo +TEST_QUERY_VARS=https://www.my-awesome-website.com/index.php?foo=bar&baz=qux diff --git a/packages/envied_generator/test/src/generator_tests.dart b/packages/envied_generator/test/src/generator_tests.dart index 6ee4183..0c9f5e0 100644 --- a/packages/envied_generator/test/src/generator_tests.dart +++ b/packages/envied_generator/test/src/generator_tests.dart @@ -1149,3 +1149,19 @@ abstract class Env35dInvalid { @EnviedField(obfuscate: true) static final ExampleEnum? invalidTestEnum = null; } + +@ShouldGenerate(r''' +// coverage:ignore-file +// ignore_for_file: type=lint +final class _Env36 { + static final Uri testQueryVars = + Uri.parse('https://www.my-awesome-website.com/index.php?foo=bar&baz=qux'); +} +''') +@Envied(path: 'test/.env.example') +abstract class Env36 { + @EnviedField( + varName: 'TEST_QUERY_VARS', + ) + static final Uri? testQueryVars = null; +}