Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override an array to be empty in YAML

Tags:

yaml

yaml-cpp

Say I have a YML file original.yml with an array of objects

array_in_yml:
- start: 1
- middle: 2
- end: 3

I included it in modified.yml

!include "original.yml" 
array_in_yml: []

I am expecting this array to be empty when I load the modified.yml, but it seems to have 3 values as original.yml. How do I force/override the array to be empty?

like image 993
mkporkodi Avatar asked Sep 11 '25 23:09

mkporkodi


1 Answers

The discussion about !include seems to lead a bit away from the actual question. Let's assume that in some unknown way, the !include line gets replaced with the content in original.yml. We would have:

array_in_yml:
- start: 1
- middle: 2
- end: 3
array_in_yml: []

This is not valid YAML, since every key in a dictionary must be unique, but you use the key array_in_yml twice. Your YAML processor might ignore this and simply assign the first value (which is a sequence of three items) to the key array_in_yml.

Now the important part: There is no way in YAML to modify previously given values. You cannot override a value given previously with a different one. What you want to do is outside the YAML spec and you would need some merging tool that does such replacements for you.

like image 187
flyx Avatar answered Sep 13 '25 19:09

flyx