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"
|
2021-05-14 05:01:44 +00:00
|
|
|
|
2020-11-20 04:29:53 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-10-17 11:10:47 +00:00
|
|
|
)
|
|
|
|
|
2021-05-14 05:01:44 +00:00
|
|
|
func isTruthyNode(node *yaml.Node) (bool, error) {
|
2020-10-17 11:10:47 +00:00
|
|
|
value := true
|
2020-10-20 05:27:30 +00:00
|
|
|
if node.Tag == "!!null" {
|
|
|
|
return false, nil
|
|
|
|
}
|
2020-10-17 11:10:47 +00:00
|
|
|
if node.Kind == yaml.ScalarNode && node.Tag == "!!bool" {
|
|
|
|
errDecoding := node.Decode(&value)
|
|
|
|
if errDecoding != nil {
|
|
|
|
return false, errDecoding
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 04:29:55 +00:00
|
|
|
func isTruthy(c *CandidateNode) (bool, error) {
|
|
|
|
node := unwrapDoc(c.Node)
|
|
|
|
return isTruthyNode(node)
|
|
|
|
}
|
|
|
|
|
2020-10-17 11:10:47 +00:00
|
|
|
type boolOp func(bool, bool) bool
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func performBoolOp(op boolOp) func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
return func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
2021-05-09 02:44:05 +00:00
|
|
|
owner := lhs
|
2020-10-17 11:10:47 +00:00
|
|
|
|
2021-05-09 02:44:05 +00:00
|
|
|
if lhs == nil && rhs == nil {
|
|
|
|
owner = &CandidateNode{}
|
|
|
|
} else if lhs == nil {
|
|
|
|
owner = rhs
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
|
|
|
|
2021-05-09 02:44:05 +00:00
|
|
|
var errDecoding error
|
|
|
|
lhsTrue := false
|
|
|
|
if lhs != nil {
|
|
|
|
lhs.Node = unwrapDoc(lhs.Node)
|
|
|
|
lhsTrue, errDecoding = isTruthy(lhs)
|
|
|
|
|
|
|
|
if errDecoding != nil {
|
|
|
|
return nil, errDecoding
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Debugf("-- lhsTrue", lhsTrue)
|
|
|
|
|
|
|
|
rhsTrue := false
|
|
|
|
if rhs != nil {
|
|
|
|
rhs.Node = unwrapDoc(rhs.Node)
|
|
|
|
rhsTrue, errDecoding = isTruthy(rhs)
|
|
|
|
if errDecoding != nil {
|
|
|
|
return nil, errDecoding
|
|
|
|
}
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
2021-05-09 02:44:05 +00:00
|
|
|
log.Debugf("-- rhsTrue", rhsTrue)
|
2020-10-17 11:10:47 +00:00
|
|
|
|
2021-05-09 02:44:05 +00:00
|
|
|
return createBooleanCandidate(owner, op(lhsTrue, rhsTrue)), nil
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 05:01:44 +00:00
|
|
|
func findBoolean(wantBool bool, d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, sequenceNode *yaml.Node) (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
|
|
|
|
candidate := &CandidateNode{Node: node}
|
2021-05-16 04:00:30 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode)
|
2021-05-14 05:01:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if rhs.MatchingNodes.Len() > 0 {
|
|
|
|
node = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node
|
|
|
|
} else {
|
|
|
|
// no results found, ignore this entry
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 04:29:55 +00:00
|
|
|
truthy, err := isTruthyNode(node)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if truthy == wantBool {
|
|
|
|
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)
|
|
|
|
candidateNode := unwrapDoc(candidate.Node)
|
|
|
|
if candidateNode.Kind != yaml.SequenceNode {
|
|
|
|
return Context{}, fmt.Errorf("any only supports arrays, was %v", candidateNode.Tag)
|
|
|
|
}
|
2022-02-07 00:55:55 +00:00
|
|
|
booleanResult, err := findBoolean(false, d, context, expressionNode.RHS, candidateNode)
|
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)
|
|
|
|
candidateNode := unwrapDoc(candidate.Node)
|
|
|
|
if candidateNode.Kind != yaml.SequenceNode {
|
|
|
|
return Context{}, fmt.Errorf("any only supports arrays, was %v", candidateNode.Tag)
|
|
|
|
}
|
2022-02-07 00:55:55 +00:00
|
|
|
booleanResult, err := findBoolean(true, d, context, expressionNode.RHS, candidateNode)
|
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) {
|
2020-10-17 11:10:47 +00:00
|
|
|
log.Debugf("-- orOp")
|
2021-05-16 04:17:13 +00:00
|
|
|
return crossFunction(d, context.ReadOnlyClone(), expressionNode, performBoolOp(
|
2020-12-21 00:42:35 +00:00
|
|
|
func(b1 bool, b2 bool) bool {
|
2021-05-09 02:44:05 +00:00
|
|
|
log.Debugf("-- peformingOrOp with %v and %v", b1, b2)
|
2020-12-21 00:42:35 +00:00
|
|
|
return b1 || b2
|
2021-05-09 02:44:05 +00:00
|
|
|
}), true)
|
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) {
|
2020-10-17 11:10:47 +00:00
|
|
|
log.Debugf("-- AndOp")
|
2021-05-16 04:17:13 +00:00
|
|
|
return crossFunction(d, context.ReadOnlyClone(), expressionNode, performBoolOp(
|
2020-12-21 00:42:35 +00:00
|
|
|
func(b1 bool, b2 bool) bool {
|
|
|
|
return b1 && b2
|
2021-05-09 02:44:05 +00:00
|
|
|
}), true)
|
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)
|
|
|
|
truthy, errDecoding := isTruthy(candidate)
|
|
|
|
if errDecoding != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, errDecoding
|
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
|
|
|
}
|