yq/pkg/yqlib/operator_create_map.go

86 lines
2.6 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-21 01:54:58 +00:00
import (
"container/list"
"gopkg.in/yaml.v3"
)
func createMapOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2020-10-21 01:54:58 +00:00
log.Debugf("-- createMapOperation")
2020-11-13 02:19:54 +00:00
//each matchingNodes entry should turn into a sequence of keys to create.
//then collect object should do a cross function of the same index sequence for all matches.
var path []interface{}
var document uint
2020-11-13 02:19:54 +00:00
sequences := list.New()
if context.MatchingNodes.Len() > 0 {
2020-11-13 02:19:54 +00:00
for matchingNodeEl := context.MatchingNodes.Front(); matchingNodeEl != nil; matchingNodeEl = matchingNodeEl.Next() {
2020-11-13 02:19:54 +00:00
matchingNode := matchingNodeEl.Value.(*CandidateNode)
sequenceNode, err := sequenceFor(d, context, matchingNode, expressionNode)
2020-11-13 02:19:54 +00:00
if err != nil {
return Context{}, err
2020-11-13 02:19:54 +00:00
}
sequences.PushBack(sequenceNode)
}
} else {
sequenceNode, err := sequenceFor(d, context, nil, expressionNode)
2020-11-13 02:19:54 +00:00
if err != nil {
return Context{}, err
2020-11-13 02:19:54 +00:00
}
sequences.PushBack(sequenceNode)
}
return context.SingleChildContext(&CandidateNode{Node: listToNodeSeq(sequences), Document: document, Path: path}), nil
2020-11-13 02:19:54 +00:00
}
func sequenceFor(d *dataTreeNavigator, context Context, matchingNode *CandidateNode, expressionNode *ExpressionNode) (*CandidateNode, error) {
2020-11-13 02:19:54 +00:00
var path []interface{}
var document uint
2020-11-13 02:19:54 +00:00
var matches = list.New()
if matchingNode != nil {
path = matchingNode.Path
document = matchingNode.Document
matches.PushBack(matchingNode)
2020-10-21 01:54:58 +00:00
}
mapPairs, err := crossFunction(d, context.ChildContext(matches), expressionNode,
func(d *dataTreeNavigator, context Context, lhs *CandidateNode, rhs *CandidateNode) (*CandidateNode, error) {
2020-10-21 01:54:58 +00:00
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{
2021-01-12 23:00:51 +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
}, false)
2020-10-21 01:54:58 +00:00
if err != nil {
return nil, err
}
innerList := listToNodeSeq(mapPairs.MatchingNodes)
2020-11-13 02:19:54 +00:00
innerList.Style = yaml.FlowStyle
return &CandidateNode{Node: innerList, Document: document, Path: path}, nil
}
//NOTE: here the document index gets dropped so we
// no longer know where the node originates from.
func listToNodeSeq(list *list.List) *yaml.Node {
2020-10-21 01:54:58 +00:00
node := yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
2020-11-13 02:19:54 +00:00
for entry := list.Front(); entry != nil; entry = entry.Next() {
entryCandidate := entry.Value.(*CandidateNode)
log.Debugf("Collecting %v into sequence", NodeToString(entryCandidate))
node.Content = append(node.Content, entryCandidate.Node)
2020-10-21 01:54:58 +00:00
}
2020-11-13 02:19:54 +00:00
return &node
2020-10-21 01:54:58 +00:00
}