2022-07-31 22:50:56 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
2022-10-28 03:16:46 +00:00
|
|
|
"bufio"
|
2022-07-31 22:50:56 +00:00
|
|
|
"bytes"
|
|
|
|
"container/list"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-10-28 03:16:46 +00:00
|
|
|
"strings"
|
2022-07-31 22:50:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StringEvaluator interface {
|
2022-10-28 03:16:46 +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-28 03:16:46 +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-28 03:16:46 +00:00
|
|
|
reader := bufio.NewReader(strings.NewReader(input))
|
|
|
|
|
|
|
|
var currentIndex uint
|
|
|
|
err = decoder.Init(reader)
|
2022-07-31 22:50:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for {
|
2022-10-28 03:16:46 +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)
|
|
|
|
}
|
2023-10-18 01:11:53 +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
|
|
|
|
}
|
|
|
|
}
|