What is the << (double left arrow) syntax in YAML called, and where's it specced?

后端 未结 2 634
耶瑟儿~
耶瑟儿~ 2020-12-28 11:59

The <<: operator in YAML is usable to import the contents of one mapping into another, similarly to the ** double-splat operator in Python or

相关标签:
2条回答
  • 2020-12-28 12:20

    To add on to other answers:

    IMO, the example from "Learn yaml in Y Minutes" is incomplete because it doesn't show what happens when the keys are the same. For example:

    base: &base
        name: Everyone has same name
        age: 5
    
    foo: &foo
        <<: *base
    
    bar: &bar
        <<: *base
        age: 20
    

    For the bottom two items yields:

    foo: 
        name: Everyone has same name
        age: 5
    
    bar:
        name: Everyone has same name
        age: 20
    

    bar overrides the age while foo does not. According to the spec the entries of the object merging in have a lower priority than those on the object receiving them.

    0 讨论(0)
  • 2020-12-28 12:24

    It is called the Merge Key Language-Independent Type for YAML version 1.1. and specced here

    It is something that parsers can optionally support, it is essentially an interpretation of the key-value pair with the special key <<, where the value is either a mapping (usually indicated via an alias as in the spec, and although that doesn't seem to be required, it makes little sense not to use an alias) or a list of mappings (i.e. aliases of mappings), and gets interpreted in a special way.

    0 讨论(0)
提交回复
热议问题