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

前端 未结 5 2186
猫巷女王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:31

    As the previous answers have pointed out, there is no built-in support for extending lists in YAML. I am offering yet another way to implement it yourself. Consider this:

    defaults: &defaults
      sites:
        - www.foo.com
        - www.bar.com
    
    setup1:
      <<: *defaults
      sites+:
        - www.baz.com
    

    This will be processed into:

    defaults:
      sites:
        - www.foo.com
        - www.bar.com
    
    setup1:
      sites:
        - www.foo.com
        - www.bar.com
        - www.baz.com
    

    The idea is to merge the contents of a key ending with a '+' to the corresponding key without a '+'. I implemented this in Python and published here.

    Enjoy!

提交回复
热议问题