Extract out compound assign logic, use it add and subtract ops

This commit is contained in:
Mike Farah
2021-07-07 20:00:46 +10:00
parent b4b2e1217a
commit cce3af001a
3 changed files with 28 additions and 26 deletions
+26
View File
@@ -10,6 +10,32 @@ import (
type operatorHandler func(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error)
type compoundCalculation func(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode
func compoundAssignFunction(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode, calculation compoundCalculation) (Context, error) {
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs)
if err != nil {
return Context{}, err
}
assignmentOp := &Operation{OperationType: assignOpType}
valueOp := &Operation{OperationType: valueOpType}
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
valueOp.CandidateNode = candidate
valueExpression := &ExpressionNode{Operation: valueOp}
assignmentOpNode := &ExpressionNode{Operation: assignmentOp, Lhs: valueExpression, Rhs: calculation(valueExpression, expressionNode.Rhs)}
_, err = d.GetMatchingNodes(context, assignmentOpNode)
if err != nil {
return Context{}, err
}
}
return context, nil
}
func unwrapDoc(node *yaml.Node) *yaml.Node {
if node.Kind == yaml.DocumentNode {
return node.Content[0]