Leont / yamlish

An implementation of YAML in Raku
Artistic License 2.0
21 stars 18 forks source link

Order keys when saving yaml #28

Open Tyil opened 5 years ago

Tyil commented 5 years ago

When I use save-yaml on a variable, the output differs each time, as the keys are iterated over in a random order. In JSON::Fast I introduced :sorted-keys to resolve the issue, but I don't see an easy way to go about it in YAMLish, so I can't bring in a PR to fix it. Can a similar feature be introduced in YAMLish?

Leont commented 5 years ago

It sounds reasonable, maybe it should even be the default.

Tyil commented 5 years ago

I have a possible solution for this, which introduces a :sorted-keys argument to save-yaml. When set to False, keys will not be sorted, otherwise they will be. I can also remove the :sorted-keys argument and make everything sorted at all times.

perlpunk commented 5 years ago

I think sorting by default is reasonable. All perl5 YAML modules do this. If a language supports remembering key order from loading, like for example Python 3.7, then it's different and not sorting should rather be the default.

Tyil commented 5 years ago

if YAMLish could support a way to remember key order from a loaded file that would be even better for my usecase, but I'm going to assume that that will take a lot of effort to get in there.

perlpunk commented 5 years ago

@Tyil I will implement that for perl5 YAML::PP with Tie::IxHash. Is there something similar in perl 6?

Digicrat commented 3 years ago

Any updates? This is still an issue (which I've just stumbled across trying to version control generated yaml output from this module).

Leont commented 3 years ago

Any updates? This is still an issue (which I've just stumbled across trying to version control generated yaml output from this module).

I've just pushed a fix for this.

Leont commented 3 years ago

I will implement that for perl5 YAML::PP with Tie::IxHash. Is there something similar in perl 6?

If such a module existed this would be easy enough to add (it's already possible to override what it does for mapping the various yaml types to raku types)

Digicrat commented 3 years ago

Thanks. That fixes the sorting for the top-level keys, but it isn't working for nested keys (ie: an array of hashes). I tried extending the patch myself, but my naive attempt inadvertently led to it sorting the arrays to :-x

FYI, you also need to bump the version number in the META.json so zef will pick up that there's updates to be installed.

Leont commented 3 years ago

Thanks. That fixes the sorting for the top-level keys, but it isn't working for nested keys (ie: an array of hashes). I tried extending the patch myself, but my naive attempt inadvertently led to it sorting the arrays to :-x

Yeah, that's a bug. Fixed now.

FYI, you also need to bump the version number in the META.json so zef will pick up that there's updates to be installed.

Done that too.

Digicrat commented 3 years ago

Perfect. Everything worked as expected now, including 'zef upgrade YAMLish' to get the latest updates. Thanks.

jubilatious1 commented 1 year ago

Ready to close?