覆盖 YAML 子项
假设我有以下 YAML 文件:
-
key1: value
# and so on...
key99: value
key100:
subkey1: value
# and so on...
subkey100: value
-
key1: value
# and so on...
key99: value
key100:
subkey1: value
# and so on...
subkey100: SOME DIFFERENT VALUE
处理大量相同数据的自然方法是使用锚点,并仅覆盖更改的键。
问题是,这里的子项是不同的。有没有一种简单的方法可以仅引用 key100['subkey100']
更改?或者我必须在每个级别使用一系列锚点?
即,是否有以下缩写:
- &anchor
key1: value
# and so on...
key99: value
key100: &subanchor
subkey1: value
# and so on...
subkey100: value
-
<<: *anchor
key100:
<<: *subanchor
subkey100: SOME DIFFERENT VALUE
say I have the following YAML file:
-
key1: value
# and so on...
key99: value
key100:
subkey1: value
# and so on...
subkey100: value
-
key1: value
# and so on...
key99: value
key100:
subkey1: value
# and so on...
subkey100: SOME DIFFERENT VALUE
The natural way to handle a large amount of identical data would be with anchors, and overriding just the key that changed.
The question is, here, a subkey is different. Is there an easy way to just reference that key100['subkey100']
changed? Or do I have to use a series of anchors at each level?
I.e., is there a shorthand for:
- &anchor
key1: value
# and so on...
key99: value
key100: &subanchor
subkey1: value
# and so on...
subkey100: value
-
<<: *anchor
key100:
<<: *subanchor
subkey100: SOME DIFFERENT VALUE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我正确理解了这个问题,我认为规范不支持覆盖锚定节点的元素。
在阅读 spec (版本 1.2,但 1.1 的说法相同)时,< a href="http://www.yaml.org/spec/1.2/spec.html#id2786196" rel="noreferrer">7.1 别名节点 指出(强调我的):
这里有两点:
“先前序列化的节点” - 此措辞表明别名旨在表示原始节点的另一个出现,而不仅仅是原始节点中的数据。换句话说,它代表同一个对象,而不是副本。
如果别名不能包含任何内容(第二个粗体部分),则您无法按照问题中建议的方式指定覆盖。
所以我对规范的解释是,你不能根据规范来做到这一点。
但是 - 如果您将原始示例(第二个代码块)粘贴到此在线工具中(您可能想要取消选中“规范”),该工具将其解释为问题中的预期,复制原始内容但覆盖 subkey100。对于这个 YAML Lint 工具 来说,与 这个在线解析器。
所以它似乎在实践中有效,但我在规范中找不到对它的支持。
If I've understood the question correctly, I don't think the spec supports overriding elements of anchored nodes.
On reading the spec (version 1.2, but 1.1 says the same), section 7.1 Alias Nodes states (emphasis mine):
Two points here:
"Previously serialized node" - this wording suggests that the alias is meant to represent another occurrence of the original node, not just the data in the original node. In other words, it represents the same object, not a copy.
If an alias cannot have any content (second bold section), then you cannot specify the override in the fashion suggested in the question.
So my interpretation of the spec is that you cannot do this according to the spec.
However - If you paste the example (second code block) from the original into this online tool(you may want to uncheck 'canonical'), that tool interprets it as intended in the question, copying the original content but overriding subkey100. Same for this YAML Lint Tool, as does this online parser.
So it seems to work in practice, but I can't find support for it within the spec.