yq/pkg/yqlib/operator_add.go

198 lines
5.0 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"
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
}
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:
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-01-23 00:35:44 +00:00
if err := addScalars(target, lhsNode, rhs.Node); err != nil {
return nil, err
}
2021-01-11 04:43:50 +00:00
}
return target, nil
}
2022-01-22 02:17:16 +00:00
func guessTagFromCustomType(node *yaml.Node) string {
2022-01-22 02:47:22 +00:00
if node.Value == "" {
log.Warning("node has no value to guess the type with")
return node.Tag
}
2022-01-22 02:17:16 +00:00
decoder := NewYamlDecoder()
decoder.Init(strings.NewReader(node.Value))
var dataBucket yaml.Node
errorReading := decoder.Decode(&dataBucket)
if errorReading != nil {
2022-01-22 02:47:22 +00:00
log.Warning("could not guess underlying tag type %v", errorReading)
2022-01-22 02:17:16 +00:00
return node.Tag
}
guessedTag := unwrapDoc(&dataBucket).Tag
log.Info("im guessing the tag %v is a %v", node.Tag, guessedTag)
return guessedTag
}
2022-01-23 00:35:44 +00:00
func addScalars(target *CandidateNode, lhs *yaml.Node, rhs *yaml.Node) error {
2022-01-22 02:17:16 +00:00
lhsTag := lhs.Tag
rhsTag := rhs.Tag
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-01-22 02:17:16 +00:00
if !strings.HasPrefix(rhsTag, "!!") {
// custom tag - we have to have a guess
rhsTag = guessTagFromCustomType(rhs)
}
if lhsTag == "!!str" {
target.Node.Tag = lhs.Tag
2021-01-11 04:43:50 +00:00
target.Node.Value = lhs.Value + rhs.Value
2022-01-22 02:17:16 +00:00
} else if lhsTag == "!!int" && rhsTag == "!!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 {
2022-01-23 00:35:44 +00:00
return err
2021-01-11 04:43:50 +00:00
}
2021-09-02 05:26:44 +00:00
_, rhsNum, err := parseInt(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 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
target.Node.Content = make([]*yaml.Node, len(lhs.Node.Content))
copy(target.Node.Content, lhs.Node.Content)
extraNodes, err := toNodes(rhs, lhs)
if err != nil {
return err
}
target.Node.Content = append(target.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 := findInArray(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
}