2020-11-24 02:07:19 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-01-11 04:43:50 +00:00
|
|
|
"strconv"
|
2022-01-22 02:17:16 +00:00
|
|
|
"strings"
|
2022-02-14 04:37:43 +00:00
|
|
|
"time"
|
2020-11-24 02:07:19 +00:00
|
|
|
)
|
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
func createAddOp(lhs *ExpressionNode, rhs *ExpressionNode) *ExpressionNode {
|
|
|
|
return &ExpressionNode{Operation: &Operation{OperationType: addOpType},
|
2022-02-07 00:55:55 +00:00
|
|
|
LHS: lhs,
|
|
|
|
RHS: rhs}
|
2020-11-28 00:24:16 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func addAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2021-07-07 10:00:46 +00:00
|
|
|
return compoundAssignFunction(d, context, expressionNode, createAddOp)
|
2020-11-27 23:41:09 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func toNodes(candidate *CandidateNode, lhs *CandidateNode) []*CandidateNode {
|
|
|
|
if candidate.Tag == "!!null" {
|
|
|
|
return []*CandidateNode{}
|
2020-11-24 02:07:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
clone := candidate.Copy()
|
|
|
|
|
|
|
|
switch candidate.Kind {
|
|
|
|
case SequenceNode:
|
|
|
|
return clone.Content
|
2020-11-24 02:07:19 +00:00
|
|
|
default:
|
2023-10-18 01:11:53 +00:00
|
|
|
if len(lhs.Content) > 0 {
|
|
|
|
clone.Style = lhs.Content[0].Style
|
2022-02-03 22:24:48 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
return []*CandidateNode{clone}
|
2020-11-24 02:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func addOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-11-24 02:07:19 +00:00
|
|
|
log.Debugf("Add operator")
|
|
|
|
|
2021-05-16 04:36:13 +00:00
|
|
|
return crossFunction(d, context.ReadOnlyClone(), expressionNode, add, false)
|
2020-12-21 00:54:03 +00:00
|
|
|
}
|
2020-11-24 02:07:19 +00:00
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func add(_ *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
|
2023-10-18 01:11:53 +00:00
|
|
|
lhsNode := lhs
|
2020-11-24 02:07:19 +00:00
|
|
|
|
2021-01-18 02:58:46 +00:00
|
|
|
if lhsNode.Tag == "!!null" {
|
2023-10-18 01:11:53 +00:00
|
|
|
return lhs.CopyAsReplacement(rhs), nil
|
2021-01-18 02:58:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
target := lhs.CopyWithoutContent()
|
2021-01-18 02:58:46 +00:00
|
|
|
|
2020-12-21 00:54:03 +00:00
|
|
|
switch lhsNode.Kind {
|
2023-10-18 01:11:53 +00:00
|
|
|
case MappingNode:
|
|
|
|
if rhs.Kind != MappingNode {
|
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be added to a %v (%v)", rhs.Tag, rhs.GetNicePath(), lhsNode.Tag, lhs.GetNicePath())
|
2022-09-16 00:04:48 +00:00
|
|
|
}
|
2022-01-23 00:35:44 +00:00
|
|
|
addMaps(target, lhs, rhs)
|
2023-10-18 01:11:53 +00:00
|
|
|
case SequenceNode:
|
|
|
|
addSequences(target, lhs, rhs)
|
|
|
|
case ScalarNode:
|
|
|
|
if rhs.Kind != ScalarNode {
|
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be added to a %v (%v)", rhs.Tag, rhs.GetNicePath(), lhsNode.Tag, lhs.GetNicePath())
|
2021-01-11 04:43:50 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Kind = ScalarNode
|
|
|
|
target.Style = lhsNode.Style
|
|
|
|
if err := addScalars(context, target, lhsNode, rhs); err != nil {
|
2022-01-23 00:35:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 04:43:50 +00:00
|
|
|
}
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func addScalars(context Context, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
|
2022-01-22 02:17:16 +00:00
|
|
|
lhsTag := lhs.Tag
|
2024-03-12 04:45:08 +00:00
|
|
|
rhsTag := rhs.GuessTagFromCustomType()
|
2022-01-22 02:17:16 +00:00
|
|
|
lhsIsCustom := false
|
|
|
|
if !strings.HasPrefix(lhsTag, "!!") {
|
|
|
|
// custom tag - we have to have a guess
|
2024-03-12 04:45:08 +00:00
|
|
|
lhsTag = lhs.GuessTagFromCustomType()
|
2022-01-22 02:17:16 +00:00
|
|
|
lhsIsCustom = true
|
|
|
|
}
|
2021-01-11 04:43:50 +00:00
|
|
|
|
2022-02-14 04:37:43 +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 {
|
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 addDateTimes(context.GetDateTimeLayout(), target, lhs, rhs)
|
|
|
|
|
|
|
|
} else if lhsTag == "!!str" {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = lhs.Tag
|
2023-08-29 23:46:45 +00:00
|
|
|
if rhsTag == "!!null" {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Value = lhs.Value
|
2023-08-29 23:46:45 +00:00
|
|
|
} else {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Value = lhs.Value + rhs.Value
|
2023-08-29 23:46:45 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
|
2022-06-16 00:09:17 +00:00
|
|
|
} else if rhsTag == "!!str" {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = rhs.Tag
|
|
|
|
target.Value = lhs.Value + rhs.Value
|
2022-01-22 02:17:16 +00:00
|
|
|
} else if lhsTag == "!!int" && rhsTag == "!!int" {
|
2022-05-06 03:46:14 +00:00
|
|
|
format, lhsNum, err := parseInt64(lhs.Value)
|
2021-01-11 04:43:50 +00:00
|
|
|
if err != nil {
|
2022-01-23 00:35:44 +00:00
|
|
|
return err
|
2021-01-11 04:43:50 +00:00
|
|
|
}
|
2022-05-06 03:46:14 +00:00
|
|
|
_, rhsNum, err := parseInt64(rhs.Value)
|
2021-01-11 04:43:50 +00:00
|
|
|
if err != nil {
|
2022-01-23 00:35:44 +00:00
|
|
|
return err
|
2021-01-11 04:43:50 +00:00
|
|
|
}
|
|
|
|
sum := lhsNum + rhsNum
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = lhs.Tag
|
|
|
|
target.Value = fmt.Sprintf(format, sum)
|
2022-01-22 02:17:16 +00:00
|
|
|
} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
|
2021-01-11 04:43:50 +00:00
|
|
|
lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
|
|
|
|
if err != nil {
|
2022-01-23 00:35:44 +00:00
|
|
|
return err
|
2021-01-11 04:43:50 +00:00
|
|
|
}
|
|
|
|
rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
|
|
|
|
if err != nil {
|
2022-01-23 00:35:44 +00:00
|
|
|
return err
|
2021-01-11 04:43:50 +00:00
|
|
|
}
|
|
|
|
sum := lhsNum + rhsNum
|
2022-01-22 02:17:16 +00:00
|
|
|
if lhsIsCustom {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = lhs.Tag
|
2022-01-22 02:17:16 +00:00
|
|
|
} else {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = "!!float"
|
2022-01-22 02:17:16 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Value = fmt.Sprintf("%v", sum)
|
2021-01-11 04:43:50 +00:00
|
|
|
} else {
|
2022-01-23 00:35:44 +00:00
|
|
|
return fmt.Errorf("%v cannot be added to %v", lhsTag, rhsTag)
|
2020-11-24 02:07:19 +00:00
|
|
|
}
|
2022-01-23 00:35:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-21 00:54:03 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func addDateTimes(layout string, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
|
2022-02-14 04:37:43 +00:00
|
|
|
|
|
|
|
duration, err := time.ParseDuration(rhs.Value)
|
|
|
|
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
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func addSequences(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) {
|
|
|
|
log.Debugf("adding sequences! target: %v; lhs %v; rhs: %v", NodeToString(target), NodeToString(lhs), NodeToString(rhs))
|
|
|
|
target.Kind = SequenceNode
|
|
|
|
if len(lhs.Content) == 0 {
|
|
|
|
log.Debugf("dont copy lhs style")
|
|
|
|
target.Style = 0
|
2022-02-03 22:24:48 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = lhs.Tag
|
2022-02-03 22:24:48 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
extraNodes := toNodes(rhs, lhs)
|
2022-02-03 22:24:48 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
target.AddChildren(lhs.Content)
|
|
|
|
target.AddChildren(extraNodes)
|
2022-01-23 00:35:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addMaps(target *CandidateNode, lhsC *CandidateNode, rhsC *CandidateNode) {
|
2023-10-18 01:11:53 +00:00
|
|
|
lhs := lhsC
|
|
|
|
rhs := rhsC
|
|
|
|
|
|
|
|
if len(lhs.Content) == 0 {
|
|
|
|
log.Debugf("dont copy lhs style")
|
|
|
|
target.Style = 0
|
|
|
|
}
|
2022-01-23 00:35:44 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Content = make([]*CandidateNode, 0)
|
|
|
|
target.AddChildren(lhs.Content)
|
2022-01-23 00:35:44 +00:00
|
|
|
|
|
|
|
for index := 0; index < len(rhs.Content); index = index + 2 {
|
|
|
|
key := rhs.Content[index]
|
|
|
|
value := rhs.Content[index+1]
|
|
|
|
log.Debug("finding %v", key.Value)
|
2023-10-18 01:11:53 +00:00
|
|
|
indexInLHS := findKeyInMap(target, key)
|
2022-02-07 00:55:55 +00:00
|
|
|
log.Debug("indexInLhs %v", indexInLHS)
|
|
|
|
if indexInLHS < 0 {
|
2022-01-23 00:35:44 +00:00
|
|
|
// not in there, append it
|
2023-10-18 01:11:53 +00:00
|
|
|
target.AddKeyValueChild(key, value)
|
2022-01-23 00:35:44 +00:00
|
|
|
} else {
|
|
|
|
// it's there, replace it
|
2023-10-18 01:11:53 +00:00
|
|
|
oldValue := target.Content[indexInLHS+1]
|
|
|
|
newValueCopy := oldValue.CopyAsReplacement(value)
|
|
|
|
target.Content[indexInLHS+1] = newValueCopy
|
2022-01-23 00:35:44 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Kind = MappingNode
|
2022-01-26 22:58:13 +00:00
|
|
|
if len(lhs.Content) > 0 {
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Style = lhs.Style
|
2022-01-26 22:58:13 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
target.Tag = lhs.Tag
|
2020-11-24 02:07:19 +00:00
|
|
|
}
|