Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override YAML subkey

Tags:

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 
like image 754
Nate Avatar asked Feb 02 '11 13:02

Nate


1 Answers

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.

like image 100
Jason Clark Avatar answered Sep 20 '22 00:09

Jason Clark