问题描述
YAML 中的 <<:
运算符可用于将一个映射的内容导入到另一个映射中,类似于 Python 中的 **
双splat 运算符或...
JavaScript 中的对象解构运算符.例如,
foo:甲:乙<<:丙:丁e: f
相当于
foo:甲:乙丙:丁e: f
这在与 节点锚点一起使用时非常有用,可以包含一些许多对象中的常见默认属性,例如,在 Y 分钟内学习 YAML 教程中说明:
# 锚可用于复制/继承属性基地:&基地姓名:每个人都有相同的名字foo: & foo<<:*基数年龄:10酒吧:&酒吧<<:*基数年龄:20
但是,我对这种语法的来源或它的工作原理感到困惑.+YAML 规范 for <<
表明它没有出现在规范中的任何地方.然而,它至少得到了 PyYAML 和 http://yaml-online-parser.appspot.com/.
这是什么语法,为什么它似乎没有出现在规范中?
它被称为 YAML 1.1 版的 Merge Key Language-Independent Type.并指定这里
这是解析器可以选择支持的东西,它本质上是对带有特殊键 <<
的键值对的解释,其中值是一个映射(通常通过规范中的别名,虽然这似乎不是必需的,但不使用别名)或映射列表(即映射的别名)没有多大意义,并以特殊方式进行解释.
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:
However, I am confused about where this syntax comes from or why it works. +ing 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?
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.
这篇关于什么是<<调用 YAML 中的(双左箭头)语法,它在哪里指定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!