2021-03-24 21:12:01 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
2022-01-22 02:47:22 +00:00
|
|
|
"strings"
|
2022-02-14 04:37:43 +00:00
|
|
|
"time"
|
2021-03-24 21:12:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func createSubtractOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
|
|
|
|
return &ExpressionNode{Operation: &Operation{OperationType: subtractOpType},
|
2022-02-07 00:55:55 +00:00
|
|
|
LHS: lhs,
|
|
|
|
RHS: rhs}
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-09-29 05:21:11 +00:00
|
|
|
func subtractArray(lhs *CandidateNode, rhs *CandidateNode) []*CandidateNode {
|
2023-10-18 01:11:53 +00:00
|
|
|
newLHSArray := make([]*CandidateNode, 0)
|
2021-09-07 06:58:34 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
for lindex := 0; lindex < len(lhs.Content); lindex = lindex + 1 {
|
2021-09-07 06:58:34 +00:00
|
|
|
shouldInclude := true
|
2023-10-18 01:11:53 +00:00
|
|
|
for rindex := 0; rindex < len(rhs.Content) && shouldInclude; rindex = rindex + 1 {
|
|
|
|
if recursiveNodeEqual(lhs.Content[lindex], rhs.Content[rindex]) {
|
2021-09-07 06:58:34 +00:00
|
|
|
shouldInclude = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldInclude {
|
2023-10-18 01:11:53 +00:00
|
|
|
newLHSArray = append(newLHSArray, lhs.Content[lindex])
|
2021-09-07 06:58:34 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-29 05:21:11 +00:00
|
|
|
return newLHSArray
|
2021-09-07 06:58:34 +00:00
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func subtract(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
2023-10-18 01:11:53 +00:00
|
|
|
if lhs.Tag == "!!null" {
|
|
|
|
return lhs.CopyAsReplacement(rhs), nil
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
target := lhs.CopyWithoutContent()
|
2021-03-24 21:12:01 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
switch lhs.Kind {
|
|
|
|
case MappingNode:
|
2022-02-14 04:37:43 +00:00
|
|
|
return nil, fmt.Errorf("maps not yet supported for subtraction")
|
2023-10-18 01:11:53 +00:00
|
|
|
case SequenceNode:
|
|
|
|
if rhs.Kind != SequenceNode {
|
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Tag, rhs.GetNicePath(), lhs.Tag)
|
2021-09-07 06:58:34 +00:00
|
|
|
}
|
2024-09-29 05:21:11 +00:00
|
|
|
target.Content = subtractArray(lhs, rhs)
|
2023-10-18 01:11:53 +00:00
|
|
|
case ScalarNode:
|
|
|
|
if rhs.Kind != ScalarNode {
|
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be subtracted from %v", rhs.Tag, rhs.GetNicePath(), lhs.Tag)
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Kind = ScalarNode
|
|
|
|
target.Style = lhs.Style
|
|
|
|
if err := subtractScalars(context, target, lhs, rhs); err != nil {
|
2022-02-14 04:37:43 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func subtractScalars(context Context, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) 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
|
2023-10-18 01:11:53 +00:00
|
|
|
lhsTag = lhs.guessTagFromCustomType()
|
2022-01-22 02:47:22 +00:00
|
|
|
lhsIsCustom = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(rhsTag, "!!") {
|
|
|
|
// custom tag - we have to have a guess
|
2023-10-18 01:11:53 +00:00
|
|
|
rhsTag = rhs.guessTagFromCustomType()
|
2022-01-22 02:47:22 +00:00
|
|
|
}
|
2021-03-24 21:12:01 +00:00
|
|
|
|
2022-11-04 01:21:12 +00:00
|
|
|
isDateTime := lhsTag == "!!timestamp"
|
2022-02-14 04:37:43 +00:00
|
|
|
// if the lhs is a string, it might be a timestamp in a custom format.
|
|
|
|
if lhsTag == "!!str" && context.GetDateTimeLayout() != time.RFC3339 {
|
2022-11-04 01:21:12 +00:00
|
|
|
_, err := parseDateTime(context.GetDateTimeLayout(), lhs.Value)
|
2022-02-14 04:37:43 +00:00
|
|
|
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 {
|
2022-02-14 04:37:43 +00:00
|
|
|
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 {
|
2022-02-14 04:37:43 +00:00
|
|
|
return err
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
result := lhsNum - rhsNum
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = lhs.Tag
|
|
|
|
target.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 {
|
2022-02-14 04:37:43 +00:00
|
|
|
return err
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
|
|
|
|
if err != nil {
|
2022-02-14 04:37:43 +00:00
|
|
|
return err
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
result := lhsNum - rhsNum
|
2022-01-22 02:47:22 +00:00
|
|
|
if lhsIsCustom {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = lhs.Tag
|
2022-01-22 02:47:22 +00:00
|
|
|
} else {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = "!!float"
|
2022-01-22 02:47:22 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Value = fmt.Sprintf("%v", result)
|
2021-03-24 21:12:01 +00:00
|
|
|
} else {
|
2022-02-14 04:37:43 +00:00
|
|
|
return fmt.Errorf("%v cannot be added to %v", lhs.Tag, rhs.Tag)
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|
|
|
|
|
2022-02-14 04:37:43 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func subtractDateTime(layout string, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
|
2022-02-14 04:37:43 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-04 01:21:12 +00:00
|
|
|
currentTime, err := parseDateTime(layout, lhs.Value)
|
2022-02-14 04:37:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newTime := currentTime.Add(duration)
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Value = newTime.Format(layout)
|
2022-02-14 04:37:43 +00:00
|
|
|
return nil
|
2021-03-24 21:12:01 +00:00
|
|
|
}
|