yq/pkg/yqlib/operator_collect.go

41 lines
1.1 KiB
Go
Raw Normal View History

2020-11-03 23:48:43 +00:00
package yqlib
2020-10-17 11:10:47 +00:00
import (
2020-10-21 01:54:58 +00:00
"container/list"
2020-11-22 02:50:32 +00:00
yaml "gopkg.in/yaml.v3"
2020-10-17 11:10:47 +00:00
)
func collectOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2020-10-17 11:10:47 +00:00
log.Debugf("-- collectOperation")
if context.MatchingNodes.Len() == 0 {
2020-11-22 02:50:32 +00:00
node := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq", Value: "[]"}
candidate := &CandidateNode{Node: node}
return context.SingleChildContext(candidate), nil
2020-11-22 02:50:32 +00:00
}
2020-10-21 01:54:58 +00:00
var results = list.New()
2020-10-17 11:10:47 +00:00
node := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
var collectC *CandidateNode
if context.MatchingNodes.Front() != nil {
2021-11-23 22:57:35 +00:00
collectC = context.MatchingNodes.Front().Value.(*CandidateNode).CreateReplacement(node)
if len(collectC.Path) > 0 {
collectC.Path = collectC.Path[:len(collectC.Path)-1]
}
} else {
collectC = &CandidateNode{Node: node}
}
2020-10-17 11:10:47 +00:00
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-10-17 11:10:47 +00:00
candidate := el.Value.(*CandidateNode)
log.Debugf("Collecting %v", NodeToString(candidate))
2021-01-12 23:00:51 +00:00
node.Content = append(node.Content, unwrapDoc(candidate.Node))
2020-10-17 11:10:47 +00:00
}
2020-10-21 01:54:58 +00:00
results.PushBack(collectC)
2020-10-17 11:10:47 +00:00
return context.ChildContext(results), nil
2020-10-17 11:10:47 +00:00
}