yq/pkg/yqlib/operator_subtract.go

163 lines
4.5 KiB
Go
Raw Permalink Normal View History

2021-03-24 21:12:01 +00:00
package yqlib
import (
"fmt"
"strconv"
2022-01-22 02:47:22 +00:00
"strings"
"time"
2021-03-24 21:12:01 +00:00
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}
2021-03-24 21:12:01 +00:00
}
func subtractAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
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")
return crossFunction(d, context.ReadOnlyClone(), expressionNode, subtract, false)
2021-03-24 21:12:01 +00:00
}
func subtractArray(lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
newLHSArray := make([]*yaml.Node, 0)
2021-09-07 06:58:34 +00:00
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])
2021-09-07 06:58:34 +00:00
}
}
lhs.Node.Content = newLHSArray
2021-09-07 06:58:34 +00:00
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" {
2021-11-23 22:57:35 +00:00
return lhs.CreateReplacement(rhs.Node), nil
2021-03-24 21:12:01 +00:00
}
2021-11-23 22:57:35 +00:00
target := lhs.CreateReplacement(&yaml.Node{})
2021-03-24 21:12:01 +00:00
switch lhsNode.Kind {
case yaml.MappingNode:
return nil, fmt.Errorf("maps not yet supported for subtraction")
2021-03-24 21:12:01 +00:00
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(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
if err := subtractScalars(context, target, lhsNode, rhs.Node); err != nil {
return nil, err
}
2021-03-24 21:12:01 +00:00
}
return target, nil
}
func subtractScalars(context Context, target *CandidateNode, lhs *yaml.Node, rhs *yaml.Node) error {
2022-01-22 02:47:22 +00:00
lhsTag := lhs.Tag
rhsTag := rhs.Tag
lhsIsCustom := false
if !strings.HasPrefix(lhsTag, "!!") {
// custom tag - we have to have a guess
lhsTag = guessTagFromCustomType(lhs)
lhsIsCustom = true
}
if !strings.HasPrefix(rhsTag, "!!") {
// custom tag - we have to have a guess
rhsTag = guessTagFromCustomType(rhs)
}
2021-03-24 21:12:01 +00:00
isDateTime := lhs.Tag == "!!timestamp"
// if the lhs is a string, it might be a timestamp in a custom format.
if lhsTag == "!!str" && context.GetDateTimeLayout() != time.RFC3339 {
_, err := time.Parse(context.GetDateTimeLayout(), lhs.Value)
isDateTime = err == nil
}
if isDateTime {
return subtractDateTime(context.GetDateTimeLayout(), target, lhs, rhs)
} else if lhsTag == "!!str" {
return fmt.Errorf("strings cannot be subtracted")
2022-01-22 02:47:22 +00:00
} else if lhsTag == "!!int" && rhsTag == "!!int" {
2022-05-06 03:46:14 +00:00
format, lhsNum, err := parseInt64(lhs.Value)
2021-03-24 21:12:01 +00:00
if err != nil {
return err
2021-03-24 21:12:01 +00:00
}
2022-05-06 03:46:14 +00:00
_, rhsNum, err := parseInt64(rhs.Value)
2021-03-24 21:12:01 +00:00
if err != nil {
return err
2021-03-24 21:12:01 +00:00
}
result := lhsNum - rhsNum
2022-01-22 02:47:22 +00:00
target.Node.Tag = lhs.Tag
2021-09-02 05:26:44 +00:00
target.Node.Value = fmt.Sprintf(format, result)
2022-01-22 02:47:22 +00:00
} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
2021-03-24 21:12:01 +00:00
lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
if err != nil {
return err
2021-03-24 21:12:01 +00:00
}
rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
if err != nil {
return err
2021-03-24 21:12:01 +00:00
}
result := lhsNum - rhsNum
2022-01-22 02:47:22 +00:00
if lhsIsCustom {
target.Node.Tag = lhs.Tag
} else {
target.Node.Tag = "!!float"
}
2021-03-24 21:12:01 +00:00
target.Node.Value = fmt.Sprintf("%v", result)
} else {
return fmt.Errorf("%v cannot be added to %v", lhs.Tag, rhs.Tag)
2021-03-24 21:12:01 +00:00
}
return nil
}
func subtractDateTime(layout string, target *CandidateNode, lhs *yaml.Node, rhs *yaml.Node) error {
var durationStr string
if strings.HasPrefix(rhs.Value, "-") {
durationStr = rhs.Value[1:]
} else {
durationStr = "-" + rhs.Value
}
duration, err := time.ParseDuration(durationStr)
if err != nil {
return fmt.Errorf("unable to parse duration [%v]: %w", rhs.Value, err)
}
currentTime, err := time.Parse(layout, lhs.Value)
if err != nil {
return err
}
newTime := currentTime.Add(duration)
target.Node.Value = newTime.Format(layout)
return nil
2021-03-24 21:12:01 +00:00
}