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},
|
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:
|
2022-01-23 00:35:44 +00:00
|
|
|
addMaps(target, lhs, rhs)
|
2020-12-21 00:54:03 +00:00
|
|
|
case yaml.SequenceNode:
|
2022-01-23 00:35:44 +00:00
|
|
|
addSequences(target, lhs, rhs)
|
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
|
|
|
|
2022-01-23 00:35:44 +00:00
|
|
|
func addSequences(target *CandidateNode, lhs *CandidateNode, rhs *CandidateNode) {
|
|
|
|
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
|
|
|
|
target.Node.Content = make([]*yaml.Node, len(lhs.Node.Content))
|
|
|
|
copy(target.Node.Content, lhs.Node.Content)
|
|
|
|
target.Node.Content = append(target.Node.Content, toNodes(rhs)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|