2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-08 23:59:03 +00:00
|
|
|
|
2020-10-09 01:04:19 +00:00
|
|
|
import (
|
2020-10-10 04:00:39 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-11-22 00:56:28 +00:00
|
|
|
logging "gopkg.in/op/go-logging.v1"
|
2020-10-09 01:04:19 +00:00
|
|
|
)
|
|
|
|
|
2020-10-08 23:59:03 +00:00
|
|
|
type DataTreeNavigator interface {
|
2023-11-23 00:54:25 +00:00
|
|
|
// given the context and an expressionNode,
|
2021-02-02 07:17:59 +00:00
|
|
|
// this will process the against the given expressionNode and return
|
|
|
|
// a new context of matching candidates
|
|
|
|
GetMatchingNodes(context Context, expressionNode *ExpressionNode) (Context, error)
|
2023-03-25 23:59:15 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
DeeplyAssign(context Context, path []interface{}, rhsNode *CandidateNode) error
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 00:56:28 +00:00
|
|
|
type dataTreeNavigator struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDataTreeNavigator() DataTreeNavigator {
|
|
|
|
return &dataTreeNavigator{}
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (d *dataTreeNavigator) DeeplyAssign(context Context, path []interface{}, rhsCandidateNode *CandidateNode) error {
|
2023-03-25 23:59:15 +00:00
|
|
|
|
|
|
|
assignmentOp := &Operation{OperationType: assignOpType, Preferences: assignPreferences{}}
|
2024-02-15 05:15:01 +00:00
|
|
|
|
|
|
|
if rhsCandidateNode.Kind == MappingNode {
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debug("DeeplyAssign: deeply merging object")
|
2024-02-15 05:15:01 +00:00
|
|
|
// if the rhs is a map, we need to deeply merge it in.
|
|
|
|
// otherwise we'll clobber any existing fields
|
|
|
|
assignmentOp = &Operation{OperationType: multiplyAssignOpType, Preferences: multiplyPreferences{
|
|
|
|
AppendArrays: true,
|
|
|
|
TraversePrefs: traversePreferences{DontFollowAlias: true},
|
|
|
|
AssignPrefs: assignPreferences{},
|
|
|
|
}}
|
|
|
|
}
|
2023-03-25 23:59:15 +00:00
|
|
|
|
|
|
|
rhsOp := &Operation{OperationType: valueOpType, CandidateNode: rhsCandidateNode}
|
|
|
|
|
|
|
|
assignmentOpNode := &ExpressionNode{
|
|
|
|
Operation: assignmentOp,
|
|
|
|
LHS: createTraversalTree(path, traversePreferences{}, false),
|
|
|
|
RHS: &ExpressionNode{Operation: rhsOp},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := d.GetMatchingNodes(context, assignmentOpNode)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func (d *dataTreeNavigator) GetMatchingNodes(context Context, expressionNode *ExpressionNode) (Context, error) {
|
2021-01-12 23:18:53 +00:00
|
|
|
if expressionNode == nil {
|
2020-10-13 01:51:37 +00:00
|
|
|
log.Debugf("getMatchingNodes - nothing to do")
|
2021-02-02 07:17:59 +00:00
|
|
|
return context, nil
|
2020-10-13 01:51:37 +00:00
|
|
|
}
|
2021-01-12 23:18:53 +00:00
|
|
|
log.Debugf("Processing Op: %v", expressionNode.Operation.toString())
|
2020-10-17 11:10:47 +00:00
|
|
|
if log.IsEnabledFor(logging.DEBUG) {
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-10-17 11:10:47 +00:00
|
|
|
log.Debug(NodeToString(el.Value.(*CandidateNode)))
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 23:18:53 +00:00
|
|
|
handler := expressionNode.Operation.OperationType.Handler
|
2020-10-20 02:53:26 +00:00
|
|
|
if handler != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return handler(d, context, expressionNode)
|
2020-10-08 23:59:03 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, fmt.Errorf("Unknown operator %v", expressionNode.Operation.OperationType)
|
2020-10-08 23:59:03 +00:00
|
|
|
|
|
|
|
}
|