1
0
mirror of https://github.com/taigrr/yq synced 2025-01-18 04:53:17 -08:00

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

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