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-29 23:56:45 +00:00
|
|
|
|
2020-11-27 23:41:09 +00:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-10-18 00:31:36 +00:00
|
|
|
)
|
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
type recursiveDescentPreferences struct {
|
|
|
|
TraversePreferences *traversePreferences
|
2020-12-28 00:24:42 +00:00
|
|
|
RecurseArray bool
|
|
|
|
}
|
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
func recursiveDescentOperator(d *dataTreeNavigator, matchMap *list.List, pathNode *PathTreeNode) (*list.List, error) {
|
2020-10-21 01:54:58 +00:00
|
|
|
var results = list.New()
|
2020-10-18 00:31:36 +00:00
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
preferences := pathNode.Operation.Preferences.(*recursiveDescentPreferences)
|
2020-12-28 00:24:42 +00:00
|
|
|
err := recursiveDecent(d, results, matchMap, preferences)
|
2020-10-18 00:31:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return results, nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
func recursiveDecent(d *dataTreeNavigator, results *list.List, matchMap *list.List, preferences *recursiveDescentPreferences) error {
|
2020-10-18 00:31:36 +00:00
|
|
|
for el := matchMap.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2020-10-27 05:45:16 +00:00
|
|
|
|
|
|
|
candidate.Node = UnwrapDoc(candidate.Node)
|
|
|
|
|
|
|
|
log.Debugf("Recursive Decent, added %v", NodeToString(candidate))
|
2020-10-21 01:54:58 +00:00
|
|
|
results.PushBack(candidate)
|
2020-10-18 00:31:36 +00:00
|
|
|
|
2020-11-27 23:41:09 +00:00
|
|
|
if candidate.Node.Kind != yaml.AliasNode && len(candidate.Node.Content) > 0 &&
|
2020-12-28 00:24:42 +00:00
|
|
|
(preferences.RecurseArray || candidate.Node.Kind != yaml.SequenceNode) {
|
2020-10-18 00:31:36 +00:00
|
|
|
|
2021-01-11 06:13:48 +00:00
|
|
|
children, err := splat(d, nodeToMap(candidate), preferences.TraversePreferences)
|
2020-10-29 23:56:45 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-28 00:24:42 +00:00
|
|
|
err = recursiveDecent(d, 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
|
|
|
|
}
|