具有特定于案例值的 YAML 可重用变量 [英] YAML reusable variables with case-specific values

查看:12
本文介绍了具有特定于案例值的 YAML 可重用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆