YAML 1.2锚点在js-yaml 3.10.0中不起作用? [英] YAML 1.2 anchors not working in js-yaml 3.10.0?
问题描述
使用 YAML锚点时=https://www.npmjs.com/package/js-yaml =nofollow noreferrer> js-yaml 3.10.0 包(这是一个很好的使用它的例子这里)我收到以下错误:
When loading YAML anchors in node.js (v4.8.7) using the js-yaml 3.10.0 package (there's a nice example of using it here) I get the following error:
无法合并映射;提供的源对象是不可接受的
"cannot merge mappings; the provided source object is unacceptable"
例如在我输入的yaml文件中,我有类似以下内容作为我的锚:
For instance in my input yaml file I have something like the following as my anchor:
defaultEd: &defaultEd
- 'Pennsylvania College of Technology AS'
- 'Pennsylvania College of Technology BS'
在输入yaml文件中引用锚点的地方我有以下内容:
And where the anchor is referenced in my input yaml file I have the following:
...
education:
<<: *defaultEd
qs:
- 'Reading'
- 'Writing'
...
我希望在我的输出中完成以下内容:
I'm hoping to accomplish the following in my output:
education:
- 'Pennsylvania College of Technology AS'
- 'Pennsylvania College of Technology BS'
错误显示如下:
{ [YAMLException: cannot merge mappings; the provided source object is unacceptable at line 21, column 1:
qs:
^]
name: 'YAMLException',
reason: 'cannot merge mappings; the provided source object is unacceptable',
mark:
Mark {
name: null,
buffer: 'defaultEd: &defaultEd\n - \'Pennsylvania College of Technology AS\'\n - \'Pennsylvania College of Technology BS..
<<: *defaultEd\nqs:\n - \'Reading\'\n - \'Writing\'\n - \'Rithmatick\'\nexperience:\n - {posName: \'Database Analyst / Net Tech\', companyName: \'Choices People Supporting People\'}\n\u0000',
position: 435,
line: 20,
column: 0 },
message: 'cannot merge mappings; the provided source object is unacceptable at line 21, column 1:\n qs:\n ^' }
Error View file does not exist: someTest.yml
推荐答案
您可能误解/混淆了别名和合并键的使用<< ;
。
You probably misunderstood / mixed up the usage of aliases and the merge key <<
.
YAML中的任何节点都可以附加锚点:
Any node in YAML can have an anchor attached:
---
mapping: &map
a: 1
b: 2
sequence: &seq
- a
- b
scalar: &scalar foo
mapping-alias: *map
sequence-alias: *seq
scalar-alias: *scalar
合并密钥<<
,这不是YAML的一部分规范本身,由一些处理器支持。它允许您将别名映射合并到另一个映射中。
The merge key <<
, which is not part of the YAML spec itself, is supported by some processors. It lets you merge an aliased mapping into another mapping.
defaults: &defaults
a: 1
b: 2
# .....
some-mapping:
<<: *defaults
c: 3
请参阅 http://yaml.org /type/merge.html
YAML中的映射通常在编程语言中称为字典,散列或有时是对象(Javascript)。
A mapping in YAML is usually called a dictionary, hash or sometimes object (Javascript) in programming languages.
在您的情况下,您可能只想:
In your case, you probably just want:
education: *defaultEd
这篇关于YAML 1.2锚点在js-yaml 3.10.0中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!