mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-04 11:25:37 +00:00
Fix merge anchor traversing
- Allow inline maps instead of just aliases - Disallow nested sequences - Disallow other types Closes #2386
This commit is contained in:
parent
40808838ba
commit
162ea5437c
@ -295,17 +295,26 @@ 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 {
|
||||||
switch value.Kind {
|
switch value.Kind {
|
||||||
case AliasNode:
|
case AliasNode:
|
||||||
if value.Alias.Kind != MappingNode {
|
return traverseMergeAnchor(newMatches, value.Alias, wantedKey, prefs, splat)
|
||||||
return fmt.Errorf("can only use merge anchors with maps (!!map), but got %v", value.Alias.Tag)
|
case MappingNode:
|
||||||
}
|
return doTraverseMap(newMatches, value, wantedKey, prefs, splat)
|
||||||
return doTraverseMap(newMatches, value.Alias, wantedKey, prefs, splat)
|
|
||||||
case SequenceNode:
|
case SequenceNode:
|
||||||
for _, childValue := range value.Content {
|
for _, childValue := range value.Content {
|
||||||
err := traverseMergeAnchor(newMatches, childValue, wantedKey, prefs, splat)
|
if childValue.Kind == AliasNode {
|
||||||
|
childValue = childValue.Alias
|
||||||
|
}
|
||||||
|
if childValue.Kind != MappingNode {
|
||||||
|
return fmt.Errorf(
|
||||||
|
"can only use merge anchors with maps (!!map) or sequences (!!seq) of maps, but got sequence containing %v",
|
||||||
|
childValue.Tag)
|
||||||
|
}
|
||||||
|
err := doTraverseMap(newMatches, childValue, wantedKey, prefs, splat)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("can only use merge anchors with maps (!!map) or sequences (!!seq) of maps, but got %v", value.Tag)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -25,7 +25,7 @@ foobar:
|
|||||||
thing: foobar_thing
|
thing: foobar_thing
|
||||||
`
|
`
|
||||||
|
|
||||||
// cannot use merge anchors with arrays
|
// cannot use merge anchors with arrays of non-maps
|
||||||
var badAliasSample = `
|
var badAliasSample = `
|
||||||
_common: &common-docker-file
|
_common: &common-docker-file
|
||||||
- FROM ubuntu:18.04
|
- FROM ubuntu:18.04
|
||||||
@ -365,6 +365,22 @@ var traversePathOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[0], (!!null)::null\n",
|
"D0, P[0], (!!null)::null\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `{<<: {a: 42}}`,
|
||||||
|
expression: `.a`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[<< a], (!!int)::42\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `{<<: [{a: 42}]}`,
|
||||||
|
expression: `.a`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[<< 0 a], (!!int)::42\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
document: mergeDocSample,
|
document: mergeDocSample,
|
||||||
@ -553,9 +569,16 @@ var traversePathOperatorScenarios = []expressionScenario{
|
|||||||
skipDoc: true,
|
skipDoc: true,
|
||||||
document: badAliasSample,
|
document: badAliasSample,
|
||||||
expression: ".steps[]",
|
expression: ".steps[]",
|
||||||
expectedError: "can only use merge anchors with maps (!!map), but got !!seq",
|
expectedError: "can only use merge anchors with maps (!!map) or sequences (!!seq) of maps, but got sequence containing !!str",
|
||||||
skipForGoccy: true, // throws an error on parsing, that's fine
|
skipForGoccy: true, // throws an error on parsing, that's fine
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
document: `{<<: 42}`,
|
||||||
|
expression: ".[]",
|
||||||
|
expectedError: "can only use merge anchors with maps (!!map) or sequences (!!seq) of maps, but got !!int",
|
||||||
|
skipForGoccy: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTraversePathOperatorScenarios(t *testing.T) {
|
func TestTraversePathOperatorScenarios(t *testing.T) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user