Is there YAML syntax for sharing part of a list or map?

前端 未结 5 2168
猫巷女王i
猫巷女王i 2020-11-29 01:07

So, I know I can do something like this:

sitelist: &sites
  - www.foo.com
  - www.bar.com

anotherlist: *sites

And have sitelist<

5条回答
  •  暖寄归人
    2020-11-29 01:21

    The merge key type is probably what you want. It uses a special << mapping key to indicate merges, allowing an alias to a mapping (or a sequence of such aliases) to be used as an initializer to merge into a single mapping. Additionally, you can still explicitly override values, or add more that weren't present in the merge list.

    It's important to note that it works with mappings, not sequences as your first example. This makes sense when you think about it, and your example looks like it probably doesn't need to be sequential anyway. Simply changing your sequence values to mapping keys should do the trick, as in the following (untested) example:

    sitelist: &sites
      ? www.foo.com  # "www.foo.com" is the key, the value is null
      ? www.bar.com
    
    anotherlist:
      << : *sites    # merge *sites into this mapping
      ? www.baz.com  # add extra stuff
    

    Some things to notice. Firstly, since << is a key, it can only be specified once per node. Secondly, when using a sequence as the value, the order is significant. This doesn't matter in the example here, since there aren't associated values, but it's worth being aware.

提交回复
热议问题