Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API request to replace map key/value pair #18

Open
srawlins opened this issue Mar 18, 2022 · 2 comments
Open

New API request to replace map key/value pair #18

srawlins opened this issue Mar 18, 2022 · 2 comments
Labels
contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) type-enhancement A request for a change that isn't a bug

Comments

@srawlins
Copy link
Member

I'd love an API (replaceMapEntry?) to replace an entire key/value pair:

final yamlEditor = YamlEditor('''
analyzer:
  foo: false
''');
  yamlEditor.replaceMapEntry(['analyzer', 'foo'], {'bar': true});
  print(yamlEditor);

/*
analyzer:
  bar: true
*/

The current methods for doing this don't work well for us:

  • add/update bar, remove foo: results in conflicting SourceEdit offsets.
  • remove foo, add/update bar: results in a flow-style map: analyzer: {foo: false}.

I'd be happy to contribute this API!

@devoncarew devoncarew added the type-enhancement A request for a change that isn't a bug label Nov 11, 2022
@jonasfj
Copy link
Member

jonasfj commented Dec 21, 2022

remove foo, add/update bar: results in a flow-style map: analyzer: {foo: false}.

This can be worked around by using wrapAsYamlNode and specify the style.


I have no problems with:

import 'package:yaml/yaml.dart';
import 'package:yaml_edit/yaml_edit.dart';

void main() {
  final yamlEditor = YamlEditor('''
analyzer:
  foo: false
''');
  //yamlEditor.replaceMapEntry(['analyzer', 'foo'], {'bar': true});
  yamlEditor.update(['analyzer', 'bar'], true);
  yamlEditor.remove(['analyzer', 'foo']);
  print(yamlEditor);

  var s = '''
analyzer:
  foo: false
''';

  for (final edit in yamlEditor.edits) {
    s = edit.apply(s);
  }
  print(s);

/*
analyzer:
  bar: true
*/

  final yamlEditor2 = YamlEditor('''
analyzer:
  foo: false
''');
  yamlEditor2.remove(['analyzer', 'foo']);
  yamlEditor2.update(
    ['analyzer'],
    wrapAsYamlNode(
      {'bar': true},
      collectionStyle: CollectionStyle.BLOCK,
    ),
  );
  print(yamlEditor2);

  s = '''
analyzer:
  foo: false
''';

  for (final edit in yamlEditor2.edits) {
    s = edit.apply(s);
  }
  print(s);
}

I'm not sure how the SourceEdits have invalid offsets, I think it's important to apply them in the right order.

@jonasfj
Copy link
Member

jonasfj commented Dec 21, 2022

That said, I can see it being useful to have a method to update a key, but I'm not sure how that isn't just removing an existing key and inserting a new key.

Maybe, the difference is that inserting a new key doesn't put the where the old key was. But I think, yaml_edit will insert keys correctly if they are already in alphabetical order (I think).

But maybe a replace-key makes sense, like yamlEditor.replaceMapEntry(['path', 'to', 'map'], old_key, new_key, value) would make sense. I think having the path being a path to map makes sense, because we usually have a map to the object being modified.

But are we sure it's not better to use the add/remove or remove/add approach? Maybe with wrapAsYamlNode to force styling.

@jonasfj jonasfj added the contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) label May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributions-welcome Contributions welcome to help resolve this (the resolution is expected to be clear from the issue) type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants