什么是<<YAML 中的(双左箭头)语法被调用,它在哪里指定? [英] What is the << (double left arrow) syntax in YAML called, and where's it specced?

查看:24
本文介绍了什么是<<YAML 中的(双左箭头)语法被调用,它在哪里指定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

YAML 中的 <<: 运算符可用于将一个映射的内容导入到另一个映射中,类似于 Python 中的 ** double-splat 运算符或... JavaScript 中的对象解构运算符.例如,

foo:甲:乙<<:c: de: f

等价于

foo:甲:乙c: de: f

这在与 节点锚 一起使用时很有用,以包含一些许多对象中的通用默认属性,例如,在 Y 分钟内学习 YAML 教程中所示:

<块引用>

# Anchors 可用于复制/继承属性基地:&基地姓名:每个人都有相同的名字富:&富<<: *基础年龄:10酒吧:&酒吧<<: *基础年龄:20

但是,我对这种语法的来源或它的工作原理感到困惑.CTRL+Fing YAML 规范 for << 表明它没有出现在规范的任何地方.然而,至少 PyYAMLhttp://yaml-online-parser.appspot.com/.

这是什么语法,为什么它似乎没有出现在规范中?

解决方案

YAML 1.1 版本称为 Merge Key Language-Independent Type.并指定 here

这是解析器可以选择支持的东西,它本质上是具有特殊键 << 的键值对的解释,其中值是映射(通常通过规范中的别名,虽然这似乎不是必需的,但不使用别名)或映射列表(即映射的别名)几乎没有意义,并以特殊方式解释.

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?

解决方案

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.

这篇关于什么是&lt;&lt;YAML 中的(双左箭头)语法被调用,它在哪里指定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆