From b64187fe3282ce04187f201395064537839f39f8 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Fri, 28 Feb 2020 15:01:46 +1100 Subject: [PATCH] Dont recurse into scalar nodes Fixes https://github.com/mikefarah/yq/issues/375 --- cmd/commands_test.go | 14 ++++++++++++++ pkg/yqlib/path_parser.go | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cmd/commands_test.go b/cmd/commands_test.go index ce1f2bb..1f36516 100644 --- a/cmd/commands_test.go +++ b/cmd/commands_test.go @@ -2005,6 +2005,20 @@ func TestDeleteYamlArrayCmd(t *testing.T) { 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) { content := `- name: fred - name: cat diff --git a/pkg/yqlib/path_parser.go b/pkg/yqlib/path_parser.go index 384e219..90a5f8a 100644 --- a/pkg/yqlib/path_parser.go +++ b/pkg/yqlib/path_parser.go @@ -4,6 +4,8 @@ import ( "fmt" "strconv" "strings" + + yaml "gopkg.in/yaml.v3" ) type PathParser interface { @@ -45,7 +47,7 @@ func (p *pathParser) MatchesNextPathElement(nodeContext NodeContext, nodeKey str } 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") result := strings.SplitN(headString, "==", 2) path := strings.TrimSpace(result[0])