Last Updated: May 25, 2018
·
38.94K
· pleone

Using pointers to merge elements in yaml file

Yaml example:

pasta:
  - pomodoro
  - sugo

hamburgher:
  - spicy
  - onion 

I want a new element called ingredients to be the merge of the elements of pasta and hamburgher.
Here's the magic:

pasta: &one
  - pomodoro
  - sugo  
hamburgher: &two
  - spicy
  - onion   
ingredients:
  << : [*one, *two]

This link helped me:
http://stackoverflow.com/questions/9254178/is-there-yaml-syntax-for-sharing-part-of-a-list-or-map

1 Response
Add your response

It doesn't work in PyYaml:
ConstructorError: while constructing a mapping
in "<string>", line 9, column 3:
<< : [*one, *two]
^
expected a mapping for merging, but found sequence
in "<string>", line 2, column 8:
pasta: &one
^
Even the specs (http://yaml.org/type/merge.html) say that it works only for mappings. I have no idea why it doesn't apply to sequences, it would be very useful indeed

over 1 year ago ·