From 350a8343e9cc6e165afc208922505c8250cfe156 Mon Sep 17 00:00:00 2001 From: Mike Farah Date: Sat, 11 Jan 2020 19:52:33 +1100 Subject: [PATCH] adv search with prefix! --- commands_test.go | 2 +- .../filter_matching_node_navigation_strategy.go | 2 +- pkg/yqlib/path_parser.go | 17 +++++++++++------ yq.go | 6 +++--- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/commands_test.go b/commands_test.go index bacc4960..9a514d66 100644 --- a/commands_test.go +++ b/commands_test.go @@ -105,7 +105,7 @@ func TestReadWithAdvancedFilterCmd(t *testing.T) { func TestReadWithAdvancedFilterMapCmd(t *testing.T) { cmd := getRootCommand() - result := test.RunCmd(cmd, "read -v examples/sample.yaml b.e[name==fred]") + result := test.RunCmd(cmd, "read -v examples/sample.yaml b.e[name==fr*]") if result.Error != nil { t.Error(result.Error) } diff --git a/pkg/yqlib/filter_matching_node_navigation_strategy.go b/pkg/yqlib/filter_matching_node_navigation_strategy.go index ad4ede0c..ac49e1c4 100644 --- a/pkg/yqlib/filter_matching_node_navigation_strategy.go +++ b/pkg/yqlib/filter_matching_node_navigation_strategy.go @@ -15,7 +15,7 @@ func FilterMatchingNodesNavigationStrategy(value string) NavigationStrategy { }, shouldVisitExtraFn: func(nodeContext NodeContext) bool { log.Debug("does %v match %v ? %v", nodeContext.Node.Value, value, nodeContext.Node.Value == value) - return nodeContext.Node.Value == value + return matchesString(value, nodeContext.Node.Value) }, } } diff --git a/pkg/yqlib/path_parser.go b/pkg/yqlib/path_parser.go index 38464522..a06ff048 100644 --- a/pkg/yqlib/path_parser.go +++ b/pkg/yqlib/path_parser.go @@ -17,6 +17,15 @@ func NewPathParser() PathParser { return &pathParser{} } +func matchesString(expression string, value string) bool { + var prefixMatch = strings.TrimSuffix(expression, "*") + if prefixMatch != expression { + log.Debug("prefix match, %v", strings.HasPrefix(value, prefixMatch)) + return strings.HasPrefix(value, prefixMatch) + } + return value == expression +} + func (p *pathParser) IsPathExpression(pathElement string) bool { return pathElement == "*" || pathElement == "**" || strings.Contains(pathElement, "==") } @@ -60,12 +69,8 @@ func (p *pathParser) MatchesNextPathElement(nodeContext NodeContext, nodeKey str return true } } - var prefixMatch = strings.TrimSuffix(head, "*") - if prefixMatch != head { - log.Debug("prefix match, %v", strings.HasPrefix(nodeKey, prefixMatch)) - return strings.HasPrefix(nodeKey, prefixMatch) - } - return nodeKey == head + + return matchesString(head, nodeKey) } func (p *pathParser) ParsePath(path string) []string { diff --git a/yq.go b/yq.go index 2a821756..fc160538 100644 --- a/yq.go +++ b/yq.go @@ -100,7 +100,7 @@ yq read things.yaml a.b.c yq r - a.b.c (reads from stdin) yq r things.yaml a.*.c yq r things.yaml a.**.c -yq r things.yaml a.(child.subchild==cool).c +yq r things.yaml a.(child.subchild==co*).c yq r -d1 things.yaml 'a.array[0].blah' yq r things.yaml 'a.array[*].blah' yq r -- things.yaml --key-starting-with-dashes.blah @@ -122,7 +122,7 @@ func createWriteCmd() *cobra.Command { yq write things.yaml a.b.c true yq write things.yaml 'a.*.c' true yq write things.yaml 'a.**' true -yq write things.yaml a.(child.subchild==cool).c true +yq write things.yaml a.(child.subchild==co*).c true yq write things.yaml a.b.c --tag '!!str' true yq write things.yaml a.b.c --tag '!!float' 3 yq write --inplace -- things.yaml a.b.c --cat @@ -189,7 +189,7 @@ func createDeleteCmd() *cobra.Command { Example: ` yq delete things.yaml a.b.c yq delete things.yaml a.*.c -yq delete things.yaml a.(child.subchild==cool).c +yq delete things.yaml a.(child.subchild==co*).c yq delete things.yaml a.** yq delete --inplace things.yaml a.b.c yq delete --inplace -- things.yaml --key-starting-with-dash