mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Moved eval function to eval interface
This commit is contained in:
parent
e6336bcb85
commit
ba223df4ac
@ -1,14 +1,20 @@
|
|||||||
package yqlib
|
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.
|
// A yaml expression evaluator that runs the expression once against all files/nodes in memory.
|
||||||
type Evaluator interface {
|
type Evaluator interface {
|
||||||
EvaluateFiles(expression string, filenames []string, printer Printer) error
|
EvaluateFiles(expression string, filenames []string, printer Printer) error
|
||||||
|
|
||||||
// Runs the expression once against the list of candidate nodes, returns the
|
// EvaluateNodes takes an expression and one or more yaml nodes, returning a list of matching candidate nodes
|
||||||
// resulting nodes.
|
EvaluateNodes(expression string, nodes ...*yaml.Node) (*list.List, error)
|
||||||
EvaluateNodes(expression string, inputCandidateNodes *list.List) (*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 {
|
type allAtOnceEvaluator struct {
|
||||||
@ -20,7 +26,15 @@ func NewAllAtOnceEvaluator() Evaluator {
|
|||||||
return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewPathTreeCreator()}
|
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)
|
node, err := treeCreator.ParsePath(expression)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -44,7 +58,7 @@ func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string
|
|||||||
allDocuments.PushBackList(fileDocuments)
|
allDocuments.PushBackList(fileDocuments)
|
||||||
fileIndex = fileIndex + 1
|
fileIndex = fileIndex + 1
|
||||||
}
|
}
|
||||||
matches, err := e.EvaluateNodes(expression, allDocuments)
|
matches, err := e.EvaluateCandidateNodes(expression, allDocuments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -30,10 +30,11 @@ var evaluateNodesScenario = []expressionScenario{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEvaluateNodesScenarios(t *testing.T) {
|
func TestAllAtOnceEvaluateNodes(t *testing.T) {
|
||||||
|
var evaluator = NewAllAtOnceEvaluator()
|
||||||
for _, tt := range evaluateNodesScenario {
|
for _, tt := range evaluateNodesScenario {
|
||||||
node := test.ParseData(tt.document)
|
node := test.ParseData(tt.document)
|
||||||
list, _ := EvaluateNodes(tt.expression, &node)
|
list, _ := evaluator.EvaluateNodes(tt.expression, &node)
|
||||||
test.AssertResultComplex(t, tt.expected, resultsToString(list))
|
test.AssertResultComplex(t, tt.expected, resultsToString(list))
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
// Use the top level Evaluator or StreamEvaluator to evaluate expressions and return matches.
|
||||||
|
//
|
||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
import (
|
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())
|
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 {
|
func KindString(kind yaml.Kind) string {
|
||||||
switch kind {
|
switch kind {
|
||||||
case yaml.ScalarNode:
|
case yaml.ScalarNode:
|
||||||
|
Loading…
Reference in New Issue
Block a user