Skip to content
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

fix: parse lines with multiple equal signs #100

Merged
merged 2 commits into from
Apr 3, 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
42 changes: 42 additions & 0 deletions packages/envied_generator/lib/src/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,45 @@ extension EnumElementExtension on EnumElement {
/// Return the names of the values defined by this enum.
Iterable<String> 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<String> partialSplit(Pattern pattern, int count) {
assert(count >= 0);

final List<String> result = [];

if (count == 0) {
return result;
}

int offset = 0;
final Iterable<Match> 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;
}
}
3 changes: 2 additions & 1 deletion packages/envied_generator/lib/src/parser.dart
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion packages/envied_generator/test/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ invalidTestDateTime=2023-11-06X22:32:55.287Z
testDate=2023-11-06
invalidTestDate=2023
testEnum=ipsum
invalidTestEnum=foo
invalidTestEnum=foo
TEST_QUERY_VARS=https://www.my-awesome-website.com/index.php?foo=bar&baz=qux
16 changes: 16 additions & 0 deletions packages/envied_generator/test/src/generator_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}