yq/pkg/yqlib/operator_collect.go

40 lines
1.0 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
)
2020-10-21 01:54:58 +00:00
func CollectOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) {
2020-10-17 11:10:47 +00:00
log.Debugf("-- collectOperation")
2020-11-22 02:50:32 +00:00
if matchMap.Len() == 0 {
node := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq", Value: "[]"}
candidate := &CandidateNode{Node: node}
return nodeToMap(candidate), nil
}
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 document uint = 0
var path []interface{}
for el := matchMap.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
log.Debugf("Collecting %v", NodeToString(candidate))
if path == nil && candidate.Path != nil && len(candidate.Path) > 1 {
path = candidate.Path[:len(candidate.Path)-1]
document = candidate.Document
}
node.Content = append(node.Content, candidate.Node)
}
collectC := &CandidateNode{Node: node, Document: document, Path: path}
2020-10-21 01:54:58 +00:00
results.PushBack(collectC)
2020-10-17 11:10:47 +00:00
return results, nil
}