Closed chriskelly closed 5 months ago
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.
But if you ping me on a PR, or drop me an email jonasfj@google.com
, I'm happy to help review a PR with appropriate test cases.
@jonasfj I managed to debug this and find the culprit. When mutating the map, YamlEditor
tries to respect the key ordering.
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 compareTo
which taints the SourceEdit
to be used. This bubbles up and presents itself in all the situations stated by @chriskelly
If you were to comment out the line, YamlEditor
works as expected. Possible ways to tackle this:
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 YamlEditor
respects the ordering, if the keys are already alphabetically ordered.
I think it's a reasonable heuristic.
But if there is only 1 key (or less) we should just append to the end of the map. That's seems very reasonable.
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. But that also seems very very complicated :rofl:
@kekavc24 I'd be happy to review a PR that:
map.length
if map.length <= 1
.
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