具有特定于案例的值的 YAML 可重用变量 [英] YAML reusable variables with case-specific values
问题描述
在 .yml
配置中是否有可能在根据特定情况设置的变量中具有动态属性.例如:
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
推荐答案
YAML 的锚点 (&MY_VAR
) 和引用 (*MY_VAR
) 在规范中是为了防止重复,但也允许序列化在层次结构中多次出现的对象,并允许它们反序列化,以便它们再次指向内存中的相同结构.
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.
这不是一些带有参数和/或条件的字符串级宏工具.在您的示例中,如果您设置 MY_VAR->key1
,您还会更改 section1->var->key1
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
当然,应用程序可以解释它加载的值(例如,在为映射中的键形成标量的复杂字符串上),但 YAML 规范中没有为此提供便利.这必须(并且可以)在应用程序级别完成.
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.
这篇关于具有特定于案例的值的 YAML 可重用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!