yq/pkg/yqlib/operator_anchors_aliases.go

262 lines
6.8 KiB
Go
Raw Normal View History

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-01-12 23:18:53 +00:00
func assignAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) {
log.Debugf("AssignAlias operator!")
aliasName := ""
2021-01-12 23:18:53 +00:00
if !expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs)
2021-01-06 09:30:48 +00:00
if err != nil {
return nil, err
}
if rhs.Front() != nil {
aliasName = rhs.Front().Value.(*CandidateNode).Node.Value
}
}
2021-01-12 23:18:53 +00:00
lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs)
if err != nil {
return nil, err
}
for el := lhs.Front(); el != nil; el = el.Next() {
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 {
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs)
2021-01-06 09:30:48 +00:00
if err != nil {
return nil, err
}
if rhs.Front() != nil {
aliasName = rhs.Front().Value.(*CandidateNode).Node.Value
}
}
candidate.Node.Kind = yaml.AliasNode
candidate.Node.Value = aliasName
}
return matchingNodes, nil
}
2021-01-12 23:18:53 +00:00
func getAliasOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) {
log.Debugf("GetAlias operator!")
var results = list.New()
for el := matchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
node := &yaml.Node{Kind: yaml.ScalarNode, Value: candidate.Node.Value, Tag: "!!str"}
result := candidate.CreateChild(nil, node)
results.PushBack(result)
}
return results, nil
}
2021-01-12 23:18:53 +00:00
func assignAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) {
log.Debugf("AssignAnchor operator!")
anchorName := ""
2021-01-12 23:18:53 +00:00
if !expressionNode.Operation.UpdateAssign {
rhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Rhs)
2021-01-06 09:30:48 +00:00
if err != nil {
return nil, err
}
if rhs.Front() != nil {
anchorName = rhs.Front().Value.(*CandidateNode).Node.Value
}
}
2021-01-12 23:18:53 +00:00
lhs, err := d.GetMatchingNodes(matchingNodes, expressionNode.Lhs)
if err != nil {
return nil, err
}
for el := lhs.Front(); el != nil; el = el.Next() {
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 {
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs)
2021-01-06 09:30:48 +00:00
if err != nil {
return nil, err
}
if rhs.Front() != nil {
anchorName = rhs.Front().Value.(*CandidateNode).Node.Value
}
}
candidate.Node.Anchor = anchorName
}
return matchingNodes, nil
}
2021-01-12 23:18:53 +00:00
func getAnchorOperator(d *dataTreeNavigator, matchingNodes *list.List, expressionNode *ExpressionNode) (*list.List, error) {
log.Debugf("GetAnchor operator!")
var results = list.New()
for el := matchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
anchor := candidate.Node.Anchor
node := &yaml.Node{Kind: yaml.ScalarNode, Value: anchor, Tag: "!!str"}
result := candidate.CreateChild(nil, node)
results.PushBack(result)
}
return results, nil
}
2021-01-12 23:18:53 +00:00
func explodeOperator(d *dataTreeNavigator, matchMap *list.List, expressionNode *ExpressionNode) (*list.List, error) {
2020-11-02 00:20:38 +00:00
log.Debugf("-- ExplodeOperation")
for el := matchMap.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
2021-01-12 23:18:53 +00:00
rhs, err := d.GetMatchingNodes(nodeToMap(candidate), expressionNode.Rhs)
2020-11-02 00:20:38 +00:00
if err != nil {
return nil, err
}
for childEl := rhs.Front(); childEl != nil; childEl = childEl.Next() {
2020-11-13 03:07:11 +00:00
err = explodeNode(childEl.Value.(*CandidateNode).Node)
if err != nil {
return nil, err
}
2020-11-02 00:20:38 +00:00
}
}
return matchMap, nil
}
func explodeNode(node *yaml.Node) error {
node.Anchor = ""
switch node.Kind {
case yaml.SequenceNode, yaml.DocumentNode:
for index, contentNode := range node.Content {
log.Debugf("exploding index %v", index)
errorInContent := explodeNode(contentNode)
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:
2020-11-02 02:43:45 +00:00
var newContent = list.New()
2020-11-02 00:20:38 +00:00
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 != "<<" {
2020-11-02 02:43:45 +00:00
err := overrideEntry(node, keyNode, valueNode, index, newContent)
if err != nil {
return err
2020-11-02 00:20:38 +00:00
}
} else {
if valueNode.Kind == yaml.SequenceNode {
log.Debugf("an alias merge list!")
2020-11-02 02:43:45 +00:00
for index := 0; index < len(valueNode.Content); index = index + 1 {
2020-11-02 00:20:38 +00:00
aliasNode := valueNode.Content[index]
2020-11-13 03:07:11 +00:00
err := applyAlias(node, aliasNode.Alias, index, newContent)
if err != nil {
return err
}
2020-11-02 00:20:38 +00:00
}
} else {
log.Debugf("an alias merge!")
2020-11-13 03:07:11 +00:00
err := applyAlias(node, valueNode.Alias, index, newContent)
if err != nil {
return err
}
2020-11-02 00:20:38 +00:00
}
}
}
2020-11-02 02:43:45 +00:00
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++
}
2020-11-02 00:20:38 +00:00
return nil
default:
return nil
}
}
2020-11-02 02:43:45 +00:00
func applyAlias(node *yaml.Node, alias *yaml.Node, aliasIndex int, newContent *list.List) 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
}
2020-11-02 02:43:45 +00:00
func overrideEntry(node *yaml.Node, key *yaml.Node, value *yaml.Node, startIndex int, newContent *list.List) error {
err := explodeNode(value)
if err != nil {
return err
}
for newEl := newContent.Front(); newEl != nil; newEl = newEl.Next() {
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
err = explodeNode(key)
if err != nil {
return err
}
log.Debugf("adding %v:%v", key.Value, value.Value)
newContent.PushBack(key)
newContent.PushBack(value)
return nil
2020-11-02 00:20:38 +00:00
}