forked from mikefarah/yq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
operator_recursive_descent.go
50 lines (38 loc) · 1.3 KB
/
operator_recursive_descent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package yqlib
import (
"container/list"
yaml "gopkg.in/yaml.v3"
)
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.Node = unwrapDoc(candidate.Node)
log.Debugf("Recursive Decent, added %v", NodeToString(candidate))
results.PushBack(candidate)
if candidate.Node.Kind != yaml.AliasNode && len(candidate.Node.Content) > 0 &&
(preferences.RecurseArray || candidate.Node.Kind != yaml.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
}