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
|
|
|
|
|
|
|
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},
|
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
|
|
|
}
|
|
|
|
|
2022-02-03 22:24:48 +00:00
|
|
|
func toNodes(candidate *CandidateNode, lhs *CandidateNode) ([]*yaml.Node, error) {
|
2020-11-24 02:07:19 +00:00
|
|
|
if candidate.Node.Tag == "!!null" {
|
2022-02-03 22:24:48 +00:00
|
|
|
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:
|
2022-02-03 22:24:48 +00:00
|
|
|
return clone.Node.Content, nil
|
2020-11-24 02:07:19 +00:00
|
|
|
default:
|
2022-02-03 22:24:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-02 07:17:59 +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
|
|
|
}
|
|
|
|
|
2021-11-23 22:57:35 +00:00
|
|
|
target := lhs.CreateReplacement(&yaml.Node{})
|
2021-01-18 02:58:46 +00:00
|
|
|
|
2020-12-21 00:54:03 +00:00
|
|
|
switch lhsNode.Kind {
|
|
|
|
case yaml.MappingNode:
|
2022-01-23 00:35:44 +00:00
|
|
|
addMaps(target, lhs, rhs)
|
2020-12-21 00:54:03 +00:00
|
|
|
case yaml.SequenceNode:
|
2022-02-03 22:24:48 +00:00
|
|
|
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 {
|
2022-01-23 00:35:44 +00:00
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be added to a %v", rhs.Node.Tag, rhs.Path, lhsNode.Tag)
|
2021-01-11 04:43:50 +00:00
|
|
|
}
|
|
|
|
target.Node.Kind = yaml.ScalarNode
|
|
|
|
target.Node.Style = lhsNode.Style
|
2022-02-14 04:37:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-14 04:37:43 +00:00
|
|
|
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
|
|
|
|
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 {
|
|
|
|
_, err := time.Parse(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
|
2022-06-16 00:09:17 +00:00
|
|
|
} 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
|
|
|
|
2022-02-14 04:37:43 +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)
|
|
|
|
}
|
|
|
|
|
|
|
|
currentTime, err := time.Parse(layout, lhs.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newTime := currentTime.Add(duration)
|
|
|
|
target.Node.Value = newTime.Format(layout)
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-02-03 22:24:48 +00:00
|
|
|
func addSequences(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) error {
|
2022-01-23 00:35:44 +00:00
|
|
|
target.Node.Kind = yaml.SequenceNode
|
2022-01-26 22:58:13 +00:00
|
|
|
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
|
2022-02-03 22:24:48 +00:00
|
|
|
|
|
|
|
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...)
|
2022-02-03 22:24:48 +00:00
|
|
|
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)
|
2022-04-28 04:45:45 +00:00
|
|
|
indexInLHS := findKeyInMap(target.Node, 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
|
|
|
|
target.Node.Content = append(target.Node.Content, key, value)
|
|
|
|
} else {
|
|
|
|
// it's there, replace it
|
2022-02-07 00:55:55 +00:00
|
|
|
target.Node.Content[indexInLHS+1] = value
|
2022-01-23 00:35:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
target.Node.Kind = yaml.MappingNode
|
2022-01-26 22:58:13 +00:00
|
|
|
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
|
|
|
}
|