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

YamlEditor appendToList and insertIntoList inserts new item into next yaml item rather than at end of list #23

Closed GeekVisit closed 1 year ago

GeekVisit commented 1 year ago

The following results in an Unhandled Exception "Assertion failed: (package:yaml_edit) Modification did not result in expected result."

import 'dart:io';

import 'package:yaml_edit/yaml_edit.dart';

Future<void> main() async {
  YamlEditor yamlEditorFile =
      YamlEditor(File('test/gh_test.yml').readAsStringSync());

  try {
    // yamlEditorFile.insertIntoList(
    //     ['school'],
    //     3,
    //     {
    //       'student': {'first_name': 'Henry', 'last_name': 'Smith'}
    //     });

    yamlEditorFile.appendToList([
      'school'
    ], {
      'student': {'first_name': 'Henry', 'last_name': 'Smith'}
    });
  } on Exception catch (e) {
    print(e);
  }

  print(yamlEditorFile);
}

gh_test.yml is:

# comment
school:
  - student:
      first_name: Harry
      last_name: "Reynolds"
  - student:
      first_name: "Sally"
      last_name: Rheims
  - student:
      first_name: "George"
      last_name: "Silver"
teachers:
  - english:
      name: "Johnson"
  - science:
      name: "Smith"

The insertIntoList call results in the same if the index is the next one (i.e., 3 - commented code in example above).

Expected behavior in all cases is that it inserts it correctly in the school list after student George Silver.

Instead the result in both examples is that the inserted list item (Henry Smith) is inserted incorrectly as the first item of the next list (i.e., teachers) as shown by the "Modification did not result in expected result" message.":


> teachers:
>   - student:
>       first_name: Henry
>       last_name: Smith
>   - english:
>       name: "Johnson"
>   - science:
>       name: "Smith"

the full error message:


Unhandled exception:
Assertion failed: (package:yaml_edit) Modification did not result in expected result.

# YAML before edit:
> # comment
> school:
>   - student:
>       first_name: Harry
>       last_name: "Reynolds"
>   - student:
>       first_name: "Sally"
>       last_name: Rheims
>   - student:
>       first_name: "George"
>       last_name: "Silver"
> teachers:
>   - english:
>       name: "Johnson"
>   - science:
>       name: "Smith"

# YAML after edit:
> # comment
> school:
>   - student:

>       first_name: Harry
>       last_name: "Reynolds"
>   - student:
>       first_name: "Sally"
>       last_name: Rheims
>   - student:
>       first_name: "George"
>       last_name: "Silver"
> teachers:
>   - student:
>       first_name: Henry
>       last_name: Smith
>   - english:
>       name: "Johnson"
>   - science:
>       name: "Smith"

Please file an issue at:
https://github.com/google/dart-neats/issues/new?labels=pkg%3Ayaml_edit%2C+pending-triage&template=yaml_edit.md

#0      YamlEditor._performEdit
#1      YamlEditor.insertIntoList
#2      YamlEditor.appendToList
#3      main

#4      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)

Exited (255)

So it appears it can't detect the end of the list. The example yaml is correctly validated. If I insert a non-list in between the two lists in the example yaml file, for example the boolean "public: false", I get "Mapping Values are not allowed here" as it attempts to insert it under the boolean yaml:

Here's that error message:


Error on line 13, column 12: Mapping values are not allowed here. Did you miss a colon earlier?
   ╷
13 │   - student:
   │            ^
   ╵
# comment
school:
  - student:
      first_name: Harry
      last_name: "Reynolds"
  - student:
      first_name: "Sally"
      last_name: Rheims
  - student:
      first_name: "George"
      last_name: "Silver"
public: false
  - student:
      first_name: Henry
      last_name: Smith
teachers:
  - english:
      name: "Johnson"
  - science:
      name: "Smith"
Exited
jonasfj commented 1 year ago

Anyone interested in working on this might want to find a smaller example first.

I'm happy to review and land any good fixes for this issue.

Ishad-M-I-M commented 1 year ago

I'm like to start with contributing to open source projects. @jonasfj Can I get this assigned to me.

jonasfj commented 1 year ago

Hmm, @Ishad-M-I-M feel free to file another PR updating the CHANGELOG.md with a bullet point explaining what bug was fixed.

Ishad-M-I-M commented 1 year ago

@jonasfj Done. https://github.com/dart-lang/yaml_edit/pull/37

Thank you for your guidance