yq/pkg/yqlib/operator_has.go

56 lines
1.4 KiB
Go
Raw Normal View History

2020-11-24 00:38:39 +00:00
package yqlib
import (
"container/list"
"strconv"
)
func hasOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2020-11-24 00:38:39 +00:00
log.Debugf("-- hasOperation")
var results = list.New()
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
2020-11-24 00:38:39 +00:00
if err != nil {
return Context{}, err
2020-11-24 00:38:39 +00:00
}
wantedKey := "null"
wanted := &CandidateNode{Tag: "!!null"}
if rhs.MatchingNodes.Len() != 0 {
wanted = rhs.MatchingNodes.Front().Value.(*CandidateNode)
wantedKey = wanted.Value
}
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-11-24 00:38:39 +00:00
candidate := el.Value.(*CandidateNode)
var contents = candidate.Content
switch candidate.Kind {
case MappingNode:
2020-11-24 00:38:39 +00:00
candidateHasKey := false
for index := 0; index < len(contents) && !candidateHasKey; index = index + 2 {
key := contents[index]
if key.Value == wantedKey {
candidateHasKey = true
}
}
results.PushBack(createBooleanCandidate(candidate, candidateHasKey))
case SequenceNode:
2020-11-24 00:38:39 +00:00
candidateHasKey := false
if wanted.Tag == "!!int" {
var number, errParsingInt = strconv.ParseInt(wantedKey, 10, 64)
2020-11-24 00:38:39 +00:00
if errParsingInt != nil {
return Context{}, errParsingInt
2020-11-24 00:38:39 +00:00
}
candidateHasKey = int64(len(contents)) > number
}
results.PushBack(createBooleanCandidate(candidate, candidateHasKey))
default:
results.PushBack(createBooleanCandidate(candidate, false))
}
}
return context.ChildContext(results), nil
2020-11-24 00:38:39 +00:00
}