2020-11-24 00:38:39 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
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()
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
|
2020-11-24 00:38:39 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-11-24 00:38:39 +00:00
|
|
|
}
|
|
|
|
|
2021-05-16 04:00:30 +00:00
|
|
|
wantedKey := "null"
|
2023-04-08 11:27:47 +00:00
|
|
|
wanted := &CandidateNode{Tag: "!!null"}
|
2021-05-16 04:00:30 +00:00
|
|
|
if rhs.MatchingNodes.Len() != 0 {
|
2023-04-08 11:27:47 +00:00
|
|
|
wanted = rhs.MatchingNodes.Front().Value.(*CandidateNode)
|
2021-05-16 04:00:30 +00:00
|
|
|
wantedKey = wanted.Value
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-11-24 00:38:39 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
|
|
|
// grab the first value
|
2023-04-08 11:27:47 +00:00
|
|
|
candidateNode := candidate.unwrapDocument()
|
2021-01-11 22:30:24 +00:00
|
|
|
var contents = candidateNode.Content
|
|
|
|
switch candidateNode.Kind {
|
2023-04-08 11:27:47 +00:00
|
|
|
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))
|
2023-04-08 11:27:47 +00:00
|
|
|
case SequenceNode:
|
2020-11-24 00:38:39 +00:00
|
|
|
candidateHasKey := false
|
|
|
|
if wanted.Tag == "!!int" {
|
2021-12-20 22:30:08 +00:00
|
|
|
var number, errParsingInt = strconv.ParseInt(wantedKey, 10, 64)
|
2020-11-24 00:38:39 +00:00
|
|
|
if errParsingInt != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2020-11-24 00:38:39 +00:00
|
|
|
}
|