adv search with prefix!

This commit is contained in:
Mike Farah 2020-01-11 19:52:33 +11:00
parent a3f8f9df10
commit 350a8343e9
4 changed files with 16 additions and 11 deletions

View File

@ -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)
}

View File

@ -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)
},
}
}

View File

@ -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 {

6
yq.go
View File

@ -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