2020-11-22 00:56:28 +00:00
|
|
|
package yqlib
|
|
|
|
|
2021-01-12 22:35:57 +00:00
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
)
|
2020-11-22 00:56:28 +00:00
|
|
|
|
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 {
|
2022-10-28 03:16:46 +00:00
|
|
|
EvaluateFiles(expression string, filenames []string, printer Printer, decoder Decoder) error
|
2021-01-11 05:46:28 +00:00
|
|
|
|
2021-01-12 22:35:57 +00:00
|
|
|
// EvaluateNodes takes an expression and one or more yaml nodes, returning a list of matching candidate nodes
|
2023-10-18 01:11:53 +00:00
|
|
|
EvaluateNodes(expression string, nodes ...*CandidateNode) (*list.List, error)
|
2021-01-12 22:35:57 +00:00
|
|
|
|
|
|
|
// 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)
|
2020-11-22 00:56:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type allAtOnceEvaluator struct {
|
|
|
|
treeNavigator DataTreeNavigator
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAllAtOnceEvaluator() Evaluator {
|
2022-03-15 02:28:52 +00:00
|
|
|
InitExpressionParser()
|
2022-02-01 03:47:51 +00:00
|
|
|
return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator()}
|
2020-11-22 00:56:28 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (e *allAtOnceEvaluator) EvaluateNodes(expression string, nodes ...*CandidateNode) (*list.List, error) {
|
2021-01-12 22:35:57 +00:00
|
|
|
inputCandidates := list.New()
|
|
|
|
for _, node := range nodes {
|
2023-10-18 01:11:53 +00:00
|
|
|
inputCandidates.PushBack(node)
|
2021-01-12 22:35:57 +00:00
|
|
|
}
|
|
|
|
return e.EvaluateCandidateNodes(expression, inputCandidates)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *allAtOnceEvaluator) EvaluateCandidateNodes(expression string, inputCandidates *list.List) (*list.List, error) {
|
2022-02-01 03:47:51 +00:00
|
|
|
node, err := ExpressionParser.ParseExpression(expression)
|
2020-11-22 00:56:28 +00:00
|
|
|
if err != nil {
|
2021-01-11 05:46:28 +00:00
|
|
|
return nil, err
|
2020-11-22 00:56:28 +00:00
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
context, err := e.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputCandidates}, node)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return context.MatchingNodes, nil
|
2021-01-11 05:46:28 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer, decoder Decoder) error {
|
2021-01-11 05:46:28 +00:00
|
|
|
fileIndex := 0
|
|
|
|
|
2021-12-20 22:30:08 +00:00
|
|
|
var allDocuments = list.New()
|
2020-11-22 00:56:28 +00:00
|
|
|
for _, filename := range filenames {
|
2022-10-28 03:16:46 +00:00
|
|
|
reader, err := readStream(filename)
|
2020-11-22 00:56:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-14 10:48:16 +00:00
|
|
|
|
2021-12-21 04:02:07 +00:00
|
|
|
fileDocuments, err := readDocuments(reader, filename, fileIndex, decoder)
|
2020-11-22 00:56:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
allDocuments.PushBackList(fileDocuments)
|
|
|
|
fileIndex = fileIndex + 1
|
|
|
|
}
|
2021-07-16 11:08:20 +00:00
|
|
|
|
|
|
|
if allDocuments.Len() == 0 {
|
2023-10-18 01:11:53 +00:00
|
|
|
candidateNode := createScalarNode(nil, "")
|
2021-07-16 11:08:20 +00:00
|
|
|
allDocuments.PushBack(candidateNode)
|
|
|
|
}
|
|
|
|
|
2021-01-12 22:35:57 +00:00
|
|
|
matches, err := e.EvaluateCandidateNodes(expression, allDocuments)
|
2020-11-22 00:56:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-20 00:19:55 +00:00
|
|
|
return printer.PrintResults(matches)
|
2020-11-22 00:56:28 +00:00
|
|
|
}
|