2021-03-24 21:12:01 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"strconv"
|
|
|
|
|
2021-09-07 06:58:34 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
2021-03-24 21:12:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func createSubtractOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
|
|
|
|
return &ExpressionNode{Operation: &Operation{OperationType: subtractOpType},
|
|
|
|
Lhs: lhs,
|
|
|
|
Rhs: rhs}
|
|
|
|
}
|
|
|
|
|
|
|
|
func subtractAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2021-07-07 10:00:46 +00:00
|
|
|
return compoundAssignFunction(d, context, expressionNode, createSubtractOp)
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func subtractOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
log.Debugf("Subtract operator")
|
|
|
|
|
2021-05-16 04:36:13 +00:00
|
|
|
return crossFunction(d, context.ReadOnlyClone(), expressionNode, subtract, false)
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-07 06:58:34 +00:00
|
|
|
func subtractArray(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
newLhsArray := make([]*yaml.Node, 0)
|
|
|
|
|
|
|
|
for lindex := 0; lindex < len(lhs.Node.Content); lindex = lindex + 1 {
|
|
|
|
shouldInclude := true
|
|
|
|
for rindex := 0; rindex < len(rhs.Node.Content) && shouldInclude; rindex = rindex + 1 {
|
|
|
|
if recursiveNodeEqual(lhs.Node.Content[lindex], rhs.Node.Content[rindex]) {
|
|
|
|
shouldInclude = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldInclude {
|
|
|
|
newLhsArray = append(newLhsArray, lhs.Node.Content[lindex])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lhs.Node.Content = newLhsArray
|
|
|
|
return lhs, nil
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:12:01 +00:00
|
|
|
func subtract(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
|
|
|
lhs.Node = unwrapDoc(lhs.Node)
|
|
|
|
rhs.Node = unwrapDoc(rhs.Node)
|
|
|
|
|
|
|
|
lhsNode := lhs.Node
|
|
|
|
|
|
|
|
if lhsNode.Tag == "!!null" {
|
|
|
|
return lhs.CreateChild(nil, rhs.Node), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target := lhs.CreateChild(nil, &yaml.Node{})
|
|
|
|
|
|
|
|
switch lhsNode.Kind {
|
|
|
|
case yaml.MappingNode:
|
|
|
|
return nil, fmt.Errorf("Maps not yet supported for subtraction")
|
|
|
|
case yaml.SequenceNode:
|
2021-09-07 06:58:34 +00:00
|
|
|
if rhs.Node.Kind != yaml.SequenceNode {
|
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Node.Tag, rhs.Path, lhsNode.Tag)
|
|
|
|
}
|
|
|
|
return subtractArray(d, context, lhs, rhs)
|
2021-03-24 21:12:01 +00:00
|
|
|
case yaml.ScalarNode:
|
|
|
|
if rhs.Node.Kind != yaml.ScalarNode {
|
2021-09-07 06:58:34 +00:00
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Node.Tag, rhs.Path, lhsNode.Tag)
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
target.Node.Kind = yaml.ScalarNode
|
|
|
|
target.Node.Style = lhsNode.Style
|
|
|
|
return subtractScalars(target, lhsNode, rhs.Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func subtractScalars(target *CandidateNode, lhs *yaml.Node, rhs *yaml.Node) (*CandidateNode, error) {
|
|
|
|
|
|
|
|
if lhs.Tag == "!!str" {
|
|
|
|
return nil, fmt.Errorf("strings cannot be subtracted")
|
|
|
|
} else if lhs.Tag == "!!int" && rhs.Tag == "!!int" {
|
2021-09-02 05:26:44 +00:00
|
|
|
format, lhsNum, err := parseInt(lhs.Value)
|
2021-03-24 21:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-02 05:26:44 +00:00
|
|
|
_, rhsNum, err := parseInt(rhs.Value)
|
2021-03-24 21:12:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := lhsNum - rhsNum
|
|
|
|
target.Node.Tag = "!!int"
|
2021-09-02 05:26:44 +00:00
|
|
|
target.Node.Value = fmt.Sprintf(format, result)
|
2021-03-24 21:12:01 +00:00
|
|
|
} else if (lhs.Tag == "!!int" || lhs.Tag == "!!float") && (rhs.Tag == "!!int" || rhs.Tag == "!!float") {
|
|
|
|
lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result := lhsNum - rhsNum
|
|
|
|
target.Node.Tag = "!!float"
|
|
|
|
target.Node.Value = fmt.Sprintf("%v", result)
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("%v cannot be added to %v", lhs.Tag, rhs.Tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
|
|
|
}
|