2020-11-03 23:48:43 +00:00
|
|
|
package yqlib
|
2020-10-18 00:31:36 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-21 01:54:58 +00:00
|
|
|
"container/list"
|
2020-10-18 00:31:36 +00:00
|
|
|
)
|
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
type recursiveDescentPreferences struct {
|
2021-01-13 05:54:28 +00:00
|
|
|
TraversePreferences traversePreferences
|
2020-12-28 00:24:42 +00:00
|
|
|
RecurseArray bool
|
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func recursiveDescentOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2020-10-21 01:54:58 +00:00
|
|
|
var results = list.New()
|
2020-10-18 00:31:36 +00:00
|
|
|
|
2021-01-13 06:00:03 +00:00
|
|
|
preferences := expressionNode.Operation.Preferences.(recursiveDescentPreferences)
|
2021-12-20 22:30:08 +00:00
|
|
|
err := recursiveDecent(results, context, preferences)
|
2020-10-18 00:31:36 +00:00
|
|
|
if err != nil {
|
2021-02-02 07:17:59 +00:00
|
|
|
return Context{}, err
|
2020-10-18 00:31:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
return context.ChildContext(results), nil
|
2020-10-18 00:31:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 22:30:08 +00:00
|
|
|
func recursiveDecent(results *list.List, context Context, preferences recursiveDescentPreferences) error {
|
2021-02-02 07:17:59 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
2020-10-18 00:31:36 +00:00
|
|
|
candidate := el.Value.(*CandidateNode)
|
2020-10-27 05:45:16 +00:00
|
|
|
|
2024-02-15 22:41:33 +00:00
|
|
|
log.Debugf("added %v", NodeToString(candidate))
|
2020-10-21 01:54:58 +00:00
|
|
|
results.PushBack(candidate)
|
2020-10-18 00:31:36 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
if candidate.Kind != AliasNode && len(candidate.Content) > 0 &&
|
|
|
|
(preferences.RecurseArray || candidate.Kind != SequenceNode) {
|
2020-10-18 00:31:36 +00:00
|
|
|
|
2021-12-20 22:30:08 +00:00
|
|
|
children, err := splat(context.SingleChildContext(candidate), preferences.TraversePreferences)
|
2020-10-29 23:56:45 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-12-20 22:30:08 +00:00
|
|
|
err = recursiveDecent(results, children, preferences)
|
2020-10-29 23:56:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-18 00:31:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|