diff --git a/CHANGELOG.md b/CHANGELOG.md index 79b1c30..44adbb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,14 @@ +## 1.0.3 + +- `YAMLWriter`: + - Added `allowUnquotedStrings`. + - Rename `identSize` to `indentSize`. + - Deprecate `identSize` field. +- lints: ^2.0.1 +- test: ^1.22.0 +- dependency_validator: ^3.2.2 +- coverage: ^1.6.1 + ## 1.0.2 - Fixed serialization of an empty `List` to `[]` diff --git a/lib/src/yaml_writer_base.dart b/lib/src/yaml_writer_base.dart index 05c9bf8..c4cf2f3 100644 --- a/lib/src/yaml_writer_base.dart +++ b/lib/src/yaml_writer_base.dart @@ -2,11 +2,28 @@ import 'dart:convert'; /// YAML Writer. class YAMLWriter extends Converter { - final int identSize; + /// The indentation size. + final int indentSize; + + @Deprecated("Use `indentSize`.") + int get identSize => indentSize; + + /// If `true` it will allow unquoted strings. + final bool allowUnquotedStrings; final String _ident; - YAMLWriter({this.identSize = 2}) : _ident = ''.padLeft(identSize, ' '); + YAMLWriter( + {int indentSize = 2, + @Deprecated("Use `indentSize`.") int? identSize, + this.allowUnquotedStrings = false}) + : indentSize = _resolveIndentSize(indentSize, identSize), + _ident = ''.padLeft(_resolveIndentSize(indentSize, identSize), ' '); + + static int _resolveIndentSize(int indentSize, int? identSize) { + var indent = (identSize ?? indentSize); + return indent < 0 ? 0 : indent; + } /// Used to convert objects to an encodable version. Object? Function(dynamic object)? toEncodable; @@ -84,7 +101,14 @@ class YAMLWriter extends Converter { return true; } else { - if (!node.contains("'")) { + var containsSingleQuote = node.contains("'"); + + if (allowUnquotedStrings && + !containsSingleQuote && + _isValidUnquotedString(node)) { + s.write(node); + return false; + } else if (!containsSingleQuote) { s.write("'"); s.write(node); s.write("'"); @@ -99,6 +123,14 @@ class YAMLWriter extends Converter { } } + static final _regexpInvalidUnquotedChars = RegExp( + r'[^0-9a-zA-ZàèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ@/. \t-]'); + + bool _isValidUnquotedString(String s) => + !_regexpInvalidUnquotedChars.hasMatch(s) && + !s.startsWith('@') && + !s.startsWith('-'); + static dynamic _defaultToEncodable(dynamic object) => object.toJson(); bool _writeObject(Object node, StringBuffer s, {String currentIdent = ''}) { diff --git a/pubspec.yaml b/pubspec.yaml index 048cd1a..f817621 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,13 +1,13 @@ name: yaml_writer description: A library to write YAML documents, supporting Object encoding and 'dart:convert' 'Converter'. -version: 1.0.2 +version: 1.0.3 homepage: https://github.com/gmpassos/yaml_writer environment: sdk: '>=2.12.0 <3.0.0' dev_dependencies: - lints: ^2.0.0 - test: ^1.17.12 - dependency_validator: ^3.2.0 - coverage: ^1.1.0 + lints: ^2.0.1 + test: ^1.22.0 + dependency_validator: ^3.2.2 + coverage: ^1.6.1 diff --git a/test/yaml_writer_test.dart b/test/yaml_writer_test.dart index 530e331..75e5c6d 100644 --- a/test/yaml_writer_test.dart +++ b/test/yaml_writer_test.dart @@ -7,6 +7,111 @@ void main() { print('---------------------------------------------'); }); + test('unquoted string', () { + var yamlWriter = YAMLWriter(allowUnquotedStrings: true); + + var tree = { + 'foo': { + 's1': 'Unquoted string with some@email and a /path/file.', + 's2': 'Quoted \$string.', + 's3': '@Quoted string.', + 's4': '-Quoted string.', + 's5': 'UnQuoted@string.', + 's6': 'UnQuoted-string.', + } + }; + + var yaml = yamlWriter.write(tree); + + print(yaml); + + expect(yaml, equals(r''' +foo: + s1: Unquoted string with some@email and a /path/file. + s2: 'Quoted $string.' + s3: '@Quoted string.' + s4: '-Quoted string.' + s5: UnQuoted@string. + s6: UnQuoted-string. +''')); + + expect(yamlWriter.convert(tree), equals(yaml)); + + var yamlWriterQuoted = YAMLWriter(allowUnquotedStrings: false); + + var yaml2 = yamlWriterQuoted.write(tree); + + print(yaml2); + + expect(yaml2, equals(r''' +foo: + s1: 'Unquoted string with some@email and a /path/file.' + s2: 'Quoted $string.' + s3: '@Quoted string.' + s4: '-Quoted string.' + s5: 'UnQuoted@string.' + s6: 'UnQuoted-string.' +''')); + }); + + test('indent 1', () { + var yamlWriter = YAMLWriter(indentSize: 1); + + var tree = { + 'foo': { + 's1': 'Some string', + } + }; + + var yaml = yamlWriter.write(tree); + + print(yaml); + + expect(yaml, equals(r''' +foo: + s1: 'Some string' +''')); + }); + + test('indent 5', () { + var yamlWriter = YAMLWriter(indentSize: 5); + + var tree = { + 'foo': { + 's1': 'Some string', + } + }; + + var yaml = yamlWriter.write(tree); + + print(yaml); + + expect(yaml, equals(r''' +foo: + s1: 'Some string' +''')); + }); + + test('@deprecated identSize 8', () { + // ignore: deprecated_member_use_from_same_package + var yamlWriter = YAMLWriter(identSize: 3); + + var tree = { + 'foo': { + 's1': 'Some string', + } + }; + + var yaml = yamlWriter.write(tree); + + print(yaml); + + expect(yaml, equals(r''' +foo: + s1: 'Some string' +''')); + }); + test('list', () { var yamlWriter = YAMLWriter(); diff --git a/yaml_writer.iml b/yaml_writer.iml index da93800..a7a886f 100644 --- a/yaml_writer.iml +++ b/yaml_writer.iml @@ -1,13 +1,13 @@ - + - + - +