diff --git a/pkg/yqlib/operator_traverse_path.go b/pkg/yqlib/operator_traverse_path.go index 0d1db100..f3afaf41 100644 --- a/pkg/yqlib/operator_traverse_path.go +++ b/pkg/yqlib/operator_traverse_path.go @@ -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 { + if value.Kind == AliasNode { + value = value.Alias + } switch value.Kind { - case AliasNode: - return traverseMergeAnchor(newMatches, value.Alias, wantedKey, prefs, splat) case MappingNode: return doTraverseMap(newMatches, value, wantedKey, prefs, splat) case SequenceNode: @@ -313,10 +314,10 @@ func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, value *CandidateNode return err } } + return nil default: 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) { diff --git a/pkg/yqlib/operator_traverse_path_test.go b/pkg/yqlib/operator_traverse_path_test.go index c81f797e..114c728e 100644 --- a/pkg/yqlib/operator_traverse_path_test.go +++ b/pkg/yqlib/operator_traverse_path_test.go @@ -365,7 +365,7 @@ var traversePathOperatorScenarios = []expressionScenario{ "D0, P[0], (!!null)::null\n", }, }, - { + { // Merge anchor with inline map skipDoc: true, document: `{<<: {a: 42}}`, expression: `.a`, @@ -373,7 +373,7 @@ var traversePathOperatorScenarios = []expressionScenario{ "D0, P[<< a], (!!int)::42\n", }, }, - { + { // Merge anchor with sequence with inline map skipDoc: true, document: `{<<: [{a: 42}]}`, expression: `.a`, @@ -381,6 +381,14 @@ var traversePathOperatorScenarios = []expressionScenario{ "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, document: mergeDocSample,