yq/pkg/yqlib/treeops/operator_create_map.go

44 lines
1.3 KiB
Go
Raw Normal View History

2020-10-21 01:54:58 +00:00
package treeops
import (
"container/list"
"gopkg.in/yaml.v3"
)
func CreateMapOperator(d *dataTreeNavigator, matchingNodes *list.List, pathNode *PathTreeNode) (*list.List, error) {
log.Debugf("-- createMapOperation")
var path []interface{} = nil
var document uint = 0
if matchingNodes.Front() != nil {
sample := matchingNodes.Front().Value.(*CandidateNode)
path = sample.Path
document = sample.Document
}
mapPairs, err := crossFunction(d, matchingNodes, pathNode,
func(d *dataTreeNavigator, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
node := yaml.Node{Kind: yaml.MappingNode, Tag: "!!map"}
2020-10-28 02:00:26 +00:00
log.Debugf("LHS:", NodeToString(lhs))
log.Debugf("RHS:", NodeToString(rhs))
2020-10-21 01:54:58 +00:00
node.Content = []*yaml.Node{
2020-10-28 02:00:26 +00:00
UnwrapDoc(lhs.Node),
UnwrapDoc(rhs.Node),
2020-10-21 01:54:58 +00:00
}
return &CandidateNode{Node: &node, Document: document, Path: path}, nil
})
if err != nil {
return nil, err
}
//wrap up all the pairs into an array
node := yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
for mapPair := mapPairs.Front(); mapPair != nil; mapPair = mapPair.Next() {
mapPairCandidate := mapPair.Value.(*CandidateNode)
log.Debugf("Collecting %v into sequence", NodeToString(mapPairCandidate))
node.Content = append(node.Content, mapPairCandidate.Node)
}
return nodeToMap(&CandidateNode{Node: &node, Document: document, Path: path}), nil
}