2020-11-22 00:56:28 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2021-11-23 22:27:17 +00:00
|
|
|
"errors"
|
2021-12-06 06:25:32 +00:00
|
|
|
"fmt"
|
2020-11-22 00:56:28 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
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 {
|
2022-10-28 03:16:46 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +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
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
candidateNode := createScalarNode(nil, "")
|
2020-12-01 03:06:49 +00:00
|
|
|
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-07-20 00:19:55 +00:00
|
|
|
return printer.PrintResults(result.MatchingNodes)
|
2020-12-01 03:06:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
func (s *streamEvaluator) EvaluateFiles(expression string, filenames []string, printer Printer, decoder Decoder) error {
|
2021-12-20 22:30:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
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
|
|
|
|
}
|
2022-10-28 03:16:46 +00:00
|
|
|
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 {
|
2022-10-28 03:16:46 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-10-28 03:16:46 +00:00
|
|
|
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
|
2022-10-28 03:16:46 +00:00
|
|
|
err := decoder.Init(reader)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-11-22 00:56:28 +00:00
|
|
|
for {
|
2022-10-28 03:16:46 +00:00
|
|
|
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 {
|
2021-12-06 06:25:32 +00:00
|
|
|
return currentIndex, fmt.Errorf("bad file '%v': %w", filename, errorReading)
|
2020-11-22 00:56:28 +00:00
|
|
|
}
|
2023-10-18 01:11:53 +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)
|
|
|
|
|
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-07-20 00:19:55 +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
|
|
|
|
}
|
|
|
|
}
|