Adding support for handling YAML Merge Key (#41) - proper keys comparison for merging#1279
Adding support for handling YAML Merge Key (#41) - proper keys comparison for merging#1279azat wants to merge 7 commits intojbeder:masterfrom
Conversation
Support for YAML Merge keys ( <<: [*dict1, *dict2] ) is added. The merge key is a specific scalar with value << (and tag !!merge) that implies that during node construction, the map (or sequence of maps) are merged into the current map. The priority rules are that each key from maps within the value associated with << are added iff the key is not yet present in the current map (and first map gets higher priority). Test cases have been added accordingly.
…the node) The problem is that const_map_to.get(*j->first) compares only the shared_ptr's, while we need to compare the key itself. v2: remove const iterator v3: fix use-after-free due to const reference
Consider the following YAML:
trait1: &t1
foo: 1
trait2: &t2
foo: 2
merged:
<<: *t1
<<: *t2
yq reports:
$ yq .merged.foo < /tmp/yaml
2
while the order that yaml-cpp returns is different, since it will
firstly handle 1, and will not replace it with 2:
$ util/parse < /tmp/yaml
trait1:
? &1 foo
: &2 1
trait2:
foo: 2
merged:
*1 : *2
(Don't mix up "*2" with "2", it is trait1)
SGSSGene
left a comment
There was a problem hiding this comment.
Requested Change:
MergeMapCollectionmust bestaticor in an anonymous namespace.
I think, the R-Value (&&)/std::move suggestion is a matter of taste. This library is not very strong on move semantics anyways.
Co-authored-by: Simon Gene Gottlieb <simon@gottliebtfreitag.de>
|
I've always been on the fence about this for two reasons:
What do you think? |
All very good questions. I have to admit I have merged #1221 which has the same issue of defining what "equality" is.
While incorrect, I think these are edge-cases and should raise serious concerns if relied on. @"Merge Keys": For some reason I didn't realize that the Merge Key proposal is just a working draft. I am assuming that @azat and the people giving " 👍" have a use case for it. @"UniqueKeys" or "ComparableKeys" I think we should change it to be more standard conforming (not part of this PR). Maybe a comparision that only relies on the Failsafe Schema. |
NOTE: this is based on #1243, but includes a fix for proper merging (by comparing the key itself, instead of shared_ptrs, see the last patch).
Support for YAML Merge keys ( <<: [*dict1, *dict2] ) is added. The merge
key is a specific scalar with value << (and tag !!merge) that implies
that during node construction, the map (or sequence of maps) are merged
into the current map. The priority rules are that each key from maps
within the value associated with << are added iff the key is not yet
present in the current map (and first map gets higher priority). Test
cases have been added accordingly.