yq/pkg/yqlib/all_at_once_evaluator.go

91 lines
2.8 KiB
Go
Raw Normal View History

2020-11-22 00:56:28 +00:00
package yqlib
2021-01-12 22:35:57 +00:00
import (
"container/list"
yaml "gopkg.in/yaml.v3"
)
2020-11-22 00:56: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 {
2021-12-21 04:02:07 +00:00
EvaluateFiles(expression string, filenames []string, printer Printer, leadingContentPreProcessing bool, decoder Decoder) error
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
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)
2020-11-22 00:56:28 +00:00
}
type allAtOnceEvaluator struct {
treeNavigator DataTreeNavigator
2021-01-12 23:18:53 +00:00
treeCreator ExpressionParser
2020-11-22 00:56:28 +00:00
}
func NewAllAtOnceEvaluator() Evaluator {
2021-01-12 23:18:53 +00:00
return &allAtOnceEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewExpressionParser()}
2020-11-22 00:56:28 +00:00
}
2021-01-12 22:35:57 +00:00
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) {
2021-01-12 23:18:53 +00:00
node, err := e.treeCreator.ParseExpression(expression)
2020-11-22 00:56:28 +00:00
if err != nil {
return nil, err
2020-11-22 00:56:28 +00:00
}
context, err := e.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputCandidates}, node)
if err != nil {
return nil, err
}
return context.MatchingNodes, nil
}
2021-12-21 04:02:07 +00:00
func (e *allAtOnceEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer, leadingContentPreProcessing bool, decoder Decoder) error {
fileIndex := 0
2021-07-19 09:52:51 +00:00
firstFileLeadingContent := ""
var allDocuments = list.New()
2020-11-22 00:56:28 +00:00
for _, filename := range filenames {
reader, leadingContent, err := readStream(filename, fileIndex == 0 && leadingContentPreProcessing)
2020-11-22 00:56:28 +00:00
if err != nil {
return err
}
2021-07-19 09:52:51 +00:00
if fileIndex == 0 {
firstFileLeadingContent = leadingContent
}
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 {
candidateNode := &CandidateNode{
2021-11-12 04:02:28 +00:00
Document: 0,
Filename: "",
Node: &yaml.Node{Kind: yaml.DocumentNode, Content: []*yaml.Node{{Tag: "!!null", Kind: yaml.ScalarNode}}},
FileIndex: 0,
LeadingContent: firstFileLeadingContent,
2021-07-16 11:08:20 +00:00
}
allDocuments.PushBack(candidateNode)
} else {
2021-11-12 04:02:28 +00:00
allDocuments.Front().Value.(*CandidateNode).LeadingContent = firstFileLeadingContent
2021-07-16 11:08:20 +00:00
}
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
}
return printer.PrintResults(matches)
2020-11-22 00:56:28 +00:00
}