Skip to content

Commit

Permalink
v1.0.3
Browse files Browse the repository at this point in the history
- `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
  • Loading branch information
gmpassos committed Dec 4, 2022
1 parent a7ae574 commit 861e8ed
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 11 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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 `[]`
Expand Down
38 changes: 35 additions & 3 deletions lib/src/yaml_writer_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,28 @@ import 'dart:convert';

/// YAML Writer.
class YAMLWriter extends Converter<Object?, String> {
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;
Expand Down Expand Up @@ -84,7 +101,14 @@ class YAMLWriter extends Converter<Object?, String> {

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("'");
Expand All @@ -99,6 +123,14 @@ class YAMLWriter extends Converter<Object?, String> {
}
}

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 = ''}) {
Expand Down
10 changes: 5 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
105 changes: 105 additions & 0 deletions test/yaml_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 3 additions & 3 deletions yaml_writer.iml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/build" />
<excludeFolder url="file://$MODULE_DIR$/.pub" />
<excludeFolder url="file://$MODULE_DIR$/.dart_tool" />
<excludeFolder url="file://$MODULE_DIR$/coverage" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Dart SDK" level="project" />
<orderEntry type="library" name="Dart Packages" level="project" />
Expand Down

0 comments on commit 861e8ed

Please sign in to comment.