覆盖 YAML 子项

发布于 2024-10-15 16:26:58 字数 718 浏览 3 评论 0原文

假设我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2024-10-22 16:26:58

如果我正确理解了这个问题,我认为规范不支持覆盖锚定节点的元素。

在阅读 spec (版本 1.2,但 1.1 的说法相同)时,< a href="http://www.yaml.org/spec/1.2/spec.html#id2786196" rel="noreferrer">7.1 别名节点 指出(强调我的):

先前序列化的节点的后续出现将显示为别名节点。第一次出现的节点必须用锚标记,以允许后续出现的节点显示为别名节点。

别名节点由“*”指示符表示。别名是指具有相同锚点的最近的前一个节点。别名节点使用文档中以前未出现过的锚点是错误的。指定一个未被任何别名节点使用的锚点并不是错误。

请注意,别名节点不得指定任何属性或内容,因为这些属性或内容已在该节点第一次出现时指定。

这里有两点:

  1. “先前序列化的节点” - 此措辞表明别名旨在表示原始节点的另一个出现,而不仅仅是原始节点中的数据。换句话说,它代表同一个对象,而不是副本。

  2. 如果别名不能包含任何内容(第二个粗体部分),则您无法按照问题中建议的方式指定覆盖。

所以我对规范的解释是,你不能根据规范来做到这一点。

但是 - 如果您将原始示例(第二个代码块)粘贴到此在线工具中(您可能想要取消选中“规范”),该工具将其解释为问题中的预期,复制原始内容但覆盖 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):

Subsequent occurrences of a previously serialized node are presented as alias nodes. The first occurrence of the node must be marked by an anchor to allow subsequent occurrences to be presented as alias nodes.

An alias node is denoted by the “*” indicator. The alias refers to the most recent preceding node having the same anchor. It is an error for an alias node to use an anchor that does not previously occur in the document. It is not an error to specify an anchor that is not used by any alias node.

Note that an alias node must not specify any properties or content, as these were already specified at the first occurrence of the node.

Two points here:

  1. "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.

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文