2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-17 11:10:47 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-21 01:54:58 +00:00
|
|
|
"container/list"
|
2021-05-14 04:29:55 +00:00
|
|
|
"fmt"
|
2023-04-09 01:14:51 +00:00
|
|
|
"strings"
|
2020-10-17 11:10:47 +00:00
|
|
|
)
|
|
|
|
|
2023-06-07 17:45:42 +00:00
|
|
|
func isTruthyNode(node *CandidateNode) bool {
|
|
|
|
if node == nil {
|
2023-05-09 03:51:21 +00:00
|
|
|
return false
|
2020-10-20 05:27:30 +00:00
|
|
|
}
|
2023-04-09 01:14:51 +00:00
|
|
|
if node.Tag == "!!null" {
|
2023-05-09 03:51:21 +00:00
|
|
|
return false
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
2023-04-09 01:14:51 +00:00
|
|
|
if node.Kind == ScalarNode && node.Tag == "!!bool" {
|
|
|
|
// yes/y/true/on
|
|
|
|
return (strings.EqualFold(node.Value, "y") ||
|
|
|
|
strings.EqualFold(node.Value, "yes") ||
|
|
|
|
strings.EqualFold(node.Value, "on") ||
|
2023-05-09 03:51:21 +00:00
|
|
|
strings.EqualFold(node.Value, "true"))
|
2020-10-17 11:10:47 +00:00
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
}
|
2023-05-09 03:51:21 +00:00
|
|
|
return true
|
2022-03-20 01:55:58 +00:00
|
|
|
}
|
2020-10-17 11:10:47 +00:00
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
func getOwner(lhs *CandidateNode, rhs *CandidateNode) *CandidateNode {
|
|
|
|
owner := lhs
|
2020-10-17 11:10:47 +00:00
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
if lhs == nil && rhs == nil {
|
|
|
|
owner = &CandidateNode{}
|
|
|
|
} else if lhs == nil {
|
|
|
|
owner = rhs
|
|
|
|
}
|
|
|
|
return owner
|
|
|
|
}
|
2020-10-17 11:10:47 +00:00
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
func returnRhsTruthy(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
owner := getOwner(lhs, rhs)
|
2023-05-09 03:51:21 +00:00
|
|
|
rhsBool := isTruthyNode(rhs)
|
2021-05-09 02:44:05 +00:00
|
|
|
|
2022-03-20 01:55:58 +00:00
|
|
|
return createBooleanCandidate(owner, rhsBool), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func returnLHSWhen(targetBool bool) func(lhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
return func(lhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
var err error
|
|
|
|
var lhsBool bool
|
|
|
|
|
2023-05-09 03:51:21 +00:00
|
|
|
if lhsBool = isTruthyNode(lhs); lhsBool != targetBool {
|
2022-03-20 01:55:58 +00:00
|
|
|
return nil, err
|
2021-05-09 02:44:05 +00:00
|
|
|
}
|
2022-03-20 01:55:58 +00:00
|
|
|
owner := &CandidateNode{}
|
|
|
|
if lhs != nil {
|
|
|
|
owner = lhs
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
2022-03-20 01:55:58 +00:00
|
|
|
return createBooleanCandidate(owner, targetBool), nil
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 01:14:51 +00:00
|
|
|
func findBoolean(wantBool bool, d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, sequenceNode *CandidateNode) (bool, error) {
|
2021-05-14 04:29:55 +00:00
|
|
|
for _, node := range sequenceNode.Content {
|
|
|
|
|
2021-05-14 05:01:44 +00:00
|
|
|
if expressionNode != nil {
|
|
|
|
//need to evaluate the expression against the node
|
2023-04-09 01:14:51 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(node), expressionNode)
|
2021-05-14 05:01:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if rhs.MatchingNodes.Len() > 0 {
|
2023-04-09 01:14:51 +00:00
|
|
|
node = rhs.MatchingNodes.Front().Value.(*CandidateNode)
|
2021-05-14 05:01:44 +00:00
|
|
|
} else {
|
|
|
|
// no results found, ignore this entry
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-09 03:51:21 +00:00
|
|
|
if isTruthyNode(node) == wantBool {
|
2021-05-14 04:29:55 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func allOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
var results = list.New()
|
|
|
|
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2023-06-07 17:45:42 +00:00
|
|
|
if candidate.Kind != SequenceNode {
|
|
|
|
return Context{}, fmt.Errorf("any only supports arrays, was %v", candidate.Tag)
|
2021-05-14 04:29:55 +00:00
|
|
|
}
|
2023-06-07 17:45:42 +00:00
|
|
|
booleanResult, err := findBoolean(false, d, context, expressionNode.RHS, candidate)
|
2021-05-14 04:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
result := createBooleanCandidate(candidate, !booleanResult)
|
|
|
|
results.PushBack(result)
|
|
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func anyOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
var results = list.New()
|
|
|
|
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2023-06-07 17:45:42 +00:00
|
|
|
if candidate.Kind != SequenceNode {
|
|
|
|
return Context{}, fmt.Errorf("any only supports arrays, was %v", candidate.Tag)
|
2021-05-14 04:29:55 +00:00
|
|
|
}
|
2023-06-07 17:45:42 +00:00
|
|
|
booleanResult, err := findBoolean(true, d, context, expressionNode.RHS, candidate)
|
2021-05-14 04:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
result := createBooleanCandidate(candidate, booleanResult)
|
|
|
|
results.PushBack(result)
|
|
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func orOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2022-03-20 01:55:58 +00:00
|
|
|
prefs := crossFunctionPreferences{
|
|
|
|
CalcWhenEmpty: true,
|
|
|
|
Calculation: returnRhsTruthy,
|
|
|
|
LhsResultValue: returnLHSWhen(true),
|
|
|
|
}
|
|
|
|
return crossFunctionWithPrefs(d, context.ReadOnlyClone(), expressionNode, prefs)
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func andOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2022-03-20 01:55:58 +00:00
|
|
|
prefs := crossFunctionPreferences{
|
|
|
|
CalcWhenEmpty: true,
|
|
|
|
Calculation: returnRhsTruthy,
|
|
|
|
LhsResultValue: returnLHSWhen(false),
|
|
|
|
}
|
|
|
|
return crossFunctionWithPrefs(d, context.ReadOnlyClone(), expressionNode, prefs)
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
2020-11-22 02:16:54 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func notOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-11-22 02:16:54 +00:00
|
|
|
log.Debugf("-- notOperation")
|
|
|
|
var results = list.New()
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-11-22 02:16:54 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
log.Debug("notOperation checking %v", candidate)
|
2023-05-09 03:51:21 +00:00
|
|
|
truthy := isTruthyNode(candidate)
|
2020-11-22 02:16:54 +00:00
|
|
|
result := createBooleanCandidate(candidate, !truthy)
|
|
|
|
results.PushBack(result)
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2020-11-22 02:16:54 +00:00
|
|
|
}
|