Skip to content

pkg/yaml: YamlMap iteration order doesn't preserve yaml document order #21328

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

Closed
DartBot opened this issue Oct 14, 2014 · 5 comments
Closed

pkg/yaml: YamlMap iteration order doesn't preserve yaml document order #21328

DartBot opened this issue Oct 14, 2014 · 5 comments
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Oct 14, 2014

This issue was originally filed by @seaneagan


Here's the failing test:

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

main() {
  Map map = loadYaml(yaml);
  expect(map.keys.toList(), ['foo', 'bar', 'baz']);
}

var yaml = '''
foo: x
bar: y
baz: z
''';

Guessing it would just need to switch to an underlying LinkedHashMap so long as the keys are inserted in document order. I expected this to work given that it does with JSON.

@DartBot
Copy link
Author

DartBot commented Oct 15, 2014

This comment was originally written by @seaneagan


YamlMap.nodes should preserve document order as well of course.

@lrhn
Copy link
Member

lrhn commented Oct 17, 2014

Added Pkg-Yaml, Area-Pkg, Triaged labels.

@nex3
Copy link
Member

nex3 commented Oct 17, 2014

The YAML spec actually explicitly forbids the native data structure to be based on the ordering of the keys in the document: http://yaml.org/spec/1.2/spec.html#id2763035


Added NotPlanned label.

@DartBot
Copy link
Author

DartBot commented Oct 18, 2014

This comment was originally written by @seaneagan


Hmm, that seems over-specified, but the spec is the spec.

However, the native data structure is the YamlMap, YamlMap.nodes is just an AST attached to it. In fact, YamlMap.nodes already exposes the key order via the SourceSpans attached to the YamlNodes representing the keys and values:

var orderedKeys = yamlMap.nodes.keys.toList()..sort((a, b) => a.span.compareTo(b.span));

... it's just not very convenient. How about only preserving the order in YamlMap.nodes and not YamlMap? That would solve my use case, which is issue #20998.

@DartBot DartBot added Type-Enhancement area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. closed-not-planned Closed as we don't intend to take action on the reported issue labels Oct 18, 2014
@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

This issue has been moved to dart-lang/tools#1885.

@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed priority-unassigned labels Mar 1, 2016
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-pkg Used for miscellaneous pkg/ packages not associated with specific area- teams. closed-not-planned Closed as we don't intend to take action on the reported issue type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants