yq/pkg/yqlib/operator_parent.go

35 lines
873 B
Go
Raw Permalink Normal View History

2021-11-23 23:16:48 +00:00
package yqlib
import "container/list"
2024-03-11 23:48:42 +00:00
type parentOpPreferences struct {
Level int
}
func getParentOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
2024-02-15 22:41:33 +00:00
log.Debugf("getParentOperator")
2021-11-23 23:16:48 +00:00
var results = list.New()
2024-03-11 23:48:42 +00:00
prefs := expressionNode.Operation.Preferences.(parentOpPreferences)
2021-11-23 23:16:48 +00:00
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
2024-03-11 23:48:42 +00:00
currentLevel := 0
for currentLevel < prefs.Level && candidate != nil {
log.Debugf("currentLevel: %v, desired: %v", currentLevel, prefs.Level)
log.Debugf("candidate: %v", NodeToString(candidate))
candidate = candidate.Parent
currentLevel++
}
log.Debugf("found candidate: %v", NodeToString(candidate))
if candidate != nil {
results.PushBack(candidate)
2021-11-23 23:16:48 +00:00
}
}
return context.ChildContext(results), nil
}