Open Tyil opened 5 years ago
It sounds reasonable, maybe it should even be the default.
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.
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.
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.
@Tyil I will implement that for perl5 YAML::PP with Tie::IxHash. Is there something similar in perl 6?
Any updates? This is still an issue (which I've just stumbled across trying to version control generated yaml output from this module).
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.
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)
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.
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.
Perfect. Everything worked as expected now, including 'zef upgrade YAMLish' to get the latest updates. Thanks.
Ready to close?
When I use
save-yaml
on a variable, the output differs each time, as the keys are iterated over in a random order. InJSON::Fast
I introduced:sorted-keys
to resolve the issue, but I don't see an easy way to go about it inYAMLish
, so I can't bring in a PR to fix it. Can a similar feature be introduced inYAMLish
?