-
Notifications
You must be signed in to change notification settings - Fork 16
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
Duplicate mapping key and parsing error when trying to add a map entry #69
Comments
And here's an even weirder error if neither map has the 'id' key import 'package:yaml_edit/yaml_edit.dart';
void main() {
const yaml = '''
- name: foo
- name: bar
''';
final yamlEditor = YamlEditor(yaml);
yamlEditor.update(
[
1,
'id',
],
"B",
);
print(yamlEditor);
} Unhandled exception:
Assertion failed: (package:yaml_edit) Modification did not result in expected result.
# YAML before edit:
> - name: foo
> - name: bar
>
# YAML after edit:
> - name: foo
> id: B
> - name: bar
>
Please file an issue at:
https://github.com/dart-lang/yaml_edit/issues/new?labels=bug
#0 YamlEditor._performEdit (package:yaml_edit/src/editor.dart:578:7)
#1 YamlEditor.update (package:yaml_edit/src/editor.dart:271:14)
#2 main (package:yaml_playground/example.dart:12:14)
#3 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#4 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12) |
Does this also happen if the top-level is not a list? To be clear, I don't think I have time to dive into all issues that this package might have. |
@jonasfj I managed to debug this and find the culprit. When mutating the map, Lines 119 to 139 in 3191934
There is a dangling check for the index based on the ordering after the loop that presents an edge case for a map with only one key. That may or may not default to zero depending on the results of the If you were to comment out the line,
Also, shouldn't the ordering compare every key with the previous key and not return after just one match? |
@kekavc24 fantastic investigation! I'd suggest option (2). I don't see how (1) would solve the issue, it would just cause the issue to only happen when the option to respect key ordering is enabled. I think it's great that I mean that code only respects ordering, if they keys are already ordered. So it seems reasonable that if there is only 1 (or zero) keys, then we declare that there is no ordering and we just append the key. I suppose in an ideal world, we would scan the rest of the YAML document to see if other keys are ordered, and if so, then we'd make sure to order in case there is only 1 key. @kekavc24 I'd be happy to review a PR that:
|
I'm trying to add a key to a map and set its value, but running into issues.
Example code for Duplicate mapping key error
For this, I get the following error:
If I swap the order around and try to update the first list item instead, I get a different error
In a larger example that I can't duplicate in this smaller example, the error I get in this case is:
Expected a key while parsing a block mapping.
Using version yaml_edit: ^2.2.0
The text was updated successfully, but these errors were encountered: