dart-lang / yaml_edit

A library for YAML manipulation with comment and whitespace preservation.
https://pub.dev/packages/yaml_edit
BSD 3-Clause "New" or "Revised" License
27 stars 16 forks source link

how to set value to if the path path does not exist #12

Closed maxzod closed 1 year ago

maxzod commented 2 years ago

iam working on CLI features that see the content of the assets directory and add pathes to pubspec.yaml based on that it works with update if there is already a list in pubspec assets

    final doc = YamlEditor(pubspecContent);
    doc.update(['flutter','assets'], assets);

BUT if there is no assets key n the pubspec under flutter


flutter:
  uses-material-design: true

it throws exception

i think this is the expected behavior to throw when updating a null but how could i set value to a key that does not exist+

jonasfj commented 2 years ago

@maxzod, you should be able to insert a new key in a YamlMap using YamlEditor.

Can you make provide a complete minimal example of this issue?

jonasfj commented 1 year ago

I have no issues doing:

import 'package:yaml_edit/yaml_edit.dart';

void main() {
  final yamlEditor = YamlEditor('''
flutter:
  uses-material-design: true
''');
  yamlEditor.update([
    'flutter',
    'assets'
  ], {
    'foo': 'foo.jpg',
    'bar': 'bar.png',
  });
  print(yamlEditor.toString());
}

that prints:

flutter:
  assets:
    foo: foo.jpg
    bar: bar.png
  uses-material-design: true