yq/pkg/yqlib/operator_collect.go

41 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
)
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 collectC *CandidateNode
if matchMap.Front() != nil {
collectC = matchMap.Front().Value.(*CandidateNode).CreateChild(nil, 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 := matchMap.Front(); el != nil; el = el.Next() {
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 results, nil
}