mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-12 13:48:06 +00:00
Extract out compound assign logic, use it add and subtract ops
This commit is contained in:
parent
b4b2e1217a
commit
cce3af001a
@ -15,27 +15,7 @@ func createAddOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func addAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
func addAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
lhs, err := d.GetMatchingNodes(context, expressionNode.Lhs)
|
return compoundAssignFunction(d, context, expressionNode, createAddOp)
|
||||||
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: createAddOp(valueExpression, expressionNode.Rhs)}
|
|
||||||
|
|
||||||
_, err = d.GetMatchingNodes(context, assignmentOpNode)
|
|
||||||
if err != nil {
|
|
||||||
return Context{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return context, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func toNodes(candidate *CandidateNode) []*yaml.Node {
|
func toNodes(candidate *CandidateNode) []*yaml.Node {
|
||||||
|
@ -15,11 +15,7 @@ func createSubtractOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode
|
|||||||
}
|
}
|
||||||
|
|
||||||
func subtractAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
func subtractAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
assignmentOp := &Operation{OperationType: assignOpType}
|
return compoundAssignFunction(d, context, expressionNode, createSubtractOp)
|
||||||
assignmentOp.UpdateAssign = true
|
|
||||||
selfExpression := &ExpressionNode{Operation: &Operation{OperationType: selfReferenceOpType}}
|
|
||||||
assignmentOpNode := &ExpressionNode{Operation: assignmentOp, Lhs: expressionNode.Lhs, Rhs: createSubtractOp(selfExpression, expressionNode.Rhs)}
|
|
||||||
return d.GetMatchingNodes(context, assignmentOpNode)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func subtractOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
func subtractOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
||||||
|
@ -10,6 +10,32 @@ import (
|
|||||||
|
|
||||||
type operatorHandler func(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error)
|
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 {
|
func unwrapDoc(node *yaml.Node) *yaml.Node {
|
||||||
if node.Kind == yaml.DocumentNode {
|
if node.Kind == yaml.DocumentNode {
|
||||||
return node.Content[0]
|
return node.Content[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user