Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

yaml

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

foo:   a: b   <<:     c: d   e: f 

is equivalent to

foo:   a: b   c: d   e: f 

This is useful when used along with node anchors to include some common default properties in many objects, as illustrated in, for example, the Learn YAML in Y minutes tutorial:

# Anchors can be used to duplicate/inherit properties base: &base     name: Everyone has same name  foo: &foo     <<: *base     age: 10  bar: &bar     <<: *base     age: 20 

However, I am confused about where this syntax comes from or why it works. CTRL+Fing the YAML spec for << reveals that it doesn't appear anywhere in the spec. Yet it's supported by, at the very least, PyYAML and http://yaml-online-parser.appspot.com/.

What is this syntax, and how come it doesn't seem to appear in the spec?

like image 348
Mark Amery Avatar asked Dec 09 '16 15:12

Mark Amery


People also ask

What is << In YAML?

The “<<” merge key is used to indicate that all the keys of one or more specified maps should be inserted into the current map. If the value associated with the key is a single mapping node, each of its key/value pairs is inserted into the current mapping, unless the key already exists in it.

What is YAML syntax?

YAML syntaxYAML has features that come from Perl, C, XML, HTML, and other programming languages. YAML is also a superset of JSON, so JSON files are valid in YAML. YAML uses Python-style indentation to indicate nesting. Tab characters are not allowed, so whitespaces are used instead.

How do I use special characters in YAML?

YAML doesn't require quoting most strings but you'll want to quote special characters if they fall within a certain list of characters. Use quotes if your value includes any of the following special characters: { , } , [ , ] , & , * , # , ? , | , - , < , > , = , ! , % , @ , : also ` and , YAML Spec.

What are directives in YAML?

YAML Directive YAML Directives are default directives. If converted in JSON, the value fetched includes forward slash character in preceding and terminating characters.


1 Answers

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.

like image 118
Anthon Avatar answered Oct 11 '22 12:10

Anthon