yq/pkg/yqlib/operator_add.go

208 lines
5.3 KiB
Go
Raw Normal View History

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"
"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},
LHS: lhs,
RHS: rhs}
}
func addAssignOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
return compoundAssignFunction(d, context, expressionNode, createAddOp)
2020-11-27 23:41:09 +00:00
}
func toNodes(candidate *CandidateNode, lhs *CandidateNode) []*CandidateNode {
if candidate.Tag == "!!null" {
return []*CandidateNode{}
2020-11-24 02:07:19 +00:00
}
clone := candidate.Copy()
switch candidate.Kind {
case SequenceNode:
return clone.Content
2020-11-24 02:07:19 +00:00
default:
if len(lhs.Content) > 0 {
clone.Style = lhs.Content[0].Style
}
return []*CandidateNode{clone}
2020-11-24 02:07:19 +00:00
}
}
func addOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2020-11-24 02:07:19 +00:00
log.Debugf("Add operator")
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) {
lhsNode := lhs
2020-11-24 02:07:19 +00:00
2021-01-18 02:58:46 +00:00
if lhsNode.Tag == "!!null" {
return lhs.CopyAsReplacement(rhs), nil
2021-01-18 02:58:46 +00:00
}
target := lhs.CopyWithoutContent()
2021-01-18 02:58:46 +00:00
2020-12-21 00:54:03 +00:00
switch lhsNode.Kind {
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-01-23 00:35:44 +00:00
addMaps(target, lhs, rhs)
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
}
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
}
func addScalars(context Context, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
2022-01-22 02:17:16 +00:00
lhsTag := lhs.Tag
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
lhsTag = lhs.guessTagFromCustomType()
2022-01-22 02:17:16 +00:00
lhsIsCustom = true
}
2021-01-11 04:43:50 +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)
isDateTime = err == nil
}
if isDateTime {
return addDateTimes(context.GetDateTimeLayout(), target, lhs, rhs)
} else if lhsTag == "!!str" {
target.Tag = lhs.Tag
if rhsTag == "!!null" {
target.Value = lhs.Value
} else {
target.Value = lhs.Value + rhs.Value
}
} else if rhsTag == "!!str" {
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
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 {
target.Tag = lhs.Tag
2022-01-22 02:17:16 +00:00
} else {
target.Tag = "!!float"
2022-01-22 02:17:16 +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
func addDateTimes(layout string, target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
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)
if err != nil {
return err
}
newTime := currentTime.Add(duration)
target.Value = newTime.Format(layout)
return nil
}
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
}
target.Tag = lhs.Tag
extraNodes := toNodes(rhs, lhs)
target.AddChildren(lhs.Content)
target.AddChildren(extraNodes)
2022-01-23 00:35:44 +00:00
}
func addMaps(target *CandidateNode, lhsC *CandidateNode, rhsC *CandidateNode) {
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
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)
indexInLHS := findKeyInMap(target, key)
log.Debug("indexInLhs %v", indexInLHS)
if indexInLHS < 0 {
2022-01-23 00:35:44 +00:00
// not in there, append it
target.AddKeyValueChild(key, value)
2022-01-23 00:35:44 +00:00
} else {
// it's there, replace it
oldValue := target.Content[indexInLHS+1]
newValueCopy := oldValue.CopyAsReplacement(value)
target.Content[indexInLHS+1] = newValueCopy
2022-01-23 00:35:44 +00:00
}
}
target.Kind = MappingNode
if len(lhs.Content) > 0 {
target.Style = lhs.Style
}
target.Tag = lhs.Tag
2020-11-24 02:07:19 +00:00
}