我可以在YAML中连接别名吗?

时间:2016-09-27 17:42:17

标签: yaml

我想做点什么:

opt-flags   : &opt_flags -DCMAKE_BUILD_TYPE=Release
dbg-flags   : &dbg_flags -DCMAKE_BUILD_TYPE=Debug
common-flags: &common    -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON 

# concatenate previous definitions to create composed definitions
dbg: *common *dbg_flags   
opt: *common *opt_flags

这不起作用。是否有可能在YAML中做同样的事情?

1 个答案:

答案 0 :(得分:0)

不,你不能这样做,别名取代了一个完整的节点。

但是,如果您正在处理映射,如果您的解析器支持它,则可以使用merge key language-independent type将多组密钥组合到一个新映射中:

opt-flags   : &opt_flags -DCMAKE_BUILD_TYPE=Release
dbg-flags   : &dbg_flags -DCMAKE_BUILD_TYPE=Debug
common-flags: &common    -DENABLE_EXAMPLES=ON -DENABLE_TESTS=ON 

dbg:
  << : [*common_flags, *dbg_flags]
opt:
  << : [*common_flags, *opt_flags]

然而,这将分别生成两个条目,而不是连接锚定的字符串标量,并且需要一个可以组合多个值的程序,不能保证排序