Skip to content

refactor: rename variables #118

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 1 commit into from
Oct 18, 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
63 changes: 32 additions & 31 deletions lib/src/parser.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// Creates key-value pairs from strings formatted as environment
/// variable definitions.
class Parser {
static const _singleQuot = "'";
static const _singleQuote = "'";
static final _leadingExport = RegExp(r'''^ *export ?''');
static final _comment = RegExp(r'''#[^'"]*$''');
static final _commentWithQuotes = RegExp(r'''#.*$''');
Expand All @@ -14,38 +14,40 @@ class Parser {
/// Creates a [Map](dart:core).
/// Duplicate keys are silently discarded.
Map<String, String> parse(Iterable<String> lines) {
var out = <String, String>{};
var envMap = <String, String>{};
for (var line in lines) {
var kv = parseOne(line, env: out);
if (kv.isEmpty) continue;
out.putIfAbsent(kv.keys.single, () => kv.values.single);
final parsedKeyValue = parseOne(line, envMap: envMap);
if (parsedKeyValue.isEmpty) continue;
envMap.putIfAbsent(parsedKeyValue.keys.single, () => parsedKeyValue.values.single);
}
return out;
return envMap;
}

/// Parses a single line into a key-value pair.
Map<String, String> parseOne(String line,
{Map<String, String> env = const {}}) {
var stripped = strip(line);
if (!_isValid(stripped)) return {};
{Map<String, String> envMap = const {}}) {
final lineWithoutComments = removeCommentsFromLine(line);
if (!_isStringWithEqualsChar(lineWithoutComments)) return {};

var idx = stripped.indexOf('=');
var lhs = stripped.substring(0, idx);
var k = swallow(lhs);
if (k.isEmpty) return {};
final indexOfEquals = lineWithoutComments.indexOf('=');
final envKey = trimExportKeyword(lineWithoutComments.substring(0, indexOfEquals));
if (envKey.isEmpty) return {};

var rhs = stripped.substring(idx + 1, stripped.length).trim();
var quotChar = surroundingQuote(rhs);
var v = unquote(rhs);
if (quotChar == _singleQuot) {
v = v.replaceAll("\\'", "'");
return {k: v};
final envValue = lineWithoutComments.substring(indexOfEquals + 1, lineWithoutComments.length).trim();
final quoteChar = getSurroundingQuoteCharacter(envValue);
var envValueWithoutQuotes = removeSurroundingQuotes(envValue);
// Add any escapted quotes
if (quoteChar == _singleQuote) {
envValueWithoutQuotes = envValueWithoutQuotes.replaceAll("\\'", "'");
// Return. We don't expect any bash variables in single quoted strings
return {envKey: envValueWithoutQuotes};
}
if (quotChar == '"') {
v = v.replaceAll('\\"', '"').replaceAll('\\n', '\n');
if (quoteChar == '"') {
envValueWithoutQuotes = envValueWithoutQuotes.replaceAll('\\"', '"').replaceAll('\\n', '\n');
}
final interpolatedValue = interpolate(v, env).replaceAll("\\\$", "\$");
return {k: interpolatedValue};
// Interpolate bash variables
final interpolatedValue = interpolate(envValueWithoutQuotes, envMap).replaceAll("\\\$", "\$");
return {envKey: interpolatedValue};
}

/// Substitutes $bash_vars in [val] with values from [env].
Expand All @@ -54,36 +56,35 @@ class Parser {
if ((m.group(1) ?? "") == "\\") {
return m.input.substring(m.start, m.end);
} else {
var k = m.group(3)!;
final k = m.group(3)!;
if (!_has(env, k)) return '';
return env[k]!;
}
});

/// If [val] is wrapped in single or double quotes, returns the quote character.
/// Otherwise, returns the empty string.

String surroundingQuote(String val) {
String getSurroundingQuoteCharacter(String val) {
if (!_surroundQuotes.hasMatch(val)) return '';
return _surroundQuotes.firstMatch(val)!.group(1)!;
}

/// Removes quotes (single or double) surrounding a value.
String unquote(String val) {
String removeSurroundingQuotes(String val) {
if (!_surroundQuotes.hasMatch(val)) {
return strip(val, includeQuotes: true).trim();
return removeCommentsFromLine(val, includeQuotes: true).trim();
}
return _surroundQuotes.firstMatch(val)!.group(2)!;
}

/// Strips comments (trailing or whole-line).
String strip(String line, {bool includeQuotes = false}) =>
String removeCommentsFromLine(String line, {bool includeQuotes = false}) =>
line.replaceAll(includeQuotes ? _commentWithQuotes : _comment, '').trim();

/// Omits 'export' keyword.
String swallow(String line) => line.replaceAll(_leadingExport, '').trim();
String trimExportKeyword(String line) => line.replaceAll(_leadingExport, '').trim();

bool _isValid(String s) => s.isNotEmpty && s.contains('=');
bool _isStringWithEqualsChar(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<String, String?> map, String key) =>
Expand Down
24 changes: 12 additions & 12 deletions test/parser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ void main() {
group('[Parser]', () {
setUp(() => rand = Random());
test('it swallows leading "export"', () {
var out = psr.swallow(' export foo = bar ');
var out = psr.trimExportKeyword(' export foo = bar ');
expect(out, equals('foo = bar'));

out = psr.swallow(' foo = bar export');
out = psr.trimExportKeyword(' foo = bar export');
expect(out, equals('foo = bar export'));
});

Expand Down Expand Up @@ -61,19 +61,19 @@ void main() {
});

test('it handles unquoted values', () {
var out = psr.unquote(' str ');
var out = psr.removeSurroundingQuotes(' str ');
expect(out, equals('str'));
});
test('it handles double quoted values', () {
var out = psr.unquote('"val "');
var out = psr.removeSurroundingQuotes('"val "');
expect(out, equals('val '));
});
test('it handles single quoted values', () {
var out = psr.unquote("' val'");
var out = psr.removeSurroundingQuotes("' val'");
expect(out, equals(' val'));
});
test('retain trailing single quote', () {
var out = psr.unquote("retained'");
var out = psr.removeSurroundingQuotes("retained'");
expect(out, equals("retained'"));
});

Expand Down Expand Up @@ -109,15 +109,15 @@ void main() {
});

test('it detects unquoted values', () {
var out = psr.surroundingQuote('no quotes here!');
var out = psr.getSurroundingQuoteCharacter('no quotes here!');
expect(out, isEmpty);
});
test('it detects double-quoted values', () {
var out = psr.surroundingQuote('"double quoted"');
var out = psr.getSurroundingQuoteCharacter('"double quoted"');
expect(out, equals('"'));
});
test('it detects single-quoted values', () {
var out = psr.surroundingQuote("'single quoted'");
var out = psr.getSurroundingQuoteCharacter("'single quoted'");
expect(out, equals("'"));
});

Expand Down Expand Up @@ -153,17 +153,17 @@ void main() {

test('it skips var substitution in single quotes', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.parseOne("some_var='my\$key_$r'", env: {'key_$r': 'val'});
var out = psr.parseOne("some_var='my\$key_$r'", envMap: {'key_$r': 'val'});
expect(out['some_var'], equals('my\$key_$r'));
});
test('it performs var subs in double quotes', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.parseOne('some_var="my\$key_$r"', env: {'key_$r': 'val'});
var out = psr.parseOne('some_var="my\$key_$r"', envMap: {'key_$r': 'val'});
expect(out['some_var'], equals('myval'));
});
test('it performs var subs without quotes', () {
var r = rand.nextInt(ceil); // avoid runtime collision with real env vars
var out = psr.parseOne("some_var=my\$key_$r", env: {'key_$r': 'val'});
var out = psr.parseOne("some_var=my\$key_$r", envMap: {'key_$r': 'val'});
expect(out['some_var'], equals('myval'));
});
});
Expand Down