yq/pkg/yqlib/stream_evaluator.go

120 lines
3.2 KiB
Go
Raw Permalink Normal View History

2020-11-22 00:56:28 +00:00
package yqlib
import (
"container/list"
2021-11-23 22:27:17 +00:00
"errors"
"fmt"
2020-11-22 00:56:28 +00:00
"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 {
Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer, decoder Decoder) (uint, error)
EvaluateFiles(expression string, filenames []string, printer Printer, decoder Decoder) error
EvaluateNew(expression string, printer Printer) error
2020-11-22 00:56:28 +00:00
}
type streamEvaluator struct {
treeNavigator DataTreeNavigator
fileIndex int
}
func NewStreamEvaluator() StreamEvaluator {
2022-02-01 03:47:51 +00:00
return &streamEvaluator{treeNavigator: NewDataTreeNavigator()}
2020-11-22 00:56:28 +00:00
}
func (s *streamEvaluator) EvaluateNew(expression string, printer Printer) error {
2022-02-01 03:47:51 +00:00
node, err := ExpressionParser.ParseExpression(expression)
2020-12-01 03:06:49 +00:00
if err != nil {
return err
}
candidateNode := &CandidateNode{
Document: 0,
Filename: "",
Node: &yaml.Node{Kind: yaml.DocumentNode, Content: []*yaml.Node{{Tag: "!!null", Kind: yaml.ScalarNode}}},
FileIndex: 0,
2020-12-01 03:06:49 +00:00
}
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
}
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer, decoder Decoder) error {
var totalProcessDocs uint
2022-02-01 03:47:51 +00:00
node, err := ExpressionParser.ParseExpression(expression)
2020-11-22 00:56:28 +00:00
if err != nil {
return err
}
for _, filename := range filenames {
reader, err := readStream(filename)
2021-07-19 09:52:51 +00:00
2020-11-22 00:56:28 +00:00
if err != nil {
return err
}
processedDocs, err := s.Evaluate(filename, reader, node, printer, decoder)
2020-11-22 00:56:28 +00:00
if err != nil {
return err
}
2021-07-16 11:08:20 +00:00
totalProcessDocs = totalProcessDocs + processedDocs
2020-11-22 00:56:28 +00:00
switch reader := reader.(type) {
case *os.File:
safelyCloseFile(reader)
}
}
2021-07-16 11:08:20 +00:00
if totalProcessDocs == 0 {
// problem is I've already slurped the leading content sadface
return s.EvaluateNew(expression, printer)
2021-07-16 11:08:20 +00:00
}
2020-11-22 00:56:28 +00:00
return nil
}
func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer, decoder Decoder) (uint, error) {
2020-11-22 00:56:28 +00:00
var currentIndex uint
err := decoder.Init(reader)
if err != nil {
return 0, err
}
2020-11-22 00:56:28 +00:00
for {
candidateNode, errorReading := decoder.Decode()
2020-11-22 00:56:28 +00:00
2021-11-22 06:43:38 +00:00
if errors.Is(errorReading, io.EOF) {
2020-11-22 00:56:28 +00:00
s.fileIndex = s.fileIndex + 1
2021-07-16 11:08:20 +00:00
return currentIndex, nil
2020-11-22 00:56:28 +00:00
} else if errorReading != nil {
return currentIndex, fmt.Errorf("bad file '%v': %w", filename, errorReading)
2020-11-22 00:56:28 +00:00
}
candidateNode.Document = currentIndex
candidateNode.Filename = filename
candidateNode.FileIndex = s.fileIndex
2021-11-12 04:02:28 +00:00
2020-11-22 00:56:28 +00:00
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 {
2021-07-16 11:08:20 +00:00
return currentIndex, errorParsing
2020-11-22 00:56:28 +00:00
}
err := printer.PrintResults(result.MatchingNodes)
2021-07-19 09:52:51 +00:00
2020-11-22 00:56:28 +00:00
if err != nil {
2021-07-16 11:08:20 +00:00
return currentIndex, err
2020-11-22 00:56:28 +00:00
}
currentIndex = currentIndex + 1
}
}