mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 05:38:04 +00:00
adv search with prefix!
This commit is contained in:
parent
a3f8f9df10
commit
350a8343e9
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -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
6
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
|
||||
|
Loading…
Reference in New Issue
Block a user