2022-01-21 09:26:09 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/mikefarah/yq/v4/test"
|
|
|
|
)
|
|
|
|
|
2022-06-25 02:22:03 +00:00
|
|
|
const complexExpectYaml = `D0, P[], (!!map)::a: Easy! as one two three
|
2022-01-21 09:26:09 +00:00
|
|
|
b:
|
|
|
|
c: 2
|
|
|
|
d:
|
|
|
|
- 3
|
|
|
|
- 4
|
|
|
|
`
|
|
|
|
|
2022-07-27 02:26:22 +00:00
|
|
|
const sampleNdJson = `{"this": "is a multidoc json file"}
|
|
|
|
{"each": ["line is a valid json document"]}
|
|
|
|
{"a number": 4}
|
|
|
|
`
|
|
|
|
|
|
|
|
const expectedNdJsonYaml = `this: is a multidoc json file
|
|
|
|
---
|
|
|
|
each:
|
|
|
|
- line is a valid json document
|
|
|
|
---
|
|
|
|
a number: 4
|
|
|
|
`
|
|
|
|
|
|
|
|
const expectedRoundTripSampleNdJson = `{"this":"is a multidoc json file"}
|
|
|
|
{"each":["line is a valid json document"]}
|
|
|
|
{"a number":4}
|
|
|
|
`
|
|
|
|
|
|
|
|
const expectedUpdatedMultilineJson = `{"this":"is a multidoc json file"}
|
|
|
|
{"each":["line is a valid json document","cool"]}
|
|
|
|
{"a number":4}
|
|
|
|
`
|
|
|
|
|
|
|
|
const sampleMultiLineJson = `{
|
|
|
|
"this": "is a multidoc json file"
|
|
|
|
}
|
|
|
|
{
|
|
|
|
"it": [
|
|
|
|
"has",
|
|
|
|
"consecutive",
|
|
|
|
"json documents"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
{
|
|
|
|
"a number": 4
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const roundTripMultiLineJson = `{
|
|
|
|
"this": "is a multidoc json file"
|
|
|
|
}
|
|
|
|
{
|
|
|
|
"it": [
|
|
|
|
"has",
|
|
|
|
"consecutive",
|
|
|
|
"json documents"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
{
|
|
|
|
"a number": 4
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2022-01-21 09:26:09 +00:00
|
|
|
var jsonScenarios = []formatScenario{
|
|
|
|
{
|
|
|
|
description: "Parse json: simple",
|
|
|
|
subdescription: "JSON is a subset of yaml, so all you need to do is prettify the output",
|
|
|
|
input: `{"cat": "meow"}`,
|
|
|
|
expected: "D0, P[], (!!map)::cat: meow\n",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Parse json: complex",
|
|
|
|
subdescription: "JSON is a subset of yaml, so all you need to do is prettify the output",
|
|
|
|
input: `{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}`,
|
|
|
|
expected: complexExpectYaml,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode json: simple",
|
|
|
|
input: `cat: meow`,
|
|
|
|
indent: 2,
|
|
|
|
expected: "{\n \"cat\": \"meow\"\n}\n",
|
|
|
|
scenarioType: "encode",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode json: simple - in one line",
|
|
|
|
input: `cat: meow # this is a comment, and it will be dropped.`,
|
|
|
|
indent: 0,
|
|
|
|
expected: "{\"cat\":\"meow\"}\n",
|
|
|
|
scenarioType: "encode",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode json: comments",
|
|
|
|
input: `cat: meow # this is a comment, and it will be dropped.`,
|
|
|
|
indent: 2,
|
|
|
|
expected: "{\n \"cat\": \"meow\"\n}\n",
|
|
|
|
scenarioType: "encode",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode json: anchors",
|
|
|
|
subdescription: "Anchors are dereferenced",
|
|
|
|
input: "cat: &ref meow\nanotherCat: *ref",
|
|
|
|
indent: 2,
|
|
|
|
expected: "{\n \"cat\": \"meow\",\n \"anotherCat\": \"meow\"\n}\n",
|
|
|
|
scenarioType: "encode",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Encode json: multiple results",
|
|
|
|
subdescription: "Each matching node is converted into a json doc. This is best used with 0 indent (json document per line)",
|
|
|
|
input: `things: [{stuff: cool}, {whatever: cat}]`,
|
|
|
|
expression: `.things[]`,
|
|
|
|
indent: 0,
|
|
|
|
expected: "{\"stuff\":\"cool\"}\n{\"whatever\":\"cat\"}\n",
|
|
|
|
scenarioType: "encode",
|
|
|
|
},
|
2022-07-27 02:26:22 +00:00
|
|
|
{
|
|
|
|
description: "Roundtrip NDJSON",
|
|
|
|
subdescription: "Unfortunately the json encoder strips leading spaces of values.",
|
|
|
|
input: sampleNdJson,
|
|
|
|
expected: expectedRoundTripSampleNdJson,
|
|
|
|
scenarioType: "roundtrip-ndjson",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Roundtrip multi-document JSON",
|
|
|
|
subdescription: "The NDJSON parser can also handle multiple multi-line json documents in a single file!",
|
|
|
|
input: sampleMultiLineJson,
|
|
|
|
expected: roundTripMultiLineJson,
|
|
|
|
scenarioType: "roundtrip-multi",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Update a specific document in a multi-document json",
|
|
|
|
subdescription: "Documents are indexed by the `documentIndex` or `di` operator.",
|
|
|
|
input: sampleNdJson,
|
|
|
|
expected: expectedUpdatedMultilineJson,
|
|
|
|
expression: `(select(di == 1) | .each ) += "cool"`,
|
|
|
|
scenarioType: "roundtrip-ndjson",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Find and update a specific document in a multi-document json",
|
|
|
|
subdescription: "Use expressions as you normally would.",
|
|
|
|
input: sampleNdJson,
|
|
|
|
expected: expectedUpdatedMultilineJson,
|
|
|
|
expression: `(select(has("each")) | .each ) += "cool"`,
|
|
|
|
scenarioType: "roundtrip-ndjson",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "Decode NDJSON",
|
|
|
|
input: sampleNdJson,
|
|
|
|
expected: expectedNdJsonYaml,
|
|
|
|
scenarioType: "decode-ndjson",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "numbers",
|
|
|
|
skipDoc: true,
|
|
|
|
input: "[3, 3.0, 3.1, -1]",
|
|
|
|
expected: "- 3\n- 3\n- 3.1\n- -1\n",
|
|
|
|
scenarioType: "decode-ndjson",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "strings",
|
|
|
|
skipDoc: true,
|
|
|
|
input: `["", "cat"]`,
|
|
|
|
expected: "- \"\"\n- cat\n",
|
|
|
|
scenarioType: "decode-ndjson",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "null",
|
|
|
|
skipDoc: true,
|
|
|
|
input: `null`,
|
|
|
|
expected: "null\n",
|
|
|
|
scenarioType: "decode-ndjson",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
description: "booleans",
|
|
|
|
skipDoc: true,
|
|
|
|
input: `[true, false]`,
|
|
|
|
expected: "- true\n- false\n",
|
|
|
|
scenarioType: "decode-ndjson",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func documentRoundtripNdJsonScenario(w *bufio.Writer, s formatScenario, indent int) {
|
|
|
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
|
|
|
|
|
|
|
if s.subdescription != "" {
|
|
|
|
writeOrPanic(w, s.subdescription)
|
|
|
|
writeOrPanic(w, "\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
writeOrPanic(w, "Given a sample.json file of:\n")
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```json\n%v\n```\n", s.input))
|
|
|
|
|
|
|
|
writeOrPanic(w, "then\n")
|
|
|
|
|
|
|
|
expression := s.expression
|
|
|
|
if expression != "" {
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=json -o=json -I=%v '%v' sample.json\n```\n", indent, expression))
|
|
|
|
} else {
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=json -o=json -I=%v sample.json\n```\n", indent))
|
|
|
|
}
|
|
|
|
|
|
|
|
writeOrPanic(w, "will output\n")
|
|
|
|
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(indent, false))))
|
|
|
|
}
|
|
|
|
|
|
|
|
func documentDecodeNdJsonScenario(w *bufio.Writer, s formatScenario) {
|
|
|
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
|
|
|
|
|
|
|
if s.subdescription != "" {
|
|
|
|
writeOrPanic(w, s.subdescription)
|
|
|
|
writeOrPanic(w, "\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
writeOrPanic(w, "Given a sample.json file of:\n")
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```json\n%v\n```\n", s.input))
|
|
|
|
|
|
|
|
writeOrPanic(w, "then\n")
|
|
|
|
|
|
|
|
expression := s.expression
|
|
|
|
if expression != "" {
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -p=json '%v' sample.json\n```\n", expression))
|
|
|
|
} else {
|
|
|
|
writeOrPanic(w, "```bash\nyq -p=json sample.json\n```\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
writeOrPanic(w, "will output\n")
|
|
|
|
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", processFormatScenario(s, NewJSONDecoder(), NewYamlEncoder(s.indent, false, true, true))))
|
2022-01-21 09:26:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
func decodeJSON(t *testing.T, jsonString string) *CandidateNode {
|
2022-01-21 09:26:09 +00:00
|
|
|
docs, err := readDocumentWithLeadingContent(jsonString, "sample.json", 0)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-01 03:47:51 +00:00
|
|
|
exp, err := getExpressionParser().ParseExpression(PrettyPrintExp)
|
2022-01-21 09:26:09 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: docs}, exp)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return context.MatchingNodes.Front().Value.(*CandidateNode)
|
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
func testJSONScenario(t *testing.T, s formatScenario) {
|
2022-06-25 02:22:03 +00:00
|
|
|
switch s.scenarioType {
|
|
|
|
case "encode", "decode":
|
2022-07-27 02:26:22 +00:00
|
|
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewYamlDecoder(), NewJSONEncoder(s.indent, false)), s.description)
|
2022-06-25 02:22:03 +00:00
|
|
|
case "":
|
2022-02-07 00:55:55 +00:00
|
|
|
var actual = resultToString(t, decodeJSON(t, s.input))
|
2022-01-21 09:26:09 +00:00
|
|
|
test.AssertResultWithContext(t, s.expected, actual, s.description)
|
2022-07-27 02:26:22 +00:00
|
|
|
case "decode-ndjson":
|
|
|
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewJSONDecoder(), NewYamlEncoder(2, false, true, true)), s.description)
|
|
|
|
case "roundtrip-ndjson":
|
|
|
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(0, false)), s.description)
|
|
|
|
case "roundtrip-multi":
|
|
|
|
test.AssertResultWithContext(t, s.expected, processFormatScenario(s, NewJSONDecoder(), NewJSONEncoder(2, false)), s.description)
|
2022-06-25 02:22:03 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
2022-01-21 09:26:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
func documentJSONDecodeScenario(t *testing.T, w *bufio.Writer, s formatScenario) {
|
2022-01-21 09:26:09 +00:00
|
|
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
|
|
|
|
|
|
|
if s.subdescription != "" {
|
|
|
|
writeOrPanic(w, s.subdescription)
|
|
|
|
writeOrPanic(w, "\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
writeOrPanic(w, "Given a sample.json file of:\n")
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```json\n%v\n```\n", s.input))
|
|
|
|
|
|
|
|
writeOrPanic(w, "then\n")
|
2022-01-27 06:21:10 +00:00
|
|
|
writeOrPanic(w, "```bash\nyq -P '.' sample.json\n```\n")
|
2022-01-21 09:26:09 +00:00
|
|
|
writeOrPanic(w, "will output\n")
|
|
|
|
|
|
|
|
var output bytes.Buffer
|
|
|
|
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true)
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
node := decodeJSON(t, s.input)
|
2022-01-21 09:26:09 +00:00
|
|
|
|
|
|
|
err := printer.PrintResults(node.AsList())
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
|
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
func documentJSONScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
2022-01-21 09:26:09 +00:00
|
|
|
s := i.(formatScenario)
|
|
|
|
if s.skipDoc {
|
|
|
|
return
|
|
|
|
}
|
2022-06-25 02:22:03 +00:00
|
|
|
switch s.scenarioType {
|
|
|
|
case "":
|
2022-02-07 00:55:55 +00:00
|
|
|
documentJSONDecodeScenario(t, w, s)
|
2022-06-25 02:22:03 +00:00
|
|
|
case "encode":
|
|
|
|
documentJSONEncodeScenario(w, s)
|
2022-07-27 02:26:22 +00:00
|
|
|
case "decode-ndjson":
|
|
|
|
documentDecodeNdJsonScenario(w, s)
|
|
|
|
case "roundtrip-ndjson":
|
|
|
|
documentRoundtripNdJsonScenario(w, s, 0)
|
|
|
|
case "roundtrip-multi":
|
|
|
|
documentRoundtripNdJsonScenario(w, s, 2)
|
2022-06-25 02:22:03 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
2022-01-21 09:26:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
func documentJSONEncodeScenario(w *bufio.Writer, s formatScenario) {
|
2022-01-21 09:26:09 +00:00
|
|
|
writeOrPanic(w, fmt.Sprintf("## %v\n", s.description))
|
|
|
|
|
|
|
|
if s.subdescription != "" {
|
|
|
|
writeOrPanic(w, s.subdescription)
|
|
|
|
writeOrPanic(w, "\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
writeOrPanic(w, "Given a sample.yml file of:\n")
|
|
|
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.input))
|
|
|
|
|
|
|
|
writeOrPanic(w, "then\n")
|
|
|
|
|
|
|
|
expression := s.expression
|
|
|
|
if expression == "" {
|
|
|
|
expression = "."
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.indent == 2 {
|
2022-01-27 06:21:10 +00:00
|
|
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=json '%v' sample.yml\n```\n", expression))
|
2022-01-21 09:26:09 +00:00
|
|
|
} else {
|
2022-01-27 06:21:10 +00:00
|
|
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -o=json -I=%v '%v' sample.yml\n```\n", s.indent, expression))
|
2022-01-21 09:26:09 +00:00
|
|
|
}
|
|
|
|
writeOrPanic(w, "will output\n")
|
|
|
|
|
2022-07-27 02:26:22 +00:00
|
|
|
writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processFormatScenario(s, NewYamlDecoder(), NewJSONEncoder(s.indent, false))))
|
2022-01-21 09:26:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 00:55:55 +00:00
|
|
|
func TestJSONScenarios(t *testing.T) {
|
2022-01-21 09:26:09 +00:00
|
|
|
for _, tt := range jsonScenarios {
|
2022-02-07 00:55:55 +00:00
|
|
|
testJSONScenario(t, tt)
|
2022-01-21 09:26:09 +00:00
|
|
|
}
|
|
|
|
genericScenarios := make([]interface{}, len(jsonScenarios))
|
|
|
|
for i, s := range jsonScenarios {
|
|
|
|
genericScenarios[i] = s
|
|
|
|
}
|
2022-02-07 00:55:55 +00:00
|
|
|
documentScenarios(t, "usage", "convert", genericScenarios, documentJSONScenario)
|
2022-01-21 09:26:09 +00:00
|
|
|
}
|