Skip to content

Commit

Permalink
Improve yaml serialization quoting
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdm committed Apr 11, 2024
1 parent 2eb464a commit ffd6f0e
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 62 deletions.
2 changes: 1 addition & 1 deletion lib/src/source/path.dart
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class ResolvedPathDescription extends ResolvedDescription {
'relative': true,
};
}
return {'path': description.path, 'relative': p.relative('false')};
return {'path': description.path, 'relative': false};
}

@override
Expand Down
30 changes: 23 additions & 7 deletions lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -504,11 +504,27 @@ String prefixLines(String text, {String prefix = '| ', String? firstPrefix}) {
return lines.join('\n');
}

/// The subset of strings that don't need quoting in YAML.
///
/// This pattern does not strictly follow the plain scalar grammar of YAML,
/// which means some strings may be unnecessarily quoted, but it's much simpler.
final _unquotableYamlString = RegExp(r'^[a-zA-Z_-][a-zA-Z_0-9-]*$');
/// Does this string need quoting in yaml?
///
/// This mechanism does not strictly follow the plain scalar grammar of YAML,
/// which means some strings may be unnecessarily quoted.
bool _needsYamlQuotes(String s) {
// These must be quoted
if ([
'true', 'True', 'TRUE', //
'false', 'False', 'FALSE',
'null', 'Null', 'NULL',
].contains(s)) return true;
// Numbers must be quoted
if (RegExp(r'^[-+]?[0-9]*\.?[0-9]*(e[0-9]+)?$').hasMatch(s)) return true;
// Hex numbers must be quoted
if (RegExp(r'^0x[0-9]+$').hasMatch(s)) return true;

// Single non-number words containing a few special cases are ok.
if (RegExp(r'^[\.a-zA-Z_0-9-]+$').hasMatch(s)) return false;
// We are unsure, better safe than sorry
return true;
}

/// Converts [data], which is a parsed YAML object, to a pretty-printed string,
/// using indentation for maps.
Expand Down Expand Up @@ -536,7 +552,7 @@ String yamlToString(Object? data) {
first = false;

var keyString = key;
if (key is! String || !_unquotableYamlString.hasMatch(key)) {
if (key is! String || _needsYamlQuotes(key)) {
keyString = jsonEncode(key);
}

Expand All @@ -552,7 +568,7 @@ String yamlToString(Object? data) {
var string = data;

// Don't quote plain strings if not needed.
if (data is! String || !_unquotableYamlString.hasMatch(data)) {
if (data is! String || _needsYamlQuotes(data)) {
string = jsonEncode(data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -179,14 +179,14 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "2.2.3"
version: 2.2.3
transitive:
dependency: transitive
description:
name: transitive
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.0.0"
version: 1.0.0
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ packages:
description:
path: "."
ref: HEAD
resolved-ref: "1ea665d6de007c60308949db4c6ae8049c2395f0"
resolved-ref: 1ea665d6de007c60308949db4c6ae8049c2395f0
url: "../bar.git"
source: git
version: "1.0.0"
version: 1.0.0
foo:
dependency: "direct main"
description:
path: "."
ref: HEAD
resolved-ref: "9e8b8ad5091bec2c4724b1e0c8ff9d6e32a7eaa4"
resolved-ref: 9e8b8ad5091bec2c4724b1e0c8ff9d6e32a7eaa4
url: "../foo.git"
source: git
version: "1.0.0"
version: 1.0.0
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -218,18 +218,18 @@ packages:
description:
path: "."
ref: HEAD
resolved-ref: "1ea665d6de007c60308949db4c6ae8049c2395f0"
resolved-ref: 1ea665d6de007c60308949db4c6ae8049c2395f0
url: "../bar.git"
source: git
version: "1.0.0"
version: 1.0.0
foo:
dependency: "direct main"
description:
path: "."
ref: HEAD
resolved-ref: "385bb2860e7ebf9519993b2548d6cab74a6008ef"
resolved-ref: 385bb2860e7ebf9519993b2548d6cab74a6008ef
url: "../foo.git"
source: git
version: "2.0.0"
version: 2.0.0
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
boo:
dependency: "direct main"
description:
name: boo
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
foo:
dependency: "direct main"
description:
name: foo
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -383,22 +383,22 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
boo:
dependency: "direct main"
description:
name: boo
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
foo:
dependency: "direct main"
description:
name: foo
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.2.4"
version: 1.2.4
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.1.1"
version: 1.1.1
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -167,6 +167,6 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.1.1"
version: 1.1.1
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.0.0"
version: 1.0.0
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -112,6 +112,6 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.0.0"
version: 1.0.0
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ packages:
name: bar
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
boo:
dependency: "direct main"
description:
name: boo
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
foo:
dependency: "direct main"
description:
name: foo
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -365,20 +365,20 @@ packages:
name: bar
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
boo:
dependency: "direct main"
description:
name: boo
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
foo:
dependency: "direct main"
description:
name: foo
url: "http://localhost:$PORT"
source: hosted
version: "1.2.4"
version: 1.2.4
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ packages:
sha256: $SHA256
url: https://pub.dartlang.org
source: hosted
version: "1.2.3"
version: 1.2.3
foo:
dependency: "direct main"
description:
name: foo
sha256: $SHA256
url: https://pub.dartlang.org
source: hosted
version: "1.2.3"
version: 1.2.3
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -339,14 +339,14 @@ packages:
sha256: $SHA256
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.3"
version: 1.2.3
foo:
dependency: "direct main"
description:
name: foo
sha256: $SHA256
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.4"
version: 1.2.4
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ packages:
path: "../bar"
relative: true
source: path
version: "1.0.0"
version: 1.0.0
foo:
dependency: "direct main"
description:
name: foo
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.0.0"
version: 1.0.0
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -177,14 +177,14 @@ packages:
path: "../bar"
relative: true
source: path
version: "1.0.0"
version: 1.0.0
foo:
dependency: "direct main"
description:
name: foo
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "2.0.0"
version: 2.0.0
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.2.3"
version: 1.2.3
transitive:
dependency: transitive
description:
name: transitive
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.0.0"
version: 1.0.0
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -217,6 +217,6 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "2.2.3"
version: 2.2.3
sdks:
dart: ">=3.0.2 <4.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.1.1"
version: 1.1.1
sdks:
dart: ">=3.0.2 <4.0.0"
-------------------------------- END OF OUTPUT ---------------------------------
Expand Down Expand Up @@ -140,6 +140,6 @@ packages:
sha256: $SHA256
url: "http://localhost:$PORT"
source: hosted
version: "1.1.1"
version: 1.1.1
sdks:
dart: ">=3.0.2 <4.0.0"
Loading

0 comments on commit ffd6f0e

Please sign in to comment.