mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
49 lines
1.3 KiB
Go
49 lines
1.3 KiB
Go
package yqlib
|
|
|
|
import (
|
|
"container/list"
|
|
)
|
|
|
|
type recursiveDescentPreferences struct {
|
|
TraversePreferences traversePreferences
|
|
RecurseArray bool
|
|
}
|
|
|
|
func recursiveDescentOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
var results = list.New()
|
|
|
|
preferences := expressionNode.Operation.Preferences.(recursiveDescentPreferences)
|
|
err := recursiveDecent(results, context, preferences)
|
|
if err != nil {
|
|
return Context{}, err
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
}
|
|
|
|
func recursiveDecent(results *list.List, context Context, preferences recursiveDescentPreferences) error {
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
candidate := el.Value.(*CandidateNode)
|
|
|
|
candidate = candidate.unwrapDocument()
|
|
|
|
log.Debugf("Recursive Decent, added %v", NodeToString(candidate))
|
|
results.PushBack(candidate)
|
|
|
|
if candidate.Kind != AliasNode && len(candidate.Content) > 0 &&
|
|
(preferences.RecurseArray || candidate.Kind != SequenceNode) {
|
|
|
|
children, err := splat(context.SingleChildContext(candidate), preferences.TraversePreferences)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = recursiveDecent(results, children, preferences)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|