yq/pkg/yqlib/operator_encoder_decoder.go

165 lines
4.7 KiB
Go
Raw Normal View History

2021-10-22 01:00:47 +00:00
package yqlib
import (
"bufio"
"bytes"
"container/list"
"errors"
"regexp"
2021-10-22 01:37:47 +00:00
"strings"
2021-10-22 01:00:47 +00:00
)
func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
switch format {
case JSONOutputFormat:
return NewJSONEncoder(indent, false, false)
case PropsOutputFormat:
return NewPropertiesEncoder(true)
case CSVOutputFormat:
return NewCsvEncoder(',')
case TSVOutputFormat:
return NewCsvEncoder('\t')
case YamlOutputFormat:
return NewYamlEncoder(indent, false, ConfiguredYamlPreferences)
case XMLOutputFormat:
2022-10-25 03:27:16 +00:00
return NewXMLEncoder(indent, ConfiguredXMLPreferences)
2022-02-22 22:26:35 +00:00
case Base64OutputFormat:
return NewBase64Encoder()
2023-01-23 00:37:18 +00:00
case UriOutputFormat:
return NewUriEncoder()
2023-02-02 01:22:52 +00:00
case ShOutputFormat:
return NewShEncoder()
}
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
encoder := configureEncoder(prefs.format, prefs.indent)
if encoder == nil {
return "", errors.New("no support for output format")
}
printer := NewPrinter(encoder, NewSinglePrinterWriter(bufio.NewWriter(&output)))
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()
hasOnlyOneNewLine := regexp.MustCompile("[^\n].*\n$")
endWithNewLine := regexp.MustCompile(".*\n$")
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)
stringValue, err := encodeToString(candidate, preferences)
2021-10-22 01:00:47 +00:00
if err != nil {
return Context{}, err
}
// 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)
2023-04-09 01:14:51 +00:00
originalNode := original.unwrapDocument()
// original block did not have a newline at the end, get rid of this one too
if !endWithNewLine.MatchString(originalNode.Value) {
stringValue = chomper.ReplaceAllString(stringValue, "")
}
}
// dont print a newline when printing json on a single line.
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, "")
}
2023-04-09 01:14:51 +00:00
results.PushBack(candidate.CreateReplacement(ScalarNode, "!!str", stringValue))
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
}
func createDecoder(format InputFormat) Decoder {
2021-12-21 04:02:07 +00:00
var decoder Decoder
switch format {
case JsonInputFormat:
decoder = NewJSONDecoder()
2021-12-21 04:02:07 +00:00
case YamlInputFormat:
decoder = NewYamlDecoder(ConfiguredYamlPreferences)
case XMLInputFormat:
2022-10-25 03:27:16 +00:00
decoder = NewXMLDecoder(ConfiguredXMLPreferences)
2022-02-22 22:26:35 +00:00
case Base64InputFormat:
decoder = NewBase64Decoder()
2022-03-28 08:48:30 +00:00
case PropertiesInputFormat:
decoder = NewPropertiesDecoder()
case CSVObjectInputFormat:
decoder = NewCSVObjectDecoder(',')
case TSVObjectInputFormat:
decoder = NewCSVObjectDecoder('\t')
2023-01-23 00:37:18 +00:00
case UriInputFormat:
decoder = NewUriDecoder()
2021-12-21 04:02:07 +00:00
}
return decoder
}
/* takes a string and decodes it back into an object */
func decodeOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
preferences := expressionNode.Operation.Preferences.(decoderPreferences)
decoder := createDecoder(preferences.format)
if decoder == nil {
return Context{}, errors.New("no support for input format")
}
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)
context.SetVariable("decoded: "+candidate.GetKey(), candidate.AsList())
2023-04-09 01:14:51 +00:00
log.Debugf("got: [%v]", candidate.Value)
2021-12-21 04:02:07 +00:00
2023-04-09 01:14:51 +00:00
err := decoder.Init(strings.NewReader(candidate.unwrapDocument().Value))
if err != nil {
return Context{}, err
}
2021-12-21 04:02:07 +00:00
decodedNode, errorReading := decoder.Decode()
2021-10-22 01:37:47 +00:00
if errorReading != nil {
return Context{}, errorReading
}
//first node is a doc
2023-04-09 01:14:51 +00:00
node := decodedNode.unwrapDocument()
node.Path = candidate.Path
node.Key = candidate.Key
node.Parent = candidate.Parent
node.Document = candidate.Document
node.FileIndex = candidate.FileIndex
node.Filename = candidate.Filename
results.PushBack(node)
2021-10-22 01:37:47 +00:00
}
return context.ChildContext(results), nil
}