Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpassos committed Jul 30, 2021
0 parents commit fe871b9
Show file tree
Hide file tree
Showing 13 changed files with 601 additions and 0 deletions.
45 changes: 45 additions & 0 deletions .github/workflows/dart.yml
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

14 changes: 14 additions & 0 deletions .gitignore
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

- Initial version.
26 changes: 26 additions & 0 deletions LICENSE
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.
78 changes: 78 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# yaml_writer

[![pub package](https://img.shields.io/pub/v/yaml_writer.svg?logo=dart&logoColor=00b9fc)](https://pub.dartlang.org/packages/yaml_writer)
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
[![CI](https://img.shields.io/github/workflow/status/gmpassos/yaml_writer/Dart%20CI/master?logo=github-actions&logoColor=white)](https://github.com/gmpassos/yaml_writer/actions)
[![GitHub Tag](https://img.shields.io/github/v/tag/gmpassos/yaml_writer?logo=git&logoColor=white)](https://github.com/gmpassos/yaml_writer/releases)
[![New Commits](https://img.shields.io/github/commits-since/gmpassos/yaml_writer/latest?logo=git&logoColor=white)](https://github.com/gmpassos/yaml_writer/network)
[![Last Commits](https://img.shields.io/github/last-commit/gmpassos/yaml_writer?logo=git&logoColor=white)](https://github.com/gmpassos/yaml_writer/commits/master)
[![Pull Requests](https://img.shields.io/github/issues-pr/gmpassos/yaml_writer?logo=github&logoColor=white)](https://github.com/gmpassos/yaml_writer/pulls)
[![Code size](https://img.shields.io/github/languages/code-size/gmpassos/yaml_writer?logo=github&logoColor=white)](https://github.com/gmpassos/yaml_writer)
[![License](https://img.shields.io/github/license/gmpassos/yaml_writer?logo=open-source-initiative&logoColor=green)](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).
16 changes: 16 additions & 0 deletions analysis_options.yaml
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/**
29 changes: 29 additions & 0 deletions example/yaml_writer_example.dart
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
//
154 changes: 154 additions & 0 deletions lib/src/yaml_writer_base.dart
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;
}
}
4 changes: 4 additions & 0 deletions lib/yaml_writer.dart
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';
13 changes: 13 additions & 0 deletions pubspec.yaml
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
7 changes: 7 additions & 0 deletions run_test_cov.sh
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
Loading

0 comments on commit fe871b9

Please sign in to comment.