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
|
|
|
)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
func collectOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-10-17 11:10:47 +00:00
|
|
|
log.Debugf("-- collectOperation")
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
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}
|
2021-02-02 07:17:59 +00:00
|
|
|
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"}
|
2021-01-12 08:36:28 +00:00
|
|
|
var collectC *CandidateNode
|
2021-02-02 07:17:59 +00:00
|
|
|
if context.MatchingNodes.Front() != nil {
|
2021-11-23 22:57:35 +00:00
|
|
|
collectC = context.MatchingNodes.Front().Value.(*CandidateNode).CreateReplacement(node)
|
2021-01-12 08:36:28 +00:00
|
|
|
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
|
|
|
|
2021-02-02 07:17:59 +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
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2020-10-17 11:10:47 +00:00
|
|
|
}
|