2021-10-22 01:00:47 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"container/list"
|
2021-10-22 03:53:39 +00:00
|
|
|
"regexp"
|
2021-10-22 01:37:47 +00:00
|
|
|
"strings"
|
2021-10-22 01:00:47 +00:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
|
|
|
|
switch format {
|
|
|
|
case JsonOutputFormat:
|
|
|
|
return NewJsonEncoder(indent)
|
|
|
|
case PropsOutputFormat:
|
|
|
|
return NewPropertiesEncoder()
|
|
|
|
case CsvOutputFormat:
|
|
|
|
return NewCsvEncoder(',')
|
|
|
|
case TsvOutputFormat:
|
|
|
|
return NewCsvEncoder('\t')
|
|
|
|
case YamlOutputFormat:
|
|
|
|
return NewYamlEncoder(indent, false, true, true)
|
|
|
|
case XmlOutputFormat:
|
|
|
|
return NewXmlEncoder(indent, XmlPreferences.AttributePrefix, XmlPreferences.ContentName)
|
|
|
|
}
|
|
|
|
panic("invalid encoder")
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeToString(candidate *CandidateNode, prefs encoderPreferences) (string, error) {
|
2021-10-22 01:00:47 +00:00
|
|
|
var output bytes.Buffer
|
2021-10-24 00:35:40 +00:00
|
|
|
log.Debug("printing with indent: %v", prefs.indent)
|
2021-10-29 03:14:39 +00:00
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
encoder := configureEncoder(prefs.format, prefs.indent)
|
|
|
|
|
|
|
|
printer := NewPrinter(encoder, NewSinglePrinterWriter(bufio.NewWriter(&output)))
|
2021-10-22 03:53:39 +00:00
|
|
|
err := printer.PrintResults(candidate.AsList())
|
2021-10-22 01:00:47 +00:00
|
|
|
return output.String(), err
|
|
|
|
}
|
|
|
|
|
|
|
|
type encoderPreferences struct {
|
|
|
|
format PrinterOutputFormat
|
2021-10-24 00:35:40 +00:00
|
|
|
indent int
|
2021-10-22 01:00:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* encodes object as yaml string */
|
|
|
|
|
|
|
|
func encodeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
preferences := expressionNode.Operation.Preferences.(encoderPreferences)
|
|
|
|
var results = list.New()
|
2021-10-22 03:53:39 +00:00
|
|
|
|
|
|
|
hasOnlyOneNewLine := regexp.MustCompile("[^\n].*\n$")
|
2021-10-22 04:21:01 +00:00
|
|
|
endWithNewLine := regexp.MustCompile(".*\n$")
|
2021-10-22 03:53:39 +00:00
|
|
|
chomper := regexp.MustCompile("\n+$")
|
|
|
|
|
2021-10-22 01:00:47 +00:00
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2022-01-15 00:57:59 +00:00
|
|
|
stringValue, err := encodeToString(candidate, preferences)
|
2021-10-22 03:53:39 +00:00
|
|
|
|
2021-10-22 01:00:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
|
|
|
|
2021-10-22 03:53:39 +00:00
|
|
|
// remove trailing newlines if needed.
|
|
|
|
// check if we originally decoded this path, and the original thing had a single line.
|
|
|
|
originalList := context.GetVariable("decoded: " + candidate.GetKey())
|
|
|
|
if originalList != nil && originalList.Len() > 0 && hasOnlyOneNewLine.MatchString(stringValue) {
|
|
|
|
|
|
|
|
original := originalList.Front().Value.(*CandidateNode)
|
2021-10-22 04:21:01 +00:00
|
|
|
originalNode := unwrapDoc(original.Node)
|
|
|
|
// original block did not have a new line at the end, get rid of this one too
|
|
|
|
if !endWithNewLine.MatchString(originalNode.Value) {
|
2021-10-22 03:53:39 +00:00
|
|
|
stringValue = chomper.ReplaceAllString(stringValue, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 00:35:40 +00:00
|
|
|
// dont print a new line when printing json on a single line.
|
2021-12-02 01:11:15 +00:00
|
|
|
if (preferences.format == JsonOutputFormat && preferences.indent == 0) ||
|
|
|
|
preferences.format == CsvOutputFormat ||
|
|
|
|
preferences.format == TsvOutputFormat {
|
2021-10-24 00:35:40 +00:00
|
|
|
stringValue = chomper.ReplaceAllString(stringValue, "")
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:00:47 +00:00
|
|
|
stringContentNode := &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: stringValue}
|
2021-11-23 22:57:35 +00:00
|
|
|
results.PushBack(candidate.CreateReplacement(stringContentNode))
|
2021-10-22 01:00:47 +00:00
|
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|
2021-10-22 01:37:47 +00:00
|
|
|
|
2021-12-21 04:02:07 +00:00
|
|
|
type decoderPreferences struct {
|
|
|
|
format InputFormat
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:37:47 +00:00
|
|
|
/* takes a string and decodes it back into an object */
|
|
|
|
func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
|
|
|
|
2021-12-21 04:02:07 +00:00
|
|
|
preferences := expressionNode.Operation.Preferences.(decoderPreferences)
|
|
|
|
|
|
|
|
var decoder Decoder
|
|
|
|
switch preferences.format {
|
|
|
|
case YamlInputFormat:
|
|
|
|
decoder = NewYamlDecoder()
|
|
|
|
case XmlInputFormat:
|
2021-12-21 05:52:54 +00:00
|
|
|
decoder = NewXmlDecoder(XmlPreferences.AttributePrefix, XmlPreferences.ContentName)
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 01:37:47 +00:00
|
|
|
var results = list.New()
|
|
|
|
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
|
|
|
|
candidate := el.Value.(*CandidateNode)
|
2021-10-22 03:53:39 +00:00
|
|
|
|
|
|
|
context.SetVariable("decoded: "+candidate.GetKey(), candidate.AsList())
|
|
|
|
|
2021-10-22 01:37:47 +00:00
|
|
|
var dataBucket yaml.Node
|
|
|
|
log.Debugf("got: [%v]", candidate.Node.Value)
|
2021-12-21 04:02:07 +00:00
|
|
|
|
|
|
|
decoder.Init(strings.NewReader(unwrapDoc(candidate.Node).Value))
|
|
|
|
|
2021-10-22 01:37:47 +00:00
|
|
|
errorReading := decoder.Decode(&dataBucket)
|
|
|
|
if errorReading != nil {
|
|
|
|
return Context{}, errorReading
|
|
|
|
}
|
|
|
|
//first node is a doc
|
|
|
|
node := unwrapDoc(&dataBucket)
|
|
|
|
|
2021-11-23 22:57:35 +00:00
|
|
|
results.PushBack(candidate.CreateReplacement(node))
|
2021-10-22 01:37:47 +00:00
|
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|