Fix precedence of merge anchor sequence for traverse (explode was already correct)

This commit is contained in:
Steven WdV 2025-06-16 13:36:42 +02:00
parent 31628e7324
commit 4d88d51b1b
No known key found for this signature in database
3 changed files with 16 additions and 14 deletions

View File

@ -399,7 +399,7 @@ foobar_thing
``` ```
## Traversing merge anchor lists ## Traversing merge anchor lists
Note that the later merge anchors override previous Note that the keys earlier in the merge anchors sequence override later ones
Given a sample.yml file of: Given a sample.yml file of:
```yaml ```yaml
@ -428,7 +428,7 @@ yq '.foobarList.thing' sample.yml
``` ```
will output will output
```yaml ```yaml
bar_thing foo_thing
``` ```
## Splatting merge anchor lists ## Splatting merge anchor lists
@ -460,9 +460,9 @@ yq '.foobarList[]' sample.yml
will output will output
```yaml ```yaml
bar_b bar_b
foo_a foo_thing
bar_thing
foobarList_c foobarList_c
foo_a
``` ```
## Select multiple indices ## Select multiple indices

View File

@ -292,15 +292,17 @@ func doTraverseMap(newMatches *orderedmap.OrderedMap, node *CandidateNode, wante
return nil return nil
} }
func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, value *CandidateNode, wantedKey string, prefs traversePreferences, splat bool) error { func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, merge *CandidateNode, wantedKey string, prefs traversePreferences, splat bool) error {
if value.Kind == AliasNode { if merge.Kind == AliasNode {
value = value.Alias merge = merge.Alias
} }
switch value.Kind { switch merge.Kind {
case MappingNode: case MappingNode:
return doTraverseMap(newMatches, value, wantedKey, prefs, splat) return doTraverseMap(newMatches, merge, wantedKey, prefs, splat)
case SequenceNode: case SequenceNode:
for _, childValue := range value.Content { // Earlier keys take precedence
for index := len(merge.Content) - 1; index >= 0; index = index - 1 {
childValue := merge.Content[index]
if childValue.Kind == AliasNode { if childValue.Kind == AliasNode {
childValue = childValue.Alias childValue = childValue.Alias
} }

View File

@ -449,11 +449,11 @@ var traversePathOperatorScenarios = []expressionScenario{
}, },
{ {
description: "Traversing merge anchor lists", description: "Traversing merge anchor lists",
subdescription: "Note that the later merge anchors override previous", subdescription: "Note that the keys earlier in the merge anchors sequence override later ones",
document: mergeDocSample, document: mergeDocSample,
expression: `.foobarList.thing`, expression: `.foobarList.thing`,
expected: []string{ expected: []string{
"D0, P[bar thing], (!!str)::bar_thing\n", "D0, P[foo thing], (!!str)::foo_thing\n",
}, },
}, },
{ {
@ -478,9 +478,9 @@ var traversePathOperatorScenarios = []expressionScenario{
expression: `.foobarList[]`, expression: `.foobarList[]`,
expected: []string{ expected: []string{
"D0, P[bar b], (!!str)::bar_b\n", "D0, P[bar b], (!!str)::bar_b\n",
"D0, P[foo a], (!!str)::foo_a\n", "D0, P[foo thing], (!!str)::foo_thing\n",
"D0, P[bar thing], (!!str)::bar_thing\n",
"D0, P[foobarList c], (!!str)::foobarList_c\n", "D0, P[foobarList c], (!!str)::foobarList_c\n",
"D0, P[foo a], (!!str)::foo_a\n",
}, },
}, },
{ {