-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fe871b9
Showing
13 changed files
with
601 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
name: Dart CI | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
branches: [ master ] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
container: | ||
image: google/dart:latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Dart version | ||
run: | | ||
dart --version | ||
uname -a | ||
- name: Install dependencies | ||
run: dart pub get | ||
- name: dart format | ||
run: dart format -o none --set-exit-if-changed . | ||
- name: dart analyze | ||
run: dart analyze --fatal-infos --fatal-warnings . | ||
- name: dependency_validator | ||
run: dart run dependency_validator | ||
- name: Run tests | ||
run: dart test | ||
- name: Generate coverage report | ||
run: dart run test_cov | ||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v1 | ||
with: | ||
directory: ./coverage/ | ||
flags: unittests | ||
env_vars: OS,DART | ||
fail_ci_if_error: true | ||
verbose: true | ||
- name: dart pub publish --dry-run | ||
run: dart pub publish --dry-run | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Files and directories created by pub. | ||
.dart_tool/ | ||
.packages | ||
|
||
# Conventional directory for build outputs. | ||
build/ | ||
|
||
# test_cov files: | ||
coverage/ | ||
test/.test_cov.dart | ||
|
||
# Omit committing pubspec.lock for library packages; see | ||
# https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
pubspec.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
- Initial version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
Copyright 2014, the Dart project authors. All rights reserved. | ||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are | ||
met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# yaml_writer | ||
|
||
[](https://pub.dartlang.org/packages/yaml_writer) | ||
[](https://dart.dev/null-safety) | ||
[](https://github.com/gmpassos/yaml_writer/actions) | ||
[](https://github.com/gmpassos/yaml_writer/releases) | ||
[](https://github.com/gmpassos/yaml_writer/network) | ||
[](https://github.com/gmpassos/yaml_writer/commits/master) | ||
[](https://github.com/gmpassos/yaml_writer/pulls) | ||
[](https://github.com/gmpassos/yaml_writer) | ||
[](https://github.com/gmpassos/yaml_writer/blob/master/LICENSE) | ||
|
||
A library to write YAML documents. | ||
|
||
## Usage | ||
|
||
A simple usage example: | ||
|
||
```dart | ||
import 'package:yaml_writer/yaml_writer.dart'; | ||
void main() { | ||
var yamlWriter = YAMLWriter(); | ||
var yamlDoc = yamlWriter.write({ | ||
'name': 'Joe', | ||
'ids': [10, 20, 30], | ||
'desc': 'This is\na multiline\ntext', | ||
'enabled': true, | ||
}); | ||
print(yamlDoc); | ||
} | ||
``` | ||
|
||
OUTPUT: | ||
|
||
```text | ||
name: 'Joe' | ||
ids: | ||
- 10 | ||
- 20 | ||
- 30 | ||
desc: |- | ||
This is | ||
a multiline | ||
text | ||
enabled: true | ||
``` | ||
|
||
## YAML Strings | ||
|
||
The writer will try to encode strings using the best format depending on the text, | ||
avoiding to escape `"` and using `|` for multiline texts. | ||
|
||
## Encoding Objects | ||
|
||
You can use `YAMLWriter.toEncodable` to convert an Object to an encodable version, | ||
similar to what is done by `dart:convert` `JsonEncoder`. | ||
|
||
If `YAMLWriter.toEncodable` is not set, it will try to call `toJson()`. | ||
|
||
## Features and bugs | ||
|
||
Please file feature requests and bugs at the [issue tracker][tracker]. | ||
|
||
[tracker]: https://github.com/gmpassos/yaml_writer/issues | ||
|
||
|
||
## Author | ||
|
||
Graciliano M. Passos: [gmpassos@GitHub][github]. | ||
|
||
[github]: https://github.com/gmpassos | ||
|
||
## License | ||
|
||
Dart free & open-source [license](https://github.com/dart-lang/stagehand/blob/master/LICENSE). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Defines a default set of lint rules enforced for projects at Google. For | ||
# details and rationale, see | ||
# https://github.com/dart-lang/pedantic#enabled-lints. | ||
|
||
include: package:pedantic/analysis_options.yaml | ||
|
||
# For lint rules and documentation, see http://dart-lang.github.io/linter/lints. | ||
|
||
# Uncomment to specify additional rules. | ||
# linter: | ||
# rules: | ||
# - camel_case_types | ||
|
||
# analyzer: | ||
# exclude: | ||
# - path/to/excluded/files/** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:yaml_writer/yaml_writer.dart'; | ||
|
||
void main() { | ||
var yamlWriter = YAMLWriter(); | ||
|
||
var yamlDoc = yamlWriter.write({ | ||
'name': 'Joe', | ||
'ids': [10, 20, 30], | ||
'desc': 'This is\na multiline\ntext', | ||
'enabled': true, | ||
}); | ||
|
||
print(yamlDoc); | ||
} | ||
|
||
//--------------------- | ||
// OUTPUT: | ||
//--------------------- | ||
// name: 'Joe' | ||
// ids: | ||
// - 10 | ||
// - 20 | ||
// - 30 | ||
// desc: |- | ||
// This is | ||
// a multiline | ||
// text | ||
// enabled: true | ||
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import 'dart:convert'; | ||
|
||
/// YAML Writer. | ||
class YAMLWriter extends Converter<Object?, String> { | ||
final int identSize; | ||
|
||
final String _ident; | ||
|
||
YAMLWriter({this.identSize = 2}) : _ident = ''.padLeft(identSize, ' '); | ||
|
||
/// Used to convert objects to an encodable version. | ||
Object? Function(dynamic object)? toEncodable; | ||
|
||
/// Converts [input] to an YAML document as [String]. | ||
/// | ||
/// This implements `dart:convert` [Converter]. | ||
/// | ||
/// - Calls [write]. | ||
@override | ||
String convert(Object? input) => write(input); | ||
|
||
/// Writes [node] to an YAML document as [String]. | ||
String write(Object? node) { | ||
var s = StringBuffer(); | ||
_writeTo(node, s); | ||
return s.toString(); | ||
} | ||
|
||
bool _writeTo(Object? node, StringBuffer s, {String currentIdent = ''}) { | ||
if (node == null) { | ||
return _writeNull(s); | ||
} else if (node is num) { | ||
_writeNum(node, s); | ||
return false; | ||
} else if (node is bool) { | ||
_writeBool(node, s); | ||
return false; | ||
} else if (node is List) { | ||
return _writeList(node, s, currentIdent: currentIdent); | ||
} else if (node is Map) { | ||
return _writeMap(node, s, currentIdent: currentIdent); | ||
} else if (node is String) { | ||
return _writeString(node, s, currentIdent: currentIdent); | ||
} else { | ||
return _writeObject(node, s, currentIdent: currentIdent); | ||
} | ||
} | ||
|
||
bool _writeNull(StringBuffer s) { | ||
s.write('null'); | ||
return false; | ||
} | ||
|
||
bool _writeNum(num node, StringBuffer s) { | ||
s.write(node); | ||
return false; | ||
} | ||
|
||
bool _writeBool(bool node, StringBuffer s) { | ||
s.write(node); | ||
return false; | ||
} | ||
|
||
bool _writeString(String node, StringBuffer s, {String currentIdent = ''}) { | ||
if (node.contains('\n')) { | ||
var nextIdent = currentIdent + _ident; | ||
|
||
var endsWithLineBreak = node.endsWith('\n'); | ||
|
||
List<String> lines; | ||
if (endsWithLineBreak) { | ||
s.write('|\n'); | ||
lines = node.substring(0, node.length - 1).split('\n'); | ||
} else { | ||
s.write('|-\n'); | ||
lines = node.split('\n'); | ||
} | ||
|
||
for (var line in lines) { | ||
s.write(nextIdent); | ||
s.write(line); | ||
s.write('\n'); | ||
} | ||
|
||
return true; | ||
} else { | ||
if (!node.contains("'")) { | ||
s.write("'"); | ||
s.write(node); | ||
s.write("'"); | ||
return false; | ||
} else { | ||
var str = node.replaceAll('\\', '\\\\').replaceAll('"', '\\"'); | ||
s.write('"'); | ||
s.write(str); | ||
s.write('"'); | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
static dynamic _defaultToEncodable(dynamic object) => object.toJson(); | ||
|
||
bool _writeObject(Object node, StringBuffer s, {String currentIdent = ''}) { | ||
var toEncodable = this.toEncodable ?? _defaultToEncodable; | ||
var o = toEncodable(node); | ||
return _writeTo(o, s, currentIdent: currentIdent); | ||
} | ||
|
||
bool _writeList(Iterable node, StringBuffer s, {String currentIdent = ''}) { | ||
if (s.isNotEmpty) { | ||
s.write('\n'); | ||
} | ||
|
||
var nextIdent = currentIdent + _ident; | ||
|
||
var wroteLineBreak = false; | ||
|
||
for (var item in node) { | ||
s.write(currentIdent); | ||
s.write('- '); | ||
var ln = _writeTo(item, s, currentIdent: nextIdent); | ||
if (!ln) { | ||
s.write('\n'); | ||
wroteLineBreak = true; | ||
} | ||
} | ||
|
||
return wroteLineBreak; | ||
} | ||
|
||
bool _writeMap(Map node, StringBuffer s, {String currentIdent = ''}) { | ||
if (s.isNotEmpty) { | ||
s.write('\n'); | ||
} | ||
|
||
var nextIdent = currentIdent + _ident; | ||
|
||
var wroteLineBreak = false; | ||
|
||
for (var entry in node.entries) { | ||
s.write(currentIdent); | ||
s.write(entry.key); | ||
s.write(': '); | ||
var ln = _writeTo(entry.value, s, currentIdent: nextIdent); | ||
if (!ln) { | ||
s.write('\n'); | ||
wroteLineBreak = true; | ||
} | ||
} | ||
|
||
return wroteLineBreak; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/// YAML Writer Library. | ||
library yaml_writer; | ||
|
||
export 'src/yaml_writer_base.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: yaml_writer | ||
description: A library to write YAML documents. | ||
version: 1.0.0 | ||
homepage: https://github.com/gmpassos/yaml_writer | ||
|
||
environment: | ||
sdk: '>=2.12.0 <3.0.0' | ||
|
||
dev_dependencies: | ||
pedantic: ^1.11.0 | ||
test: ^1.17.9 | ||
dependency_validator: ^3.1.0 | ||
test_cov: ^1.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/bin/bash | ||
|
||
dart run test_cov | ||
|
||
genhtml -o coverage coverage/lcov.info | ||
|
||
open coverage/index.html |
Oops, something went wrong.