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

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

Closed
DartBot opened this issue Jun 5, 2015 · 4 comments
Closed
Labels
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 Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/444270?v=3" align="left" width="96" height="96"hspace="10"> Issue by seaneagan
Originally opened as dart-lang/sdk#21328


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 Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/444270?v=3" align="left" width="48" height="48"hspace="10"> Comment by seaneagan


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

@DartBot DartBot added type-enhancement A request for a change that isn't a bug NotPlanned labels Jun 5, 2015
@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/4865287?v=3" align="left" width="48" height="48"hspace="10"> Comment by lrhn


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

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> Comment by nex3


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 Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/444270?v=3" align="left" width="48" height="48"hspace="10"> Comment 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 #4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

1 participant