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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func yamlToString(candidate *CandidateNode, prefs encoderPreferences) (string, error) {
|
|
|
|
var output bytes.Buffer
|
|
|
|
printer := NewPrinter(bufio.NewWriter(&output), prefs.format, true, false, 2, true)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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$")
|
|
|
|
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 := yamlToString(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)
|
|
|
|
if unwrapDoc(original.Node).Style == yaml.SingleQuotedStyle ||
|
|
|
|
unwrapDoc(original.Node).Style == yaml.DoubleQuotedStyle {
|
|
|
|
stringValue = chomper.ReplaceAllString(stringValue, "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:00:47 +00:00
|
|
|
stringContentNode := &yaml.Node{Kind: yaml.ScalarNode, Tag: "!!str", Value: stringValue}
|
|
|
|
results.PushBack(candidate.CreateChild(nil, stringContentNode))
|
|
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|
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) {
|
|
|
|
|
|
|
|
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)
|
|
|
|
decoder := yaml.NewDecoder(strings.NewReader(unwrapDoc(candidate.Node).Value))
|
|
|
|
errorReading := decoder.Decode(&dataBucket)
|
|
|
|
if errorReading != nil {
|
|
|
|
return Context{}, errorReading
|
|
|
|
}
|
|
|
|
//first node is a doc
|
|
|
|
node := unwrapDoc(&dataBucket)
|
|
|
|
|
|
|
|
results.PushBack(candidate.CreateChild(nil, node))
|
|
|
|
}
|
|
|
|
return context.ChildContext(results), nil
|
|
|
|
}
|