2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-11-02 00:20:38 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
|
2021-01-06 09:30:48 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-11-02 00:20:38 +00:00
|
|
|
)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func assignAliasOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-12-22 01:22:59 +00:00
|
|
|
|
|
|
|
log.Debugf("AssignAlias operator!")
|
|
|
|
|
|
|
|
aliasName := ""
|
2021-01-12 23:18:53 +00:00
|
|
|
if !expressionNode.Operation.UpdateAssign {
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
|
2021-01-06 09:30:48 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2021-01-06 09:30:48 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
if rhs.MatchingNodes.Front() != nil {
|
|
|
|
aliasName = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
|
2021-01-06 09:30:48 +00:00
|
|
|
}
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
|
2020-12-22 01:22:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-12-22 01:22:59 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
log.Debugf("Setting aliasName : %v", candidate.GetKey())
|
2021-01-06 09:30:48 +00:00
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
if expressionNode.Operation.UpdateAssign {
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
|
2021-01-06 09:30:48 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2021-01-06 09:30:48 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
if rhs.MatchingNodes.Front() != nil {
|
|
|
|
aliasName = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
|
2021-01-06 09:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-16 04:17:13 +00:00
|
|
|
if aliasName != "" {
|
|
|
|
candidate.Node.Kind = yaml.AliasNode
|
|
|
|
candidate.Node.Value = aliasName
|
|
|
|
}
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context, nil
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func getAliasOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-12-22 01:22:59 +00:00
|
|
|
log.Debugf("GetAlias operator!")
|
|
|
|
var results = list.New()
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-12-22 01:22:59 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
node := &yaml.Node{Kind: yaml.ScalarNode, Value: candidate.Node.Value, Tag: "!!str"}
|
2021-11-23 22:57:35 +00:00
|
|
|
result := candidate.CreateReplacement(node)
|
2021-01-12 08:36:28 +00:00
|
|
|
results.PushBack(result)
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func assignAnchorOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-12-22 01:22:59 +00:00
|
|
|
|
|
|
|
log.Debugf("AssignAnchor operator!")
|
|
|
|
|
|
|
|
anchorName := ""
|
2021-01-12 23:18:53 +00:00
|
|
|
if !expressionNode.Operation.UpdateAssign {
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.ReadOnlyClone(), expressionNode.RHS)
|
2021-01-06 09:30:48 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2021-01-06 09:30:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
if rhs.MatchingNodes.Front() != nil {
|
|
|
|
anchorName = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
|
2021-01-06 09:30:48 +00:00
|
|
|
}
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
lhs, err := d.GetMatchingNodes(context, expressionNode.LHS)
|
2020-12-22 01:22:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := lhs.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-12-22 01:22:59 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
log.Debugf("Setting anchorName of : %v", candidate.GetKey())
|
2021-01-06 09:30:48 +00:00
|
|
|
|
2021-01-12 23:18:53 +00:00
|
|
|
if expressionNode.Operation.UpdateAssign {
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode.RHS)
|
2021-01-06 09:30:48 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2021-01-06 09:30:48 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
if rhs.MatchingNodes.Front() != nil {
|
|
|
|
anchorName = rhs.MatchingNodes.Front().Value.(*CandidateNode).Node.Value
|
2021-01-06 09:30:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 01:22:59 +00:00
|
|
|
candidate.Node.Anchor = anchorName
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context, nil
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func getAnchorOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-12-22 01:22:59 +00:00
|
|
|
log.Debugf("GetAnchor operator!")
|
|
|
|
var results = list.New()
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-12-22 01:22:59 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
anchor := candidate.Node.Anchor
|
|
|
|
node := &yaml.Node{Kind: yaml.ScalarNode, Value: anchor, Tag: "!!str"}
|
2021-11-23 22:57:35 +00:00
|
|
|
result := candidate.CreateReplacement(node)
|
2021-01-12 08:36:28 +00:00
|
|
|
results.PushBack(result)
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2020-12-22 01:22:59 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func explodeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-11-02 00:20:38 +00:00
|
|
|
log.Debugf("-- ExplodeOperation")
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-11-02 00:20:38 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
rhs, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.RHS)
|
2020-11-02 00:20:38 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
for childEl := rhs.MatchingNodes.Front(); childEl != nil; childEl = childEl.Next() {
|
|
|
|
err = explodeNode(childEl.Value.(*CandidateNode).Node, context)
|
2020-11-13 03:07:11 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-11-13 03:07:11 +00:00
|
|
|
}
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
return context, nil
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-10-10 23:41:15 +00:00
|
|
|
func reconstructAliasedMap(node *yaml.Node, context Context) error {
|
|
|
|
var newContent = list.New()
|
|
|
|
// can I short cut here by prechecking if there's an anchor in the map?
|
|
|
|
// no it needs to recurse in overrideEntry.
|
|
|
|
|
|
|
|
for index := 0; index < len(node.Content); index = index + 2 {
|
|
|
|
keyNode := node.Content[index]
|
|
|
|
valueNode := node.Content[index+1]
|
|
|
|
log.Debugf("traversing %v", keyNode.Value)
|
|
|
|
if keyNode.Value != "<<" {
|
|
|
|
err := overrideEntry(node, keyNode, valueNode, index, context.ChildContext(newContent))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if valueNode.Kind == yaml.SequenceNode {
|
|
|
|
log.Debugf("an alias merge list!")
|
|
|
|
for index := len(valueNode.Content) - 1; index >= 0; index = index - 1 {
|
|
|
|
aliasNode := valueNode.Content[index]
|
|
|
|
err := applyAlias(node, aliasNode.Alias, index, context.ChildContext(newContent))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Debugf("an alias merge!")
|
|
|
|
err := applyAlias(node, valueNode.Alias, index, context.ChildContext(newContent))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node.Content = make([]*yaml.Node, newContent.Len())
|
|
|
|
index := 0
|
|
|
|
for newEl := newContent.Front(); newEl != nil; newEl = newEl.Next() {
|
|
|
|
node.Content[index] = newEl.Value.(*yaml.Node)
|
|
|
|
index++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func explodeNode(node *yaml.Node, context Context) error {
|
2020-11-02 00:20:38 +00:00
|
|
|
node.Anchor = ""
|
|
|
|
switch node.Kind {
|
|
|
|
case yaml.SequenceNode, yaml.DocumentNode:
|
|
|
|
for index, contentNode := range node.Content {
|
|
|
|
log.Debugf("exploding index %v", index)
|
2021-02-02 07:17:59 +00:00
|
|
|
errorInContent := explodeNode(contentNode, context)
|
2020-11-02 00:20:38 +00:00
|
|
|
if errorInContent != nil {
|
|
|
|
return errorInContent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case yaml.AliasNode:
|
|
|
|
log.Debugf("its an alias!")
|
|
|
|
if node.Alias != nil {
|
|
|
|
node.Kind = node.Alias.Kind
|
|
|
|
node.Style = node.Alias.Style
|
|
|
|
node.Tag = node.Alias.Tag
|
|
|
|
node.Content = node.Alias.Content
|
|
|
|
node.Value = node.Alias.Value
|
|
|
|
node.Alias = nil
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case yaml.MappingNode:
|
2021-10-10 23:41:15 +00:00
|
|
|
// //check the map has an alias in it
|
|
|
|
hasAlias := false
|
2020-11-02 00:20:38 +00:00
|
|
|
for index := 0; index < len(node.Content); index = index + 2 {
|
|
|
|
keyNode := node.Content[index]
|
2021-10-10 23:41:15 +00:00
|
|
|
if keyNode.Value == "<<" {
|
|
|
|
hasAlias = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasAlias {
|
|
|
|
// this is a slow op, which is why we want to check before running it.
|
|
|
|
return reconstructAliasedMap(node, context)
|
2021-12-20 22:30:08 +00:00
|
|
|
}
|
|
|
|
// this map has no aliases, but it's kids might
|
|
|
|
for index := 0; index < len(node.Content); index = index + 2 {
|
|
|
|
keyNode := node.Content[index]
|
|
|
|
valueNode := node.Content[index+1]
|
|
|
|
err := explodeNode(keyNode, context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = explodeNode(valueNode, context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-12-20 22:30:08 +00:00
|
|
|
return nil
|
2020-11-02 00:20:38 +00:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func applyAlias(node *yaml.Node, alias *yaml.Node, aliasIndex int, newContent Context) error {
|
2020-11-02 00:20:38 +00:00
|
|
|
if alias == nil {
|
2020-11-02 02:43:45 +00:00
|
|
|
return nil
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
|
|
|
for index := 0; index < len(alias.Content); index = index + 2 {
|
|
|
|
keyNode := alias.Content[index]
|
|
|
|
log.Debugf("applying alias key %v", keyNode.Value)
|
|
|
|
valueNode := alias.Content[index+1]
|
2020-11-02 02:43:45 +00:00
|
|
|
err := overrideEntry(node, keyNode, valueNode, aliasIndex, newContent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
2020-11-02 02:43:45 +00:00
|
|
|
return nil
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func overrideEntry(node *yaml.Node, key *yaml.Node, value *yaml.Node, startIndex int, newContent Context) error {
|
2020-11-02 02:43:45 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
err := explodeNode(value, newContent)
|
2020-11-02 02:43:45 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
for newEl := newContent.MatchingNodes.Front(); newEl != nil; newEl = newEl.Next() {
|
2020-11-02 02:43:45 +00:00
|
|
|
valueEl := newEl.Next() // move forward twice
|
|
|
|
keyNode := newEl.Value.(*yaml.Node)
|
|
|
|
log.Debugf("checking new content %v:%v", keyNode.Value, valueEl.Value.(*yaml.Node).Value)
|
|
|
|
if keyNode.Value == key.Value && keyNode.Alias == nil && key.Alias == nil {
|
|
|
|
log.Debugf("overridign new content")
|
|
|
|
valueEl.Value = value
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
newEl = valueEl // move forward twice
|
|
|
|
}
|
|
|
|
|
|
|
|
for index := startIndex + 2; index < len(node.Content); index = index + 2 {
|
2020-11-02 00:20:38 +00:00
|
|
|
keyNode := node.Content[index]
|
2020-11-02 02:43:45 +00:00
|
|
|
|
|
|
|
if keyNode.Value == key.Value && keyNode.Alias == nil {
|
|
|
|
log.Debugf("content will be overridden at index %v", index)
|
|
|
|
return nil
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-02 02:43:45 +00:00
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
err = explodeNode(key, newContent)
|
2020-11-02 02:43:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugf("adding %v:%v", key.Value, value.Value)
|
2021-02-02 07:17:59 +00:00
|
|
|
newContent.MatchingNodes.PushBack(key)
|
|
|
|
newContent.MatchingNodes.PushBack(value)
|
2020-11-02 02:43:45 +00:00
|
|
|
return nil
|
2020-11-02 00:20:38 +00:00
|
|
|
}
|