2022-07-31 22:50:56 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"container/list"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
type StringEvaluator interface {
|
2022-10-25 04:35:49 +00:00
|
|
|
Evaluate(expression string, input string, encoder Encoder, decoder Decoder) (string, error)
|
2022-07-31 22:50:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type stringEvaluator struct {
|
|
|
|
treeNavigator DataTreeNavigator
|
|
|
|
fileIndex int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStringEvaluator() StringEvaluator {
|
|
|
|
return &stringEvaluator{
|
|
|
|
treeNavigator: NewDataTreeNavigator(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 04:35:49 +00:00
|
|
|
func (s *stringEvaluator) Evaluate(expression string, input string, encoder Encoder, decoder Decoder) (string, error) {
|
2022-07-31 22:50:56 +00:00
|
|
|
|
|
|
|
// Use bytes.Buffer for output of string
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
printer := NewPrinter(encoder, NewSinglePrinterWriter(out))
|
|
|
|
|
|
|
|
InitExpressionParser()
|
|
|
|
node, err := ExpressionParser.ParseExpression(expression)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2022-10-25 04:35:49 +00:00
|
|
|
reader, err := readString(input)
|
2022-07-31 22:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentIndex uint
|
2022-10-25 04:35:49 +00:00
|
|
|
err = decoder.Init(reader)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-07-31 22:50:56 +00:00
|
|
|
for {
|
2022-10-25 04:35:49 +00:00
|
|
|
candidateNode, errorReading := decoder.Decode()
|
2022-07-31 22:50:56 +00:00
|
|
|
|
|
|
|
if errors.Is(errorReading, io.EOF) {
|
|
|
|
s.fileIndex = s.fileIndex + 1
|
|
|
|
return out.String(), nil
|
|
|
|
} else if errorReading != nil {
|
|
|
|
return "", fmt.Errorf("bad input '%v': %w", input, errorReading)
|
|
|
|
}
|
2022-10-25 04:35:49 +00:00
|
|
|
candidateNode.Document = currentIndex
|
|
|
|
candidateNode.FileIndex = s.fileIndex
|
2022-07-31 22:50:56 +00:00
|
|
|
|
|
|
|
inputList := list.New()
|
|
|
|
inputList.PushBack(candidateNode)
|
|
|
|
|
|
|
|
result, errorParsing := s.treeNavigator.GetMatchingNodes(Context{MatchingNodes: inputList}, node)
|
|
|
|
if errorParsing != nil {
|
|
|
|
return "", errorParsing
|
|
|
|
}
|
|
|
|
err = printer.PrintResults(result.MatchingNodes)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
currentIndex = currentIndex + 1
|
|
|
|
}
|
|
|
|
}
|