Improved consistency of map key sorting. #439
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There are some specific sets of keys that will be sorted inconsistently when marshaling. This can result in YAML documents that change significantly when re-marshaled, even if the underlying data remains identical.
I noticed this when marshaling a cache object (vanilla
map[string]string
) as part of a CI pipeline. The resulting (git
) diff kept thrashing even if the cache data didn't.As a minimal repro, I was able to find a set of keys that trigger this behavior. These keys do not have a strict ordering, as
z01 < z12
,z12 < z1a
, andz1a < z01
, creating a cycle.The changes in this PR removes this inconsistency, while maintaining the same ordering behavior asserted in the existing test suite.
Some specific feedback that I think this PR needs:
yaml
) package contents? - All current tests exist in an external (yaml_test
) package, and cannot assert this behavior directly.v2
(orv3
, etc)? - All current testsimport "gopkg.in/yaml.v2"
which would make new tests fail as that package doesn't yet contain the corresponding new fixes.Looking forward to hearing your feedback, cheers!