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

Order keys when saving yaml #28

Open
Tyil opened this issue Apr 10, 2019 · 12 comments
Open

Order keys when saving yaml #28

Tyil opened this issue Apr 10, 2019 · 12 comments

Comments

@Tyil
Copy link

Tyil commented Apr 10, 2019

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
Copy link
Owner

Leont commented Apr 10, 2019

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

@Tyil
Copy link
Author

Tyil commented May 13, 2019

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
Copy link

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
Copy link
Author

Tyil commented May 13, 2019

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
Copy link

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

@Digicrat
Copy link

Digicrat commented Jun 8, 2021

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

@Leont
Copy link
Owner

Leont commented Jun 8, 2021

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
Copy link
Owner

Leont commented Jun 8, 2021

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
Copy link

Digicrat commented Jun 9, 2021

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
Copy link
Owner

Leont commented Jun 9, 2021

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
Copy link

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

@jubilatious1
Copy link

Ready to close?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants