2020-11-22 00:56:28 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
yaml "gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2021-01-11 05:46:28 +00:00
|
|
|
// 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-07-16 11:08:20 +00:00
|
|
|
Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) (uint, 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)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
result, errorParsing := s.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputList}, node)
|
2020-12-01 03:06:49 +00:00
|
|
|
if errorParsing != nil {
|
|
|
|
return errorParsing
|
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
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-07-16 11:08:20 +00:00
|
|
|
var totalProcessDocs uint = 0
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:48:16 +00:00
|
|
|
for index, filename := range filenames {
|
|
|
|
reader, leadingSeperator, err := readStream(filename)
|
|
|
|
if index == 0 && leadingSeperator {
|
|
|
|
printer.SetPrintLeadingSeperator(leadingSeperator)
|
|
|
|
}
|
2020-11-22 00:56:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-16 11:08:20 +00:00
|
|
|
processedDocs, err := s.Evaluate(filename, reader, node, printer)
|
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 {
|
2021-07-16 12:08:22 +00:00
|
|
|
|
|
|
|
if len(filenames) > 0 {
|
|
|
|
reader, _, err := readStream(filenames[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch reader := reader.(type) {
|
|
|
|
case *os.File:
|
|
|
|
defer safelyCloseFile(reader)
|
|
|
|
}
|
|
|
|
printer.SetPreamble(reader)
|
|
|
|
}
|
|
|
|
|
2021-07-16 11:08:20 +00:00
|
|
|
return s.EvaluateNew(expression, printer)
|
|
|
|
}
|
|
|
|
|
2020-11-22 00:56:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-16 11:08:20 +00:00
|
|
|
func (s *streamEvaluator) Evaluate(filename string, reader io.Reader, node *ExpressionNode, printer Printer) (uint, 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
|
2021-07-16 11:08:20 +00:00
|
|
|
return currentIndex, nil
|
2020-11-22 00:56:28 +00:00
|
|
|
} else if errorReading != nil {
|
2021-07-16 11:08:20 +00:00
|
|
|
return currentIndex, errorReading
|
2020-11-22 00:56:28 +00:00
|
|
|
}
|
|
|
|
candidateNode := &CandidateNode{
|
|
|
|
Document: currentIndex,
|
|
|
|
Filename: filename,
|
|
|
|
Node: &dataBucket,
|
|
|
|
FileIndex: s.fileIndex,
|
|
|
|
}
|
|
|
|
inputList := list.New()
|
|
|
|
inputList.PushBack(candidateNode)
|
|
|
|
|
2021-02-02 07:17:59 +00:00
|
|
|
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
|
|
|
}
|
2021-02-02 07:17:59 +00:00
|
|
|
err := printer.PrintResults(result.MatchingNodes)
|
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
|
|
|
|
}
|
|
|
|
}
|