user3416593 user3416593 - 5 days ago 4x
YAML Question

Can I concatenate aliases in YAML?

I would like to do something like:

opt-flags : &opt_flags -DCMAKE_BUILD_TYPE=Release
dbg-flags : &dbg_flags -DCMAKE_BUILD_TYPE=Debug

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

This doesn't work directly. Is it possible to do something equivalent to this in YAML?


No you cannot do that, an alias replaces a complete node.

However if you are dealing with mappings, you can use the merge key language-independent type if your parser supports it to combine multiple sets of keys into a new mapping:

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 

  << : [&common_flags, &dbg_flags]
  << : [&common_flags, &opt_flags]

This however will make two entries each, and not concatenate the strings scalars that are anchored, and will need a program that can combine the multiple values, for which the ordering is not guaranteed.