2021-10-22 01:00:47 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"container/list"
|
2023-03-01 02:19:06 +00:00
|
|
|
"errors"
|
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
|
|
|
)
|
|
|
|
|
2024-02-24 03:58:11 +00:00
|
|
|
func configureEncoder(format *PrinterOutputFormat, indent int) Encoder {
|
2024-02-24 04:03:30 +00:00
|
|
|
|
2022-01-15 00:57:59 +00:00
|
|
|
switch format {
|
2022-02-07 00:55:55 +00:00
|
|
|
case JSONOutputFormat:
|
2024-02-24 04:59:12 +00:00
|
|
|
prefs := ConfiguredJSONPreferences.Copy()
|
2024-02-24 04:48:59 +00:00
|
|
|
prefs.Indent = indent
|
|
|
|
prefs.ColorsEnabled = false
|
|
|
|
prefs.UnwrapScalar = false
|
|
|
|
return NewJSONEncoder(prefs)
|
2022-01-15 00:57:59 +00:00
|
|
|
case YamlOutputFormat:
|
2024-02-24 04:36:16 +00:00
|
|
|
var prefs = ConfiguredYamlPreferences.Copy()
|
|
|
|
prefs.Indent = indent
|
|
|
|
prefs.ColorsEnabled = false
|
|
|
|
return NewYamlEncoder(prefs)
|
2022-02-07 00:55:55 +00:00
|
|
|
case XMLOutputFormat:
|
2024-02-24 04:03:30 +00:00
|
|
|
var xmlPrefs = ConfiguredXMLPreferences.Copy()
|
|
|
|
xmlPrefs.Indent = indent
|
|
|
|
return NewXMLEncoder(xmlPrefs)
|
2022-01-15 00:57:59 +00:00
|
|
|
}
|
2024-02-24 05:07:15 +00:00
|
|
|
return format.EncoderFactory()
|
2022-01-15 00:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-03-01 02:19:06 +00:00
|
|
|
if encoder == nil {
|
|
|
|
return "", errors.New("no support for output format")
|
|
|
|
}
|
2022-01-15 00:57:59 +00:00
|
|
|
|
|
|
|
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 {
|
2024-02-24 03:58:11 +00:00
|
|
|
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 */
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func encodeOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2021-10-22 01:00:47 +00:00
|
|
|
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)
|
2023-03-16 02:39:36 +00:00
|
|
|
// original block did not have a newline at the end, get rid of this one too
|
2023-10-18 01:11:53 +00:00
|
|
|
if !endWithNewLine.MatchString(original.Value) {
|
2021-10-22 03:53:39 +00:00
|
|
|
stringValue = chomper.ReplaceAllString(stringValue, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-16 02:39:36 +00:00
|
|
|
// dont print a newline when printing json on a single line.
|
2022-02-07 00:55:55 +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, "")
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +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
|
|
|
|
}
|
|
|
|
|
2023-03-01 02:19:06 +00:00
|
|
|
func createDecoder(format InputFormat) Decoder {
|
2021-12-21 04:02:07 +00:00
|
|
|
var decoder Decoder
|
2023-03-01 02:19:06 +00:00
|
|
|
switch format {
|
|
|
|
case JsonInputFormat:
|
|
|
|
decoder = NewJSONDecoder()
|
2021-12-21 04:02:07 +00:00
|
|
|
case YamlInputFormat:
|
2022-10-28 03:16:46 +00:00
|
|
|
decoder = NewYamlDecoder(ConfiguredYamlPreferences)
|
2022-02-07 00:55:55 +00:00
|
|
|
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()
|
2022-08-01 00:28:34 +00:00
|
|
|
case CSVObjectInputFormat:
|
2024-02-15 02:11:53 +00:00
|
|
|
decoder = NewCSVObjectDecoder(ConfiguredCsvPreferences)
|
2022-08-01 00:28:34 +00:00
|
|
|
case TSVObjectInputFormat:
|
2024-02-15 02:11:53 +00:00
|
|
|
decoder = NewCSVObjectDecoder(ConfiguredTsvPreferences)
|
2023-01-23 00:37:18 +00:00
|
|
|
case UriInputFormat:
|
|
|
|
decoder = NewUriDecoder()
|
2021-12-21 04:02:07 +00:00
|
|
|
}
|
2023-03-01 02:19:06 +00:00
|
|
|
return decoder
|
|
|
|
}
|
|
|
|
|
|
|
|
/* takes a string and decodes it back into an object */
|
2024-01-11 02:17:34 +00:00
|
|
|
func decodeOperator(_ *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
|
2023-03-01 02:19:06 +00:00
|
|
|
|
|
|
|
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)
|
2021-10-22 03:53:39 +00:00
|
|
|
|
|
|
|
context.SetVariable("decoded: "+candidate.GetKey(), candidate.AsList())
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
log.Debugf("got: [%v]", candidate.Value)
|
2021-12-21 04:02:07 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
err := decoder.Init(strings.NewReader(candidate.Value))
|
2022-10-28 03:16:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return Context{}, err
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
node, errorReading := decoder.Decode()
|
2021-10-22 01:37:47 +00:00
|
|
|
if errorReading != nil {
|
|
|
|
return Context{}, errorReading
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
node.Key = candidate.Key
|
|
|
|
node.Parent = candidate.Parent
|
2021-10-22 01:37:47 +00:00
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
results.PushBack(node)
|
2021-10-22 01:37:47 +00:00
|
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|