Merge anchor traversing: add test for aliased sequence, cleanup

This commit is contained in:
Steven WdV 2025-06-16 09:56:10 +02:00
parent 162ea5437c
commit c3782799c5
No known key found for this signature in database
2 changed files with 14 additions and 5 deletions

View File

@ -293,9 +293,10 @@ func doTraverseMap(newMatches *orderedmap.OrderedMap, node *CandidateNode, wante
} }
func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, value *CandidateNode, wantedKey string, prefs traversePreferences, splat bool) error { func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, value *CandidateNode, wantedKey string, prefs traversePreferences, splat bool) error {
if value.Kind == AliasNode {
value = value.Alias
}
switch value.Kind { switch value.Kind {
case AliasNode:
return traverseMergeAnchor(newMatches, value.Alias, wantedKey, prefs, splat)
case MappingNode: case MappingNode:
return doTraverseMap(newMatches, value, wantedKey, prefs, splat) return doTraverseMap(newMatches, value, wantedKey, prefs, splat)
case SequenceNode: case SequenceNode:
@ -313,10 +314,10 @@ func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, value *CandidateNode
return err return err
} }
} }
return nil
default: default:
return fmt.Errorf("can only use merge anchors with maps (!!map) or sequences (!!seq) of maps, but got %v", value.Tag) return fmt.Errorf("can only use merge anchors with maps (!!map) or sequences (!!seq) of maps, but got %v", value.Tag)
} }
return nil
} }
func traverseArray(candidate *CandidateNode, operation *Operation, prefs traversePreferences) (*list.List, error) { func traverseArray(candidate *CandidateNode, operation *Operation, prefs traversePreferences) (*list.List, error) {

View File

@ -365,7 +365,7 @@ var traversePathOperatorScenarios = []expressionScenario{
"D0, P[0], (!!null)::null\n", "D0, P[0], (!!null)::null\n",
}, },
}, },
{ { // Merge anchor with inline map
skipDoc: true, skipDoc: true,
document: `{<<: {a: 42}}`, document: `{<<: {a: 42}}`,
expression: `.a`, expression: `.a`,
@ -373,7 +373,7 @@ var traversePathOperatorScenarios = []expressionScenario{
"D0, P[<< a], (!!int)::42\n", "D0, P[<< a], (!!int)::42\n",
}, },
}, },
{ { // Merge anchor with sequence with inline map
skipDoc: true, skipDoc: true,
document: `{<<: [{a: 42}]}`, document: `{<<: [{a: 42}]}`,
expression: `.a`, expression: `.a`,
@ -381,6 +381,14 @@ var traversePathOperatorScenarios = []expressionScenario{
"D0, P[<< 0 a], (!!int)::42\n", "D0, P[<< 0 a], (!!int)::42\n",
}, },
}, },
{ // Merge anchor with aliased sequence with inline map
skipDoc: true,
document: `{s: &s [{a: 42}], m: {<<: *s}}`,
expression: `.m.a`,
expected: []string{
"D0, P[s 0 a], (!!int)::42\n",
},
},
{ {
skipDoc: true, skipDoc: true,
document: mergeDocSample, document: mergeDocSample,