Reputation: 48743
It is not clear if it is possible to use YAML merge key operator several time for the same map.
The confusion comes from:
Original spec says:
The “<<” merge key is used to indicate that all the keys of one or more specified maps should be inserted into the current map.
but they don't provide an example and I cannot find it elsewhere. Like:
defs:
- map1: &map1-ref
key1: val1
- map2: &map2-ref
key2: val2
config:
database:
<<: *map1-ref
<<: *map2-ref
key3: val3
Which major parsers support multiply merges (python/ruby/java/node)?
UPDATE Python 3 check code:
import yaml
with open("my.yml") as f:
y = yaml.safe_load(f)
print(y)
UPDATE 2 @flyx's suggested syntax can be written in an alternative way:
config:
database:
<<:
- *map1-ref
- *map2-ref
key3: val3
Upvotes: 9
Views: 15910
Reputation: 39638
While a quick test shows that PyYAML does support this, I would advise against it since having multiple identical keys (<<
) in the same mapping violates the core YAML spec (both 1.1 and 1.2).
Moreover, it is unnecessary since the merge key can take multiple maps as arguments:
defs:
- map1: &map1-ref
key1: val1
- map2: &map2-ref
key2: val2
config:
database:
<<: [*map1-ref, *map2-ref]
key3: val3
If you use this syntax, the question boils down to „which implementations support the merge key“. I can't give a full list but here's what I know:
YAML::PP
Upvotes: 20