2021-10-22 01:00:47 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2021-10-24 00:35:40 +00:00
|
|
|
var prefix = "D0, P[], (doc)::a:\n cool:\n bob: dylan\n"
|
|
|
|
|
2021-10-22 01:37:47 +00:00
|
|
|
var encoderDecoderOperatorScenarios = []expressionScenario{
|
2021-10-22 01:00:47 +00:00
|
|
|
{
|
2021-12-02 01:11:15 +00:00
|
|
|
description: "Encode value as json string",
|
|
|
|
document: `{a: {cool: "thing"}}`,
|
|
|
|
expression: `.b = (.a | to_json)`,
|
2021-10-22 01:00:47 +00:00
|
|
|
expected: []string{
|
2021-12-02 01:11:15 +00:00
|
|
|
`D0, P[], (doc)::{a: {cool: "thing"}, b: "{\n \"cool\": \"thing\"\n}\n"}
|
|
|
|
`,
|
2021-10-24 00:35:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-02 01:11:15 +00:00
|
|
|
description: "Encode value as json string, on one line",
|
|
|
|
subdescription: "Pass in a 0 indent to print json on a single line.",
|
|
|
|
document: `{a: {cool: "thing"}}`,
|
|
|
|
expression: `.b = (.a | to_json(0))`,
|
2021-10-24 00:35:40 +00:00
|
|
|
expected: []string{
|
2021-12-02 01:11:15 +00:00
|
|
|
`D0, P[], (doc)::{a: {cool: "thing"}, b: '{"cool":"thing"}'}
|
|
|
|
`,
|
2021-10-22 01:00:47 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-02 01:11:15 +00:00
|
|
|
description: "Encode value as json string, on one line shorthand",
|
|
|
|
subdescription: "Pass in a 0 indent to print json on a single line.",
|
2021-10-22 01:00:47 +00:00
|
|
|
document: `{a: {cool: "thing"}}`,
|
2021-12-02 01:11:15 +00:00
|
|
|
expression: `.b = (.a | @json)`,
|
2021-10-22 01:00:47 +00:00
|
|
|
expected: []string{
|
2021-12-02 01:11:15 +00:00
|
|
|
`D0, P[], (doc)::{a: {cool: "thing"}, b: '{"cool":"thing"}'}
|
2021-10-22 01:00:47 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-02 01:11:15 +00:00
|
|
|
description: "Decode a json encoded string",
|
|
|
|
subdescription: "Keep in mind JSON is a subset of YAML. If you want idiomatic yaml, pipe through the style operator to clear out the JSON styling.",
|
|
|
|
document: `a: '{"cool":"thing"}'`,
|
|
|
|
expression: `.a | from_json | ... style=""`,
|
2021-10-22 01:00:47 +00:00
|
|
|
expected: []string{
|
2021-12-02 01:11:15 +00:00
|
|
|
"D0, P[a], (!!map)::cool: thing\n",
|
2021-10-24 00:35:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-02 01:11:15 +00:00
|
|
|
skipDoc: true,
|
|
|
|
document: `{a: {cool: "thing"}}`,
|
|
|
|
expression: `.b = (.a | to_props)`,
|
2021-10-24 00:35:40 +00:00
|
|
|
expected: []string{
|
2021-12-02 01:11:15 +00:00
|
|
|
`D0, P[], (doc)::{a: {cool: "thing"}, b: "cool = thing\n"}
|
2021-10-22 01:00:47 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode value as props string",
|
|
|
|
document: `{a: {cool: "thing"}}`,
|
2021-12-02 01:11:15 +00:00
|
|
|
expression: `.b = (.a | @props)`,
|
2021-10-22 01:00:47 +00:00
|
|
|
expected: []string{
|
|
|
|
`D0, P[], (doc)::{a: {cool: "thing"}, b: "cool = thing\n"}
|
2021-12-02 01:11:15 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
2022-03-28 08:48:30 +00:00
|
|
|
{
|
|
|
|
description: "Decode props encoded string",
|
|
|
|
document: `a: "cats=great\ndogs=cool as well"`,
|
2022-08-01 00:28:34 +00:00
|
|
|
expression: `.a |= @propsd`,
|
2022-03-28 08:48:30 +00:00
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a:\n cats: great\n dogs: cool as well\n",
|
|
|
|
},
|
|
|
|
},
|
2022-08-01 00:28:34 +00:00
|
|
|
{
|
|
|
|
description: "Decode csv encoded string",
|
|
|
|
document: `a: "cats,dogs\ngreat,cool as well"`,
|
|
|
|
expression: `.a |= @csvd`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a:\n - cats: great\n dogs: cool as well\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Decode tsv encoded string",
|
|
|
|
document: `a: "cats dogs\ngreat cool as well"`,
|
|
|
|
expression: `.a |= @tsvd`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a:\n - cats: great\n dogs: cool as well\n",
|
|
|
|
},
|
|
|
|
},
|
2021-12-02 01:11:15 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
document: "a:\n cool:\n bob: dylan",
|
|
|
|
expression: `.b = (.a | @yaml)`,
|
|
|
|
expected: []string{
|
|
|
|
prefix + "b: |\n cool:\n bob: dylan\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode value as yaml string",
|
|
|
|
subdescription: "Indent defaults to 2",
|
|
|
|
document: "a:\n cool:\n bob: dylan",
|
|
|
|
expression: `.b = (.a | to_yaml)`,
|
|
|
|
expected: []string{
|
|
|
|
prefix + "b: |\n cool:\n bob: dylan\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode value as yaml string, with custom indentation",
|
|
|
|
subdescription: "You can specify the indentation level as the first parameter.",
|
|
|
|
document: "a:\n cool:\n bob: dylan",
|
|
|
|
expression: `.b = (.a | to_yaml(8))`,
|
|
|
|
expected: []string{
|
|
|
|
prefix + "b: |\n cool:\n bob: dylan\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-02 01:29:23 +00:00
|
|
|
skipDoc: true,
|
|
|
|
document: `{a: {cool: "thing"}}`,
|
|
|
|
expression: `.b = (.a | to_yaml)`,
|
2021-12-02 01:11:15 +00:00
|
|
|
expected: []string{
|
|
|
|
`D0, P[], (doc)::{a: {cool: "thing"}, b: "{cool: \"thing\"}\n"}
|
2021-10-22 01:00:47 +00:00
|
|
|
`,
|
|
|
|
},
|
|
|
|
},
|
2021-10-22 01:37:47 +00:00
|
|
|
{
|
|
|
|
description: "Decode a yaml encoded string",
|
|
|
|
document: `a: "foo: bar"`,
|
|
|
|
expression: `.b = (.a | from_yaml)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: \"foo: bar\"\nb:\n foo: bar\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-10-22 03:53:39 +00:00
|
|
|
description: "Update a multiline encoded yaml string",
|
2021-10-22 01:37:47 +00:00
|
|
|
dontFormatInputForDoc: true,
|
2021-10-22 04:21:01 +00:00
|
|
|
document: "a: |\n foo: bar\n baz: dog\n",
|
2021-10-22 01:37:47 +00:00
|
|
|
expression: `.a |= (from_yaml | .foo = "cat" | to_yaml)`,
|
|
|
|
expected: []string{
|
2021-10-22 03:53:39 +00:00
|
|
|
"D0, P[], (doc)::a: |\n foo: cat\n baz: dog\n",
|
|
|
|
},
|
|
|
|
},
|
2021-10-22 04:21:01 +00:00
|
|
|
{
|
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
2021-10-22 03:53:39 +00:00
|
|
|
{
|
|
|
|
description: "Update a single line encoded yaml string",
|
|
|
|
dontFormatInputForDoc: true,
|
|
|
|
document: "a: 'foo: bar'",
|
|
|
|
expression: `.a |= (from_yaml | .foo = "cat" | to_yaml)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: 'foo: cat'\n",
|
|
|
|
},
|
|
|
|
},
|
2021-12-02 01:11:15 +00:00
|
|
|
{
|
|
|
|
description: "Encode array of scalars as csv string",
|
|
|
|
subdescription: "Scalars are strings, numbers and booleans.",
|
|
|
|
document: `[cat, "thing1,thing2", true, 3.40]`,
|
|
|
|
expression: `@csv`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::cat,\"thing1,thing2\",true,3.40\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode array of arrays as csv string",
|
|
|
|
document: `[[cat, "thing1,thing2", true, 3.40], [dog, thing3, false, 12]]`,
|
|
|
|
expression: `@csv`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::cat,\"thing1,thing2\",true,3.40\ndog,thing3,false,12\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode array of array scalars as tsv string",
|
|
|
|
subdescription: "Scalars are strings, numbers and booleans.",
|
|
|
|
document: `[[cat, "thing1,thing2", true, 3.40], [dog, thing3, false, 12]]`,
|
|
|
|
expression: `@tsv`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::cat\tthing1,thing2\ttrue\t3.40\ndog\tthing3\tfalse\t12\n",
|
|
|
|
},
|
|
|
|
},
|
2021-10-22 03:53:39 +00:00
|
|
|
{
|
|
|
|
skipDoc: true,
|
|
|
|
dontFormatInputForDoc: true,
|
|
|
|
document: "a: \"foo: bar\"",
|
|
|
|
expression: `.a |= (from_yaml | .foo = {"a": "frog"} | to_yaml)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: \"foo:\\n a: frog\"\n",
|
2021-10-22 01:37:47 +00:00
|
|
|
},
|
|
|
|
},
|
2021-12-21 05:52:54 +00:00
|
|
|
{
|
|
|
|
description: "Encode value as xml string",
|
|
|
|
document: `{a: {cool: {foo: "bar", +id: hi}}}`,
|
|
|
|
expression: `.a | to_xml`,
|
|
|
|
expected: []string{
|
2021-12-21 05:59:09 +00:00
|
|
|
"D0, P[a], (!!str)::<cool id=\"hi\">\n <foo>bar</foo>\n</cool>\n\n",
|
2021-12-21 05:52:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode value as xml string on a single line",
|
|
|
|
document: `{a: {cool: {foo: "bar", +id: hi}}}`,
|
|
|
|
expression: `.a | @xml`,
|
|
|
|
expected: []string{
|
2021-12-21 05:59:09 +00:00
|
|
|
"D0, P[a], (!!str)::<cool id=\"hi\"><foo>bar</foo></cool>\n\n",
|
2021-12-21 05:52:54 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode value as xml string with custom indentation",
|
|
|
|
document: `{a: {cool: {foo: "bar", +id: hi}}}`,
|
2021-12-21 05:59:09 +00:00
|
|
|
expression: `{"cat": .a | to_xml(1)}`,
|
2021-12-21 05:52:54 +00:00
|
|
|
expected: []string{
|
2021-12-21 05:59:09 +00:00
|
|
|
"D0, P[], (!!map)::cat: |\n <cool id=\"hi\">\n <foo>bar</foo>\n </cool>\n",
|
2021-12-21 05:52:54 +00:00
|
|
|
},
|
|
|
|
},
|
2021-12-21 04:02:07 +00:00
|
|
|
{
|
|
|
|
description: "Decode a xml encoded string",
|
|
|
|
document: `a: "<foo>bar</foo>"`,
|
|
|
|
expression: `.b = (.a | from_xml)`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::a: \"<foo>bar</foo>\"\nb:\n foo: bar\n",
|
|
|
|
},
|
|
|
|
},
|
2022-02-22 22:26:35 +00:00
|
|
|
{
|
|
|
|
description: "Encode a string to base64",
|
|
|
|
document: "coolData: a special string",
|
|
|
|
expression: ".coolData | @base64",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[coolData], (!!str)::YSBzcGVjaWFsIHN0cmluZw==\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode a yaml document to base64",
|
|
|
|
subdescription: "Pipe through @yaml first to convert to a string, then use @base64 to encode it.",
|
|
|
|
document: "a: apple",
|
|
|
|
expression: "@yaml | @base64",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::YTogYXBwbGUK\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Decode a base64 encoded string",
|
|
|
|
subdescription: "Decoded data is assumed to be a string.",
|
|
|
|
document: "coolData: V29ya3Mgd2l0aCBVVEYtMTYg8J+Yig==",
|
|
|
|
expression: ".coolData | @base64d",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[coolData], (!!str)::Works with UTF-16 😊\n",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Decode a base64 encoded yaml document",
|
|
|
|
subdescription: "Pipe through `from_yaml` to parse the decoded base64 string as a yaml document.",
|
|
|
|
document: "coolData: YTogYXBwbGUK",
|
|
|
|
expression: ".coolData |= (@base64d | from_yaml)",
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (doc)::coolData:\n a: apple\n",
|
|
|
|
},
|
|
|
|
},
|
2022-05-27 00:56:43 +00:00
|
|
|
{
|
|
|
|
description: "empty base64 decode",
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `"" | @base64d`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!str)::\n",
|
|
|
|
},
|
|
|
|
},
|
2022-05-27 01:18:38 +00:00
|
|
|
{
|
|
|
|
description: "empty xml decode",
|
|
|
|
skipDoc: true,
|
|
|
|
expression: `"" | @xmld`,
|
|
|
|
expected: []string{
|
|
|
|
"D0, P[], (!!null)::\n",
|
|
|
|
},
|
|
|
|
},
|
2021-10-22 01:00:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 01:37:47 +00:00
|
|
|
func TestEncoderDecoderOperatorScenarios(t *testing.T) {
|
|
|
|
for _, tt := range encoderDecoderOperatorScenarios {
|
2021-10-22 01:00:47 +00:00
|
|
|
testScenario(t, &tt)
|
|
|
|
}
|
2021-12-21 04:02:07 +00:00
|
|
|
documentOperatorScenarios(t, "encode-decode", encoderDecoderOperatorScenarios)
|
2021-10-22 01:00:47 +00:00
|
|
|
}
|