From 85629af59ccc029d1c975fbe5e7c218e5d9fc495 Mon Sep 17 00:00:00 2001 From: Mikhail Katychev Date: Tue, 12 Jan 2021 12:54:26 -0600 Subject: [PATCH] added EvaluateNodes and EvaluateCandidateNodes to yqlib --- pkg/yqlib/lib.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 6fc0b168..2bab479e 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -166,6 +166,24 @@ func NodeToString(node *CandidateNode) string { return fmt.Sprintf(`D%v, P%v, (%v)::%v`, node.Document, node.Path, tag, buf.String()) } +// EvaluateNodes takes an expression and one or more yaml nodes, returning a list of matching candidate nodes +func EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) { + inputCandidates := list.New() + for _, node := range nodes { + inputCandidates.PushBack(&CandidateNode{Node: node}) + } + return EvaluateCandidateNodes(expression, inputCandidates) +} + +// EvaluateCandidateNodes takes an expression and list of candidate nodes, returning a list of matching candidate nodes +func EvaluateCandidateNodes(expression string, inputCandidates *list.List) (*list.List, error) { + node, err := treeCreator.ParsePath(expression) + if err != nil { + return nil, err + } + return treeNavigator.GetMatchingNodes(inputCandidates, node) +} + func KindString(kind yaml.Kind) string { switch kind { case yaml.ScalarNode: