yq/pkg/yqlib/stream_evaluator.go

111 lines
2.7 KiB
Go
Raw Normal View History

2020-11-22 00:56:28 +00:00
package yqlib
import (
"container/list"
"io"
"os"
yaml "gopkg.in/yaml.v3"
)
// A yaml expression evaluator that runs the expression multiple times for each given yaml document.
// Uses less memory than loading all documents and running the expression once, but this cannot process
// cross document expressions.
2020-11-22 00:56:28 +00:00
type StreamEvaluator interface {
2021-01-12 23:18:53 +00:00
Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) error
2020-11-22 00:56:28 +00:00
EvaluateFiles(expression string, filenames []string, printer Printer) error
2020-12-01 03:06:49 +00:00
EvaluateNew(expression string, printer Printer) error
2020-11-22 00:56:28 +00:00
}
type streamEvaluator struct {
treeNavigator DataTreeNavigator
2021-01-12 23:18:53 +00:00
treeCreator ExpressionParser
2020-11-22 00:56:28 +00:00
fileIndex int
}
func NewStreamEvaluator() StreamEvaluator {
2021-01-12 23:18:53 +00:00
return &streamEvaluator{treeNavigator: NewDataTreeNavigator(), treeCreator: NewExpressionParser()}
2020-11-22 00:56:28 +00:00
}
2020-12-01 03:06:49 +00:00
func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error {
2021-01-12 23:18:53 +00:00
node, err := s.treeCreator.ParseExpression(expression)
2020-12-01 03:06:49 +00:00
if err != nil {
return err
}
candidateNode := &CandidateNode{
Document: 0,
Filename: "",
2021-01-14 03:25:31 +00:00
Node: &yaml.Node{Tag: "!!null", Kind: yaml.ScalarNode},
2020-12-01 03:06:49 +00:00
FileIndex: 0,
}
inputList := list.New()
inputList.PushBack(candidateNode)
result, errorParsing := s.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputList}, node)
2020-12-01 03:06:49 +00:00
if errorParsing != nil {
return errorParsing
}
return printer.PrintResults(result.MatchingNodes)
2020-12-01 03:06:49 +00:00
}
2020-11-22 00:56:28 +00:00
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer) error {
2021-01-12 23:18:53 +00:00
node, err := s.treeCreator.ParseExpression(expression)
2020-11-22 00:56:28 +00:00
if err != nil {
return err
}
for _, filename := range filenames {
reader, err := readStream(filename)
if err != nil {
return err
}
err = s.Evaluate(filename, reader, node, printer)
if err != nil {
return err
}
switch reader := reader.(type) {
case *os.File:
safelyCloseFile(reader)
}
}
return nil
}
2021-01-12 23:18:53 +00:00
func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) error {
2020-11-22 00:56:28 +00:00
var currentIndex uint
decoder := yaml.NewDecoder(reader)
for {
var dataBucket yaml.Node
errorReading := decoder.Decode(&dataBucket)
if errorReading == io.EOF {
s.fileIndex = s.fileIndex + 1
return nil
} else if errorReading != nil {
return errorReading
}
candidateNode := &CandidateNode{
Document: currentIndex,
Filename: filename,
Node: &dataBucket,
FileIndex: s.fileIndex,
}
inputList := list.New()
inputList.PushBack(candidateNode)
result, errorParsing := s.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputList}, node)
2020-11-22 00:56:28 +00:00
if errorParsing != nil {
return errorParsing
}
err := printer.PrintResults(result.MatchingNodes)
2020-11-22 00:56:28 +00:00
if err != nil {
return err
}
currentIndex = currentIndex + 1
}
}