Nicer error message when trying to use merge anchor tags other than maps #1184

This commit is contained in:
Mike Farah 2022-04-27 14:46:52 +10:00
parent 70ad7a35a8
commit 26529fae94
2 changed files with 18 additions and 0 deletions

View File

@ -309,6 +309,9 @@ func doTraverseMap(newMatches *orderedmap.OrderedMap, candidate *CandidateNode,
func traverseMergeAnchor(newMatches *orderedmap.OrderedMap, originalCandidate *CandidateNode, value *yaml.Node, wantedKey string, prefs traversePreferences, splat bool) error {
switch value.Kind {
case yaml.AliasNode:
if value.Alias.Kind != yaml.MappingNode {
return fmt.Errorf("can only use merge anchors with maps (!!map), but got %v", value.Alias.Tag)
}
candidateNode := originalCandidate.CreateReplacement(value.Alias)
return doTraverseMap(newMatches, candidateNode, wantedKey, prefs, splat)
case yaml.SequenceNode:

View File

@ -26,6 +26,15 @@ foobar:
thing: foobar_thing
`
// cannot use merge anchors with arrays
var badAliasSample = `
_common: &common-docker-file
- FROM ubuntu:18.04
steps:
<<: *common-docker-file
`
var traversePathOperatorScenarios = []expressionScenario{
{
skipDoc: true,
@ -529,6 +538,12 @@ var traversePathOperatorScenarios = []expressionScenario{
"D0, P[a 2], (!!str)::c\n",
},
},
{
skipDoc: true,
document: badAliasSample,
expression: ".steps[]",
expectedError: "can only use merge anchors with maps (!!map), but got !!seq",
},
}
func TestTraversePathOperatorScenarios(t *testing.T) {