If the value associated with the key is a single mapping node, each of its key/value pairs is
inserted into the current mapping, unless the key already exists in it.
It's not clear what "already exists" mean. If you look at the last item in the example, the x key is overriding the one from the merge key, although at the time of the parsing/loading of the merge key it does not yet exist.
Also the spec doesn't say what happens if a merge key comes after other keys, or if two merge keys are allowed at all:
<< : *alias1
key1: value
<< : *alias2
That means that a post-processing of the mapping is necessary.
Also a merge key is very different from all other tag definitions, because the usual tags target the node itself.
A merge key like the following (explicitly marked with !!merge to make it clear
!!merge << : *alias
actually alters the behaviour of the surrounding mapping, especially since post-processing is necessary.
That means that it is more work to implement it. While all other tags can be implemented generically, we need extra code to make this work.
Also, implementations differ slightly.
The merge key has to be a plain scalar, without quotes. but js-yaml also reads "<<" as a merge key, preventing people from using a literal << as a key.
Ruby's implementation differs slightly if you change the order of keys, so that the merge key comes after other keys.
You can only merge mappings
There is no smilar thing for merging lists, although that could also be useful in some situations.
Why do we still want merge keys then?
First of all, they are implemented in PyYAML, ruamel, SnakeYAML, js-yaml, Ruby psych and others, and people are using them often. They are useful after all.
I think people using YAML in perl should be able to use them too.
What is a merge key?
https://yaml.org/type/merge.html
Problems
The specification of merge keys is incomplete
It's not clear what "already exists" mean. If you look at the last item in the example, the
x
key is overriding the one from the merge key, although at the time of the parsing/loading of the merge key it does not yet exist. Also the spec doesn't say what happens if a merge key comes after other keys, or if two merge keys are allowed at all:That means that a post-processing of the mapping is necessary. Also a merge key is very different from all other tag definitions, because the usual tags target the node itself. A merge key like the following (explicitly marked with
!!merge
to make it clearactually alters the behaviour of the surrounding mapping, especially since post-processing is necessary.
That means that it is more work to implement it. While all other tags can be implemented generically, we need extra code to make this work.
Also, implementations differ slightly. The merge key has to be a plain scalar, without quotes. but js-yaml also reads
"<<"
as a merge key, preventing people from using a literal<<
as a key. Ruby's implementation differs slightly if you change the order of keys, so that the merge key comes after other keys.You can only merge mappings
There is no smilar thing for merging lists, although that could also be useful in some situations.
Why do we still want merge keys then?
First of all, they are implemented in PyYAML, ruamel, SnakeYAML, js-yaml, Ruby psych and others, and people are using them often. They are useful after all.
I think people using YAML in perl should be able to use them too.