mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Added JSON conversion tests and doc generation
This commit is contained in:
parent
e347090571
commit
2526b03d67
@ -44,11 +44,11 @@ expression and prints the result in sequence.`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func processExpression(expression string) string {
|
func processExpression(expression string) string {
|
||||||
var prettyPrintExp = `(... | (select(tag != "!!str"), select(tag == "!!str") | select(test("(?i)^(y|yes|n|no|on|off)$") | not)) ) style=""`
|
|
||||||
if prettyPrint && expression == "" {
|
if prettyPrint && expression == "" {
|
||||||
return prettyPrintExp
|
return yqlib.PrettyPrintExp
|
||||||
} else if prettyPrint {
|
} else if prettyPrint {
|
||||||
return fmt.Sprintf("%v | %v", expression, prettyPrintExp)
|
return fmt.Sprintf("%v | %v", expression, yqlib.PrettyPrintExp)
|
||||||
}
|
}
|
||||||
return expression
|
return expression
|
||||||
}
|
}
|
||||||
|
126
pkg/yqlib/doc/usage/convert.md
Normal file
126
pkg/yqlib/doc/usage/convert.md
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
# JSON
|
||||||
|
|
||||||
|
Encode and decode to and from JSON. Note that YAML is a _superset_ of JSON - so `yq` can read any json file without doing anything special.
|
||||||
|
|
||||||
|
This means you don't need to 'convert' a JSON file to YAML - however if you want idiomatic YAML styling, then you can use the `-P/--prettyPrint` flag, see examples below.
|
||||||
|
|
||||||
|
## Parse json: simple
|
||||||
|
JSON is a subset of yaml, so all you need to do is prettify the output
|
||||||
|
|
||||||
|
Given a sample.json file of:
|
||||||
|
```json
|
||||||
|
{"cat": "meow"}
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq e -P '.' sample.json
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
cat: meow
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parse json: complex
|
||||||
|
JSON is a subset of yaml, so all you need to do is prettify the output
|
||||||
|
|
||||||
|
Given a sample.json file of:
|
||||||
|
```json
|
||||||
|
{"a":"Easy! as one two three","b":{"c":2,"d":[3,4]}}
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq e -P '.' sample.json
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
a: Easy! as one two three
|
||||||
|
b:
|
||||||
|
c: 2
|
||||||
|
d:
|
||||||
|
- 3
|
||||||
|
- 4
|
||||||
|
```
|
||||||
|
|
||||||
|
## Encode json: simple
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
cat: meow
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq e -o=json '.' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"cat": "meow"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Encode json: simple - in one line
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
cat: meow # this is a comment, and it will be dropped.
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq e -o=json -I=0 '.' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```json
|
||||||
|
{"cat":"meow"}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Encode json: comments
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
cat: meow # this is a comment, and it will be dropped.
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq e -o=json '.' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"cat": "meow"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Encode json: anchors
|
||||||
|
Anchors are dereferenced
|
||||||
|
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
cat: &ref meow
|
||||||
|
anotherCat: *ref
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq e -o=json '.' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"cat": "meow",
|
||||||
|
"anotherCat": "meow"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Encode json: multiple results
|
||||||
|
Each matching node is converted into a json doc. This is best used with 0 indent (json document per line)
|
||||||
|
|
||||||
|
Given a sample.yml file of:
|
||||||
|
```yaml
|
||||||
|
things: [{stuff: cool}, {whatever: cat}]
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq e -o=json -I=0 '.things[]' sample.yml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```json
|
||||||
|
{"stuff":"cool"}
|
||||||
|
{"whatever":"cat"}
|
||||||
|
```
|
||||||
|
|
5
pkg/yqlib/doc/usage/headers/convert.md
Normal file
5
pkg/yqlib/doc/usage/headers/convert.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# JSON
|
||||||
|
|
||||||
|
Encode and decode to and from JSON. Note that YAML is a _superset_ of JSON - so `yq` can read any json file without doing anything special.
|
||||||
|
|
||||||
|
This means you don't need to 'convert' a JSON file to YAML - however if you want idiomatic YAML styling, then you can use the `-P/--prettyPrint` flag, see examples below.
|
229
pkg/yqlib/json_test.go
Normal file
229
pkg/yqlib/json_test.go
Normal file
@ -0,0 +1,229 @@
|
|||||||
|
package yqlib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mikefarah/yq/v4/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
var complexExpectYaml = `D0, P[], (!!map)::a: Easy! as one two three
|
||||||
|
b:
|
||||||
|
c: 2
|
||||||
|
d:
|
||||||
|
- 3
|
||||||
|
- 4
|
||||||
|
`
|
||||||
|
|
||||||
|
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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func decodeJson(t *testing.T, jsonString string) *CandidateNode {
|
||||||
|
docs, err := readDocumentWithLeadingContent(jsonString, "sample.json", 0)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
exp, err := NewExpressionParser().ParseExpression(PrettyPrintExp)
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testJsonScenario(t *testing.T, s formatScenario) {
|
||||||
|
if s.scenarioType == "encode" || s.scenarioType == "roundtrip" {
|
||||||
|
test.AssertResultWithContext(t, s.expected, processJsonScenario(s), s.description)
|
||||||
|
} else {
|
||||||
|
var actual = resultToString(t, decodeJson(t, s.input))
|
||||||
|
test.AssertResultWithContext(t, s.expected, actual, s.description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func processJsonScenario(s formatScenario) string {
|
||||||
|
|
||||||
|
var output bytes.Buffer
|
||||||
|
writer := bufio.NewWriter(&output)
|
||||||
|
|
||||||
|
var encoder = NewJsonEncoder(s.indent)
|
||||||
|
|
||||||
|
var decoder = NewYamlDecoder()
|
||||||
|
|
||||||
|
inputs, err := readDocuments(strings.NewReader(s.input), "sample.yml", 0, decoder)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
expression := s.expression
|
||||||
|
if expression == "" {
|
||||||
|
expression = "."
|
||||||
|
}
|
||||||
|
|
||||||
|
exp, err := NewExpressionParser().ParseExpression(expression)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
context, err := NewDataTreeNavigator().GetMatchingNodes(Context{MatchingNodes: inputs}, exp)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
printer := NewPrinter(encoder, NewSinglePrinterWriter(writer))
|
||||||
|
err = printer.PrintResults(context.MatchingNodes)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
writer.Flush()
|
||||||
|
|
||||||
|
return output.String()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentJsonDecodeScenario(t *testing.T, 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")
|
||||||
|
writeOrPanic(w, "```bash\nyq e -P '.' sample.json\n```\n")
|
||||||
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
|
var output bytes.Buffer
|
||||||
|
printer := NewSimpleYamlPrinter(bufio.NewWriter(&output), YamlOutputFormat, true, false, 2, true)
|
||||||
|
|
||||||
|
node := decodeJson(t, s.input)
|
||||||
|
|
||||||
|
err := printer.PrintResults(node.AsList())
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", output.String()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentJsonScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||||
|
s := i.(formatScenario)
|
||||||
|
|
||||||
|
if s.skipDoc {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.scenarioType == "encode" {
|
||||||
|
documentJsonEncodeScenario(w, s)
|
||||||
|
} else {
|
||||||
|
documentJsonDecodeScenario(t, w, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentJsonEncodeScenario(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.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 {
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq e -o=json '%v' sample.yml\n```\n", expression))
|
||||||
|
} else {
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq e -o=json -I=%v '%v' sample.yml\n```\n", s.indent, expression))
|
||||||
|
}
|
||||||
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```json\n%v```\n\n", processJsonScenario(s)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestJsonScenarios(t *testing.T) {
|
||||||
|
for _, tt := range jsonScenarios {
|
||||||
|
testJsonScenario(t, tt)
|
||||||
|
}
|
||||||
|
genericScenarios := make([]interface{}, len(jsonScenarios))
|
||||||
|
for i, s := range jsonScenarios {
|
||||||
|
genericScenarios[i] = s
|
||||||
|
}
|
||||||
|
documentScenarios(t, "usage", "convert", genericScenarios, documentJsonScenario)
|
||||||
|
}
|
@ -20,6 +20,8 @@ type xmlPreferences struct {
|
|||||||
|
|
||||||
var log = logging.MustGetLogger("yq-lib")
|
var log = logging.MustGetLogger("yq-lib")
|
||||||
|
|
||||||
|
var PrettyPrintExp = `(... | (select(tag != "!!str"), select(tag == "!!str") | select(test("(?i)^(y|yes|n|no|on|off)$") | not)) ) style=""`
|
||||||
|
|
||||||
// GetLogger returns the yq logger instance.
|
// GetLogger returns the yq logger instance.
|
||||||
func GetLogger() *logging.Logger {
|
func GetLogger() *logging.Logger {
|
||||||
return log
|
return log
|
||||||
|
@ -51,6 +51,8 @@ func processXmlScenario(s formatScenario) string {
|
|||||||
|
|
||||||
type formatScenario struct {
|
type formatScenario struct {
|
||||||
input string
|
input string
|
||||||
|
indent int
|
||||||
|
expression string
|
||||||
expected string
|
expected string
|
||||||
description string
|
description string
|
||||||
subdescription string
|
subdescription string
|
||||||
|
Loading…
Reference in New Issue
Block a user