Dont recurse into scalar nodes

Fixes https://github.com/mikefarah/yq/issues/375
This commit is contained in:
Mike Farah 2020-02-28 15:01:46 +11:00
parent 8e6ceba2ac
commit b64187fe32
2 changed files with 17 additions and 1 deletions

View File

@ -2005,6 +2005,20 @@ func TestDeleteYamlArrayCmd(t *testing.T) {
test.AssertResult(t, expectedOutput, result.Output) test.AssertResult(t, expectedOutput, result.Output)
} }
func TestReadExpression(t *testing.T) {
content := `name: value`
filename := test.WriteTempYamlFile(content)
defer test.RemoveTempYamlFile(filename)
cmd := getRootCommand()
result := test.RunCmd(cmd, fmt.Sprintf("r %s (x==f)", filename))
if result.Error != nil {
t.Error(result.Error)
}
expectedOutput := ``
test.AssertResult(t, expectedOutput, result.Output)
}
func TestDeleteYamlArrayExpressionCmd(t *testing.T) { func TestDeleteYamlArrayExpressionCmd(t *testing.T) {
content := `- name: fred content := `- name: fred
- name: cat - name: cat

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"strings" "strings"
yaml "gopkg.in/yaml.v3"
) )
type PathParser interface { type PathParser interface {
@ -45,7 +47,7 @@ func (p *pathParser) MatchesNextPathElement(nodeContext NodeContext, nodeKey str
} }
var headString = fmt.Sprintf("%v", head) var headString = fmt.Sprintf("%v", head)
if strings.Contains(headString, "==") { if strings.Contains(headString, "==") && nodeContext.Node.Kind != yaml.ScalarNode {
log.Debug("ooh deep recursion time") log.Debug("ooh deep recursion time")
result := strings.SplitN(headString, "==", 2) result := strings.SplitN(headString, "==", 2)
path := strings.TrimSpace(result[0]) path := strings.TrimSpace(result[0])