yq/pkg/yqlib/operator_collect.go

80 lines
2.6 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 collectTogether(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (*CandidateNode, error) {
collectedNode := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
collectExpResults, err := d.GetMatchingNodes(context.SingleReadonlyChildContext(candidate), expressionNode)
if err != nil {
return nil, err
}
for result := collectExpResults.MatchingNodes.Front(); result != nil; result = result.Next() {
resultC := result.Value.(*CandidateNode)
log.Debugf("found this: %v", NodeToString(resultC))
collectedNode.Content = append(collectedNode.Content, unwrapDoc(resultC.Node))
}
}
return &CandidateNode{Node: collectedNode}, nil
}
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 {
log.Debugf("nothing to collect")
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
}
var evaluateAllTogether = true
for matchEl := context.MatchingNodes.Front(); matchEl != nil; matchEl = matchEl.Next() {
evaluateAllTogether = evaluateAllTogether && matchEl.Value.(*CandidateNode).EvaluateTogether
if !evaluateAllTogether {
break
}
}
2020-10-17 11:10:47 +00:00
if evaluateAllTogether {
log.Debugf("collect together")
collectedNode, err := collectTogether(d, context, expressionNode.RHS)
if err != nil {
return Context{}, err
}
return context.SingleChildContext(collectedNode), nil
}
2020-10-17 11:10:47 +00:00
var results = list.New()
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
2020-10-17 11:10:47 +00:00
candidate := el.Value.(*CandidateNode)
collectedNode := &yaml.Node{Kind: yaml.SequenceNode, Tag: "!!seq"}
collectCandidate := candidate.CreateReplacement(collectedNode)
log.Debugf("collect rhs: %v", expressionNode.RHS.Operation.toString())
collectExpResults, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.RHS)
if err != nil {
return Context{}, err
}
for result := collectExpResults.MatchingNodes.Front(); result != nil; result = result.Next() {
resultC := result.Value.(*CandidateNode)
log.Debugf("found this: %v", NodeToString(resultC))
collectedNode.Content = append(collectedNode.Content, unwrapDoc(resultC.Node))
}
log.Debugf("done collect rhs: %v", expressionNode.RHS.Operation.toString())
results.PushBack(collectCandidate)
}
2020-10-17 11:10:47 +00:00
return context.ChildContext(results), nil
2020-10-17 11:10:47 +00:00
}