yq/pkg/yqlib/operator_add.go

210 lines
5.5 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
yaml "gopkg.in/yaml.v3"
)
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) ([]*yaml.Node, error) {
2020-11-24 02:07:19 +00:00
if candidate.Node.Tag == "!!null" {
return []*yaml.Node{}, nil
}
clone, err := candidate.Copy()
if err != nil {
return nil, err
2020-11-24 02:07:19 +00:00
}
switch candidate.Node.Kind {
case yaml.SequenceNode:
return clone.Node.Content, nil
2020-11-24 02:07:19 +00:00
default:
if len(lhs.Node.Content) > 0 {
clone.Node.Style = lhs.Node.Content[0].Style
}
return []*yaml.Node{clone.Node}, nil
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
func add(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
2021-01-12 23:00:51 +00:00
lhs.Node = unwrapDoc(lhs.Node)
rhs.Node = unwrapDoc(rhs.Node)
2020-11-24 02:07:19 +00:00
2020-12-21 00:54:03 +00:00
lhsNode := lhs.Node
2020-11-24 02:07:19 +00:00
2021-01-18 02:58:46 +00:00
if lhsNode.Tag == "!!null" {
2021-11-23 22:57:35 +00:00
return lhs.CreateReplacement(rhs.Node), nil
2021-01-18 02:58:46 +00:00
}
target := lhs.CreateReplacement(&yaml.Node{
Anchor: lhs.Node.Anchor,
})
2021-01-18 02:58:46 +00:00
2020-12-21 00:54:03 +00:00
switch lhsNode.Kind {
case yaml.MappingNode:
if rhs.Node.Kind != yaml.MappingNode {
return nil, fmt.Errorf("%v (%v) cannot be added to a %v (%v)", rhs.Node.Tag, rhs.GetNicePath(), lhsNode.Tag, lhs.GetNicePath())
}
2022-01-23 00:35:44 +00:00
addMaps(target, lhs, rhs)
2020-12-21 00:54:03 +00:00
case yaml.SequenceNode:
if err := addSequences(target, lhs, rhs); err != nil {
return nil, err
}
2020-12-21 00:54:03 +00:00
case yaml.ScalarNode:
2021-01-11 04:43:50 +00:00
if rhs.Node.Kind != yaml.ScalarNode {
return nil, fmt.Errorf("%v (%v) cannot be added to a %v (%v)", rhs.Node.Tag, rhs.GetNicePath(), lhsNode.Tag, lhs.GetNicePath())
2021-01-11 04:43:50 +00:00
}
target.Node.Kind = yaml.ScalarNode
target.Node.Style = lhsNode.Style
if err := addScalars(context, target, lhsNode, rhs.Node); 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 *yaml.Node, rhs *yaml.Node) error {
2022-01-22 02:17:16 +00:00
lhsTag := lhs.Tag
2022-02-22 03:50:45 +00:00
rhsTag := guessTagFromCustomType(rhs)
2022-01-22 02:17:16 +00:00
lhsIsCustom := false
if !strings.HasPrefix(lhsTag, "!!") {
// custom tag - we have to have a guess
lhsTag = guessTagFromCustomType(lhs)
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" {
2022-01-22 02:17:16 +00:00
target.Node.Tag = lhs.Tag
2021-01-11 04:43:50 +00:00
target.Node.Value = lhs.Value + rhs.Value
} else if rhsTag == "!!str" {
target.Node.Tag = rhs.Tag
target.Node.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
2022-01-22 02:17:16 +00:00
target.Node.Tag = lhs.Tag
2021-09-02 05:26:44 +00:00
target.Node.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.Node.Tag = lhs.Tag
} else {
target.Node.Tag = "!!float"
}
2021-01-11 04:43:50 +00:00
target.Node.Value = fmt.Sprintf("%v", sum)
} 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 *yaml.Node, rhs *yaml.Node) 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.Node.Value = newTime.Format(layout)
return nil
}
func addSequences(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
2022-01-23 00:35:44 +00:00
target.Node.Kind = yaml.SequenceNode
if len(lhs.Node.Content) > 0 {
target.Node.Style = lhs.Node.Style
}
2022-01-23 00:35:44 +00:00
target.Node.Tag = lhs.Node.Tag
extraNodes, err := toNodes(rhs, lhs)
if err != nil {
return err
}
2022-03-19 07:42:12 +00:00
target.Node.Content = append(deepCloneContent(lhs.Node.Content), extraNodes...)
return nil
2022-01-23 00:35:44 +00:00
}
func addMaps(target *CandidateNode, lhsC *CandidateNode, rhsC *CandidateNode) {
lhs := lhsC.Node
rhs := rhsC.Node
target.Node.Content = make([]*yaml.Node, len(lhs.Content))
copy(target.Node.Content, lhs.Content)
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.Node, key)
log.Debug("indexInLhs %v", indexInLHS)
if indexInLHS < 0 {
2022-01-23 00:35:44 +00:00
// not in there, append it
target.Node.Content = append(target.Node.Content, key, value)
} else {
// it's there, replace it
target.Node.Content[indexInLHS+1] = value
2022-01-23 00:35:44 +00:00
}
}
target.Node.Kind = yaml.MappingNode
if len(lhs.Content) > 0 {
target.Node.Style = lhs.Style
}
2022-01-23 00:35:44 +00:00
target.Node.Tag = lhs.Tag
2020-11-24 02:07:19 +00:00
}