2

Is it possible in .yml config to have dynamic properties in variables that are set depending on a particular case. For example:

MY_VAR: &MY_VAR
  keys:
   key2: blahblahblah
   key3: blahblahblah  # only apply this for section2, not section1

section1: 
 var: *MY_VAR

section2:  # this case needs key3 set, otherwise everything else is the same 
 var: *MY_VAR
dreftymac
  • 31,404
  • 26
  • 119
  • 182
trace
  • 361
  • 1
  • 4
  • 9

1 Answers1

1

YAML's anchors (&MY_VAR) and references (*MY_VAR) are in the specification to prevent duplication, but also to allow serialisation of objects that occur multiple times in a hierarchy, and to allow them to be deserialized so that they again point to the same structure in memory.

This is not some string level macro facility with parameters and/or conditions. In your example, if you set MY_VAR->key1 you also change the value of section1->var->key1

Of course an application can interpret the values it loads (e.g. on complex strings that form scalar for the key in a mapping), but for that there is no facility in the YAML specification. That has to be (and can be) done at the application level.

Anthon
  • 69,918
  • 32
  • 186
  • 246
  • I understand that section1.var maps to MY_VAR & would change if MY_VAR changes. I saw an example of ruby where it looks like one can update properties in a shared variable, per case (https://gist.github.com/bowsersenior/979804). Was wondering if this was possible as a yml feature or with nodejs. – trace Jun 18 '15 at 12:30
  • @trace not if you stay within YAML. your application that reads the YAML files needs to do this. I have done so by patchiing my ruamel.yaml parser (python) but it is still the program that loads the parser doing so. Although it looks transparent to the rest of the program, it is **not** a feature of YAML and not portable. – Anthon Jun 18 '15 at 12:35