Fixed newline handling in encoder/decoder

This commit is contained in:
Mike Farah 2021-10-22 15:21:01 +11:00
parent a1af1b95d0
commit 587af7f722
3 changed files with 16 additions and 3 deletions

View File

@ -1,5 +1,6 @@
Encode operators will take the piped in object structure and encode it as a string in the desired format. The decode operators do the opposite, they take a formatted string and decode it into the relevant object structure.
These operators are useful to process yaml documents that have stringified embeded yaml/json/props in them.
## Encode value as yaml string
Given a sample.yml file of:
```yaml
@ -98,6 +99,7 @@ Given a sample.yml file of:
a: |
foo: bar
baz: dog
```
then
```bash

View File

@ -28,6 +28,7 @@ func encodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
var results = list.New()
hasOnlyOneNewLine := regexp.MustCompile("[^\n].*\n$")
endWithNewLine := regexp.MustCompile(".*\n$")
chomper := regexp.MustCompile("\n+$")
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
@ -44,8 +45,9 @@ func encodeOperator(d *dataTreeNavigator, context Context, expressionNode *Expre
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 {
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) {
stringValue = chomper.ReplaceAllString(stringValue, "")
}
}

View File

@ -53,12 +53,21 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
{
description: "Update a multiline encoded yaml string",
dontFormatInputForDoc: true,
document: "a: |\n foo: bar\n baz: dog",
document: "a: |\n foo: bar\n baz: dog\n",
expression: `.a |= (from_yaml | .foo = "cat" | to_yaml)`,
expected: []string{
"D0, P[], (doc)::a: |\n foo: cat\n baz: dog\n",
},
},
{
skipDoc: true,
dontFormatInputForDoc: true,
document: "a: |-\n foo: bar\n baz: dog\n",
expression: `.a |= (from_yaml | .foo = "cat" | to_yaml)`,
expected: []string{
"D0, P[], (doc)::a: |-\n foo: cat\n baz: dog\n",
},
},
{
description: "Update a single line encoded yaml string",
dontFormatInputForDoc: true,