Fix 55 (#75)

Fix removal of last key from map in block-mode when value is empty.

Fixes #55.

When the value is empty the `SourceSpan` for the `YamlNode` representing
the value in a map points to the colon.

Example:
```yaml
foo:
  bar:
```

The `YamlNode` for `foo.bar` has a value of `null` and starts and ends
at the colon `:` following `bar`. This means that removal might leave
the colon behind, which causes invalid YAML.

We have the same issue when removing `foo.bar` from the following YAML
document:

```yaml
foo:
  baz: true
  bar:
```

However, in this case, we have a hack that ensures we always strip away
the any comments that follows `bar`. We do this by deleting up-to the
next newline. If we apply the same hack when removing `foo.bar` in the
first example, then it works.

One could argue that it works by accident, but it's kind of desired that
trailing comments are removed, when the value they are trailing is
removed.
15 files changed
tree: 0a70e723a1eedf9e9e7f1e538a6d5be7800ace10
  1. .github/
  2. example/
  3. lib/
  4. test/
  5. .gitignore
  6. analysis_options.yaml
  7. CHANGELOG.md
  8. LICENSE
  9. pubspec.yaml
  10. README.md
README.md

Dart CI pub package package publisher Coverage Status

A library for YAML manipulation while preserving comments.

Usage

A simple usage example:

import 'package:yaml_edit/yaml_edit.dart';

void main() {
  final yamlEditor = YamlEditor('{YAML: YAML}');
  yamlEditor.update(['YAML'], "YAML Ain't Markup Language");
  print(yamlEditor);
  // Expected output:
  // {YAML: YAML Ain't Markup Language}
}

Example: Converting JSON to YAML (block formatted)

void main() {
  final jsonString = r'''
{
  "key": "value",
  "list": [
    "first",
    "second",
    "last entry in the list"
  ],
  "map": {
    "multiline": "this is a fairly long string with\nline breaks..."
  }
}
''';
  final jsonValue = json.decode(jsonString);

  // Convert jsonValue to YAML
  final yamlEditor = YamlEditor('');
  yamlEditor.update([], jsonValue);
  print(yamlEditor.toString());
}

Testing

Testing is done in two strategies: Unit testing (/test/editor_test.dart) and Golden testing (/test/golden_test.dart). More information on Golden testing and the input/output format can be found at /test/testdata/README.md.

These tests are automatically run with pub run test.

Limitations

  1. Users are not allowed to define tags in the modifications.
  2. Map keys will always be added in the flow style.