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-10-17 11:10:47 +00:00
|
|
|
)
|
|
|
|
|
2021-11-30 02:19:30 +00:00
|
|
|
func collectTogether(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (*CandidateNode, error) {
|
2023-04-09 01:14:51 +00:00
|
|
|
collectedNode := &CandidateNode{Kind: SequenceNode, Tag: "!!seq"}
|
2021-11-30 02:19:30 +00:00
|
|
|
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))
|
2023-05-09 03:51:21 +00:00
|
|
|
collectedNode.AddChild(resultC)
|
2021-11-30 02:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-09 01:14:51 +00:00
|
|
|
return collectedNode, nil
|
2021-11-30 02:19:30 +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 {
|
2023-03-31 05:24:23 +00:00
|
|
|
log.Debugf("nothing to collect")
|
2023-04-09 01:14:51 +00:00
|
|
|
node := &CandidateNode{Kind: SequenceNode, Tag: "!!seq", Value: "[]"}
|
|
|
|
return context.SingleChildContext(node), nil
|
2020-11-22 02:50:32 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 02:19:30 +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
|
|
|
|
2021-11-30 02:19:30 +00:00
|
|
|
if evaluateAllTogether {
|
2023-03-31 05:24:23 +00:00
|
|
|
log.Debugf("collect together")
|
2022-02-07 00:55:55 +00:00
|
|
|
collectedNode, err := collectTogether(d, context, expressionNode.RHS)
|
2021-11-30 02:19:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
2021-01-12 08:36:28 +00:00
|
|
|
}
|
2021-11-30 02:19:30 +00:00
|
|
|
return context.SingleChildContext(collectedNode), nil
|
|
|
|
|
2021-01-12 08:36:28 +00:00
|
|
|
}
|
2020-10-17 11:10:47 +00:00
|
|
|
|
2021-11-30 02:19:30 +00:00
|
|
|
var results = list.New()
|
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)
|
|
|
|
|
2023-04-08 11:27:47 +00:00
|
|
|
collectCandidate := candidate.CreateReplacement(SequenceNode, "!!seq", "")
|
2021-11-30 02:19:30 +00:00
|
|
|
|
2023-03-31 05:24:23 +00:00
|
|
|
log.Debugf("collect rhs: %v", expressionNode.RHS.Operation.toString())
|
|
|
|
|
2022-10-24 06:55:19 +00:00
|
|
|
collectExpResults, err := d.GetMatchingNodes(context.SingleChildContext(candidate), expressionNode.RHS)
|
2021-11-30 02:19:30 +00:00
|
|
|
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))
|
2023-05-09 03:51:21 +00:00
|
|
|
collectCandidate.AddChild(resultC)
|
2021-11-30 02:19:30 +00:00
|
|
|
}
|
2023-03-31 05:24:23 +00:00
|
|
|
log.Debugf("done collect rhs: %v", expressionNode.RHS.Operation.toString())
|
2021-11-30 02:19:30 +00:00
|
|
|
|
|
|
|
results.PushBack(collectCandidate)
|
|
|
|
}
|
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
|
|
|
}
|