2020-11-22 00:56:28 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import "container/list"
|
|
|
|
|
2021-01-11 05:46:28 +00:00
|
|
|
// A yaml expression evaluator that runs the expression once against all files/nodes in memory.
|
2020-11-22 00:56:28 +00:00
|
|
|
type Evaluator interface {
|
|
|
|
EvaluateFiles(expression string, filenames []string, printer Printer) error
|
2021-01-11 05:46:28 +00:00
|
|
|
|
|
|
|
// Runs the expression once against the list of candidate nodes, returns the
|
|
|
|
// resulting nodes.
|
|
|
|
EvaluateNodes(expression string, inputCandidateNodes *list.List) (*list.List, error)
|
2020-11-22 00:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type allAtOnceEvaluator struct {
|
|
|
|
treeNavigator DataTreeNavigator
|
|
|
|
treeCreator PathTreeCreator
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAllAtOnceEvaluator() Evaluator {
|
|
|
|
return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewPathTreeCreator()}
|
|
|
|
}
|
|
|
|
|
2021-01-11 05:46:28 +00:00
|
|
|
func (e *allAtOnceEvaluator) EvaluateNodes(expression string, inputCandidates *list.List) (*list.List, error) {
|
2020-11-22 00:56:28 +00:00
|
|
|
node, err := treeCreator.ParsePath(expression)
|
|
|
|
if err != nil {
|
2021-01-11 05:46:28 +00:00
|
|
|
return nil, err
|
2020-11-22 00:56:28 +00:00
|
|
|
}
|
2021-01-11 05:46:28 +00:00
|
|
|
return treeNavigator.GetMatchingNodes(inputCandidates, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error {
|
|
|
|
fileIndex := 0
|
|
|
|
|
2020-11-22 00:56:28 +00:00
|
|
|
var allDocuments *list.List = list.New()
|
|
|
|
for _, filename := range filenames {
|
|
|
|
reader, err := readStream(filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fileDocuments, err := readDocuments(reader, filename, fileIndex)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
allDocuments.PushBackList(fileDocuments)
|
|
|
|
fileIndex = fileIndex + 1
|
|
|
|
}
|
2021-01-11 05:46:28 +00:00
|
|
|
matches, err := e.EvaluateNodes(expression, allDocuments)
|
2020-11-22 00:56:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return printer.PrintResults(matches)
|
|
|
|
}
|