YAML reusable variables with case-specific values

青春壹個敷衍的年華 提交于 2019-12-11 03:49:23

问题


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

回答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.



来源:https://stackoverflow.com/questions/30905103/yaml-reusable-variables-with-case-specific-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!