2020-11-24 02:07:19 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-01-11 04:43:50 +00:00
|
|
|
"strconv"
|
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},
|
2021-01-11 04:43:50 +00:00
|
|
|
Lhs: lhs,
|
2020-11-28 00:24:16 +00:00
|
|
|
Rhs: rhs}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-21 00:54:03 +00:00
|
|
|
func toNodes(candidate *CandidateNode) []*yaml.Node {
|
2020-11-24 02:07:19 +00:00
|
|
|
if candidate.Node.Tag == "!!null" {
|
|
|
|
return []*yaml.Node{}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch candidate.Node.Kind {
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
return candidate.Node.Content
|
|
|
|
default:
|
|
|
|
return []*yaml.Node{candidate.Node}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
return nil, fmt.Errorf("Maps not yet supported for addition")
|
|
|
|
case yaml.SequenceNode:
|
|
|
|
target.Node.Kind = yaml.SequenceNode
|
|
|
|
target.Node.Style = lhsNode.Style
|
|
|
|
target.Node.Tag = "!!seq"
|
|
|
|
target.Node.Content = append(lhsNode.Content, toNodes(rhs)...)
|
|
|
|
case yaml.ScalarNode:
|
2021-01-11 04:43:50 +00:00
|
|
|
if rhs.Node.Kind != yaml.ScalarNode {
|
2021-11-23 22:57:35 +00:00
|
|
|
return nil, fmt.Errorf("%v (%v) cannot be added to a 2%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
|
|
|
|
return addScalars(target, lhsNode, rhs.Node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return target, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addScalars(target *CandidateNode, lhs *yaml.Node, rhs *yaml.Node) (*CandidateNode, error) {
|
|
|
|
|
|
|
|
if lhs.Tag == "!!str" {
|
|
|
|
target.Node.Tag = "!!str"
|
|
|
|
target.Node.Value = lhs.Value + rhs.Value
|
|
|
|
} else if lhs.Tag == "!!int" && rhs.Tag == "!!int" {
|
2021-09-02 05:26:44 +00:00
|
|
|
format, lhsNum, err := parseInt(lhs.Value)
|
2021-01-11 04:43:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-02 05:26:44 +00:00
|
|
|
_, rhsNum, err := parseInt(rhs.Value)
|
2021-01-11 04:43:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sum := lhsNum + rhsNum
|
|
|
|
target.Node.Tag = "!!int"
|
2021-09-02 05:26:44 +00:00
|
|
|
target.Node.Value = fmt.Sprintf(format, sum)
|
2021-01-11 04:43:50 +00:00
|
|
|
} else if (lhs.Tag == "!!int" || lhs.Tag == "!!float") && (rhs.Tag == "!!int" || rhs.Tag == "!!float") {
|
|
|
|
lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rhsNum, err := strconv.ParseFloat(rhs.Value, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sum := lhsNum + rhsNum
|
|
|
|
target.Node.Tag = "!!float"
|
|
|
|
target.Node.Value = fmt.Sprintf("%v", sum)
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("%v cannot be added to %v", lhs.Tag, rhs.Tag)
|
2020-11-24 02:07:19 +00:00
|
|
|
}
|
2020-12-21 00:54:03 +00:00
|
|
|
|
|
|
|
return target, nil
|
2020-11-24 02:07:19 +00:00
|
|
|
}
|