[英]YAML 1.2 anchors not working in 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: 当加载YAML锚使用Node.js的中(v4.8.7) JS-YAML 3.10.0包(有使用它的一个很好的例子在这里 )我得到以下错误:
"cannot merge mappings; the provided source object is unacceptable"
“无法合并映射;提供的源对象是不可接受的”
For instance in my input yaml file I have something like the following as my anchor: 例如,在我的输入yaml文件中,我有类似以下内容作为我的锚点:
defaultEd: &defaultEd
- 'Pennsylvania College of Technology AS'
- 'Pennsylvania College of Technology BS'
And where the anchor is referenced in my input yaml file I have the following: 在我的输入yaml文件中引用锚点的地方我有以下内容:
...
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'
The error is displayed something like this: 错误显示如下:
{ [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 <<
. 您可能误解/混淆了别名和合并键
<<
。
Any node in YAML can have an anchor attached: YAML中的任何节点都可以附加锚点:
---
mapping: &map
a: 1
b: 2
sequence: &seq
- a
- b
scalar: &scalar foo
mapping-alias: *map
sequence-alias: *seq
scalar-alias: *scalar
The merge key <<
, which is not part of the YAML spec itself, is supported by some processors. 某些处理器支持合并键
<<
,它不是YAML规范本身的一部分。 It lets you merge an aliased mapping into another mapping. 它允许您将别名映射合并到另一个映射中。
defaults: &defaults
a: 1
b: 2
# .....
some-mapping:
<<: *defaults
c: 3
See http://yaml.org/type/merge.html 见http://yaml.org/type/merge.html
A mapping in YAML is usually called a dictionary, hash or sometimes object (Javascript) in programming languages. YAML中的映射通常在编程语言中称为字典,散列或有时对象(Javascript)。
In your case, you probably just want: 在您的情况下,您可能只是想:
education: *defaultEd
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.