diff --git a/pkg/yqlib/all_at_once_evaluator.go b/pkg/yqlib/all_at_once_evaluator.go index 4efd6afd..4a9c6d5e 100644 --- a/pkg/yqlib/all_at_once_evaluator.go +++ b/pkg/yqlib/all_at_once_evaluator.go @@ -1,14 +1,20 @@ package yqlib -import "container/list" +import ( + "container/list" + + yaml "gopkg.in/yaml.v3" +) // A yaml expression evaluator that runs the expression once against all files/nodes in memory. type Evaluator interface { EvaluateFiles(expression string, filenames []string, printer Printer) error - // Runs the expression once against the list of candidate nodes, returns the - // resulting nodes. - EvaluateNodes(expression string, inputCandidateNodes *list.List) (*list.List, error) + // EvaluateNodes takes an expression and one or more yaml nodes, returning a list of matching candidate nodes + EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) + + // EvaluateCandidateNodes takes an expression and list of candidate nodes, returning a list of matching candidate nodes + EvaluateCandidateNodes(expression string, inputCandidateNodes *list.List) (*list.List, error) } type allAtOnceEvaluator struct { @@ -20,7 +26,15 @@ func NewAllAtOnceEvaluator() Evaluator { return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewPathTreeCreator()} } -func (e *allAtOnceEvaluator) EvaluateNodes(expression string, inputCandidates *list.List) (*list.List, error) { +func (e *allAtOnceEvaluator) EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error) { + inputCandidates := list.New() + for _, node := range nodes { + inputCandidates.PushBack(&CandidateNode{Node: node}) + } + return e.EvaluateCandidateNodes(expression, inputCandidates) +} + +func (e *allAtOnceEvaluator) EvaluateCandidateNodes(expression string, inputCandidates *list.List) (*list.List, error) { node, err := treeCreator.ParsePath(expression) if err != nil { return nil, err @@ -44,7 +58,7 @@ func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string allDocuments.PushBackList(fileDocuments) fileIndex = fileIndex + 1 } - matches, err := e.EvaluateNodes(expression, allDocuments) + matches, err := e.EvaluateCandidateNodes(expression, allDocuments) if err != nil { return err } diff --git a/pkg/yqlib/lib_test.go b/pkg/yqlib/all_at_once_evaluator_test.go similarity index 81% rename from pkg/yqlib/lib_test.go rename to pkg/yqlib/all_at_once_evaluator_test.go index b62ba39b..b75ab300 100644 --- a/pkg/yqlib/lib_test.go +++ b/pkg/yqlib/all_at_once_evaluator_test.go @@ -30,10 +30,11 @@ var evaluateNodesScenario = []expressionScenario{ }, } -func TestEvaluateNodesScenarios(t *testing.T) { +func TestAllAtOnceEvaluateNodes(t *testing.T) { + var evaluator = NewAllAtOnceEvaluator() for _, tt := range evaluateNodesScenario { node := test.ParseData(tt.document) - list, _ := EvaluateNodes(tt.expression, &node) + list, _ := evaluator.EvaluateNodes(tt.expression, &node) test.AssertResultComplex(t, tt.expected, resultsToString(list)) } } diff --git a/pkg/yqlib/lib.go b/pkg/yqlib/lib.go index 2bab479e..d5aab1f3 100644 --- a/pkg/yqlib/lib.go +++ b/pkg/yqlib/lib.go @@ -1,3 +1,5 @@ +// Use the top level Evaluator or StreamEvaluator to evaluate expressions and return matches. +// package yqlib import ( @@ -166,24 +168,6 @@ 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: