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"
|
|
|
|
|
2020-11-20 04:29:53 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-10-17 11:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func isTruthy(c *CandidateNode) (bool, error) {
|
2020-10-27 05:45:16 +00:00
|
|
|
node := UnwrapDoc(c.Node)
|
2020-10-17 11:10:47 +00:00
|
|
|
value := true
|
2020-10-27 05:45:16 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type boolOp func(bool, bool) bool
|
|
|
|
|
2020-11-20 04:29:53 +00:00
|
|
|
func performBoolOp(results *list.List, lhs *list.List, rhs *list.List, op boolOp) error {
|
|
|
|
for lhsChild := lhs.Front(); lhsChild != nil; lhsChild = lhsChild.Next() {
|
|
|
|
lhsCandidate := lhsChild.Value.(*CandidateNode)
|
|
|
|
lhsTrue, errDecoding := isTruthy(lhsCandidate)
|
|
|
|
if errDecoding != nil {
|
|
|
|
return errDecoding
|
|
|
|
}
|
|
|
|
|
|
|
|
for rhsChild := rhs.Front(); rhsChild != nil; rhsChild = rhsChild.Next() {
|
|
|
|
rhsCandidate := rhsChild.Value.(*CandidateNode)
|
|
|
|
rhsTrue, errDecoding := isTruthy(rhsCandidate)
|
|
|
|
if errDecoding != nil {
|
|
|
|
return errDecoding
|
|
|
|
}
|
|
|
|
boolResult := createBooleanCandidate(lhsCandidate, op(lhsTrue, rhsTrue))
|
|
|
|
results.PushBack(boolResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:54:58 +00:00
|
|
|
func booleanOp(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode, op boolOp) (*list.List, error) {
|
|
|
|
var results = list.New()
|
2020-10-17 11:10:47 +00:00
|
|
|
|
2020-11-20 04:29:53 +00:00
|
|
|
if matchingNodes.Len() == 0 {
|
|
|
|
lhs, err := d.GetMatchingNodes(list.New(), pathNode.Lhs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rhs, err := d.GetMatchingNodes(list.New(), pathNode.Rhs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return results, performBoolOp(results, lhs, rhs, op)
|
|
|
|
}
|
|
|
|
|
2020-10-17 11:10:47 +00:00
|
|
|
for el := matchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2020-10-27 05:45:16 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Lhs)
|
2020-10-17 11:10:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-27 05:45:16 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), pathNode.Rhs)
|
2020-10-17 11:10:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-20 04:29:53 +00:00
|
|
|
err = performBoolOp(results, lhs, rhs, op)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:54:58 +00:00
|
|
|
func OrOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
2020-10-17 11:10:47 +00:00
|
|
|
log.Debugf("-- orOp")
|
|
|
|
return booleanOp(d, matchingNodes, pathNode, func(b1 bool, b2 bool) bool {
|
|
|
|
return b1 || b2
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-21 01:54:58 +00:00
|
|
|
func AndOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
2020-10-17 11:10:47 +00:00
|
|
|
log.Debugf("-- AndOp")
|
|
|
|
return booleanOp(d, matchingNodes, pathNode, func(b1 bool, b2 bool) bool {
|
|
|
|
return b1 && b2
|
|
|
|
})
|
|
|
|
}
|
2020-11-22 02:16:54 +00:00
|
|
|
|
|
|
|
func NotOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
|
|
|
log.Debugf("-- notOperation")
|
|
|
|
var results = list.New()
|
|
|
|
|
|
|
|
for el := matchMap.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
log.Debug("notOperation checking %v", candidate)
|
|
|
|
truthy, errDecoding := isTruthy(candidate)
|
|
|
|
if errDecoding != nil {
|
|
|
|
return nil, errDecoding
|
|
|
|
}
|
|
|
|
result := createBooleanCandidate(candidate, !truthy)
|
|
|
|
results.PushBack(result)
|
|
|
|
}
|
|
|
|
return results, nil
|
|
|
|
}
|