mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Parse toml docs and tests
This commit is contained in:
parent
f116ef8fd0
commit
732a1e33d5
2
.gitignore
vendored
2
.gitignore
vendored
@ -41,6 +41,8 @@ yq*.snap
|
|||||||
|
|
||||||
test.yml
|
test.yml
|
||||||
test*.yml
|
test*.yml
|
||||||
|
test*.xml
|
||||||
|
test*.toml
|
||||||
test*.yaml
|
test*.yaml
|
||||||
0.yml
|
0.yml
|
||||||
1.yml
|
1.yml
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
setUp() {
|
setUp() {
|
||||||
rm test*.yml 2>/dev/null || true
|
rm test*.yml 2>/dev/null || true
|
||||||
|
rm test*.toml 2>/dev/null || true
|
||||||
rm test*.tfstate 2>/dev/null || true
|
rm test*.tfstate 2>/dev/null || true
|
||||||
rm test*.json 2>/dev/null || true
|
rm test*.json 2>/dev/null || true
|
||||||
rm test*.properties 2>/dev/null || true
|
rm test*.properties 2>/dev/null || true
|
||||||
@ -30,6 +31,26 @@ EOM
|
|||||||
assertEquals "$expected" "$X"
|
assertEquals "$expected" "$X"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
testInputToml() {
|
||||||
|
cat >test.toml <<EOL
|
||||||
|
[owner]
|
||||||
|
name = "Tom Preston-Werner"
|
||||||
|
dob = 1979-05-27T07:32:00-08:00
|
||||||
|
EOL
|
||||||
|
|
||||||
|
read -r -d '' expected << EOM
|
||||||
|
owner:
|
||||||
|
name: Tom Preston-Werner
|
||||||
|
dob: 1979-05-27T07:32:00-08:00
|
||||||
|
EOM
|
||||||
|
|
||||||
|
X=$(./yq -oy test.toml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
|
||||||
|
X=$(./yq ea -oy test.toml)
|
||||||
|
assertEquals "$expected" "$X"
|
||||||
|
}
|
||||||
|
|
||||||
testInputTfstate() {
|
testInputTfstate() {
|
||||||
cat >test.tfstate <<EOL
|
cat >test.tfstate <<EOL
|
||||||
{ "mike" : { "things": "cool" } }
|
{ "mike" : { "things": "cool" } }
|
||||||
|
@ -75,6 +75,9 @@ func initCommand(cmd *cobra.Command, args []string) (string, []string, error) {
|
|||||||
}
|
}
|
||||||
} else if isAutomaticOutputFormat() {
|
} else if isAutomaticOutputFormat() {
|
||||||
// automatic input worked, we can do it for output too unless specified
|
// automatic input worked, we can do it for output too unless specified
|
||||||
|
if inputFormat == "toml" {
|
||||||
|
return "", nil, fmt.Errorf("toml is not yet supported as an output format. Please specify another output format using the [--output-format/-o] flag")
|
||||||
|
}
|
||||||
outputFormat = inputFormat
|
outputFormat = inputFormat
|
||||||
}
|
}
|
||||||
} else if isAutomaticOutputFormat() {
|
} else if isAutomaticOutputFormat() {
|
||||||
|
4
pkg/yqlib/doc/usage/headers/toml.md
Normal file
4
pkg/yqlib/doc/usage/headers/toml.md
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# TOML
|
||||||
|
|
||||||
|
Decode from TOML. Note that `yq` does not yet support outputting in TOML format (and therefore it cannot roundtrip)
|
||||||
|
|
111
pkg/yqlib/doc/usage/toml.md
Normal file
111
pkg/yqlib/doc/usage/toml.md
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
# TOML
|
||||||
|
|
||||||
|
Decode from TOML. Note that `yq` does not yet support outputting in TOML format (and therefore it cannot roundtrip)
|
||||||
|
|
||||||
|
|
||||||
|
## Parse: Simple
|
||||||
|
Given a sample.toml file of:
|
||||||
|
```toml
|
||||||
|
A = "hello"
|
||||||
|
B = 12
|
||||||
|
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -oy '.' sample.toml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
A: hello
|
||||||
|
B: 12
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parse: Deep paths
|
||||||
|
Given a sample.toml file of:
|
||||||
|
```toml
|
||||||
|
person.name = "hello"
|
||||||
|
person.address = "12 cat st"
|
||||||
|
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -oy '.' sample.toml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
person:
|
||||||
|
name: hello
|
||||||
|
address: 12 cat st
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parse: inline table
|
||||||
|
Given a sample.toml file of:
|
||||||
|
```toml
|
||||||
|
name = { first = "Tom", last = "Preston-Werner" }
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -oy '.' sample.toml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
name:
|
||||||
|
first: Tom
|
||||||
|
last: Preston-Werner
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parse: Array Table
|
||||||
|
Given a sample.toml file of:
|
||||||
|
```toml
|
||||||
|
|
||||||
|
[owner.contact]
|
||||||
|
name = "Tom Preston-Werner"
|
||||||
|
age = 36
|
||||||
|
|
||||||
|
[[owner.addresses]]
|
||||||
|
street = "first street"
|
||||||
|
suburb = "ok"
|
||||||
|
|
||||||
|
[[owner.addresses]]
|
||||||
|
street = "second street"
|
||||||
|
suburb = "nice"
|
||||||
|
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -oy '.' sample.toml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
owner:
|
||||||
|
contact:
|
||||||
|
name: Tom Preston-Werner
|
||||||
|
age: 36
|
||||||
|
addresses:
|
||||||
|
- street: first street
|
||||||
|
suburb: ok
|
||||||
|
- street: second street
|
||||||
|
suburb: nice
|
||||||
|
```
|
||||||
|
|
||||||
|
## Parse: with header
|
||||||
|
Given a sample.toml file of:
|
||||||
|
```toml
|
||||||
|
|
||||||
|
[servers]
|
||||||
|
|
||||||
|
[servers.alpha]
|
||||||
|
ip = "10.0.0.1"
|
||||||
|
|
||||||
|
```
|
||||||
|
then
|
||||||
|
```bash
|
||||||
|
yq -oy '.' sample.toml
|
||||||
|
```
|
||||||
|
will output
|
||||||
|
```yaml
|
||||||
|
servers:
|
||||||
|
alpha:
|
||||||
|
ip: 10.0.0.1
|
||||||
|
```
|
||||||
|
|
@ -1,6 +1,8 @@
|
|||||||
package yqlib
|
package yqlib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/mikefarah/yq/v4/test"
|
"github.com/mikefarah/yq/v4/test"
|
||||||
@ -81,19 +83,19 @@ var tomlScenarios = []formatScenario{
|
|||||||
scenarioType: "decode-error",
|
scenarioType: "decode-error",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Simple",
|
description: "Parse: Simple",
|
||||||
input: "A = \"hello\"\nB = 12\n",
|
input: "A = \"hello\"\nB = 12\n",
|
||||||
expected: "A: hello\nB: 12\n",
|
expected: "A: hello\nB: 12\n",
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Deep paths",
|
description: "Parse: Deep paths",
|
||||||
input: "person.name = \"hello\"\nperson.address = \"12 cat st\"\n",
|
input: "person.name = \"hello\"\nperson.address = \"12 cat st\"\n",
|
||||||
expected: "person:\n name: hello\n address: 12 cat st\n",
|
expected: "person:\n name: hello\n address: 12 cat st\n",
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "Simpl nested",
|
skipDoc: true,
|
||||||
input: `A.B = "hello"`,
|
input: `A.B = "hello"`,
|
||||||
expected: "A:\n B: hello\n",
|
expected: "A:\n B: hello\n",
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
@ -149,8 +151,7 @@ var tomlScenarios = []formatScenario{
|
|||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
description: "Parse: inline table",
|
||||||
description: "inline table",
|
|
||||||
input: `name = { first = "Tom", last = "Preston-Werner" }`,
|
input: `name = { first = "Tom", last = "Preston-Werner" }`,
|
||||||
expected: "name:\n first: Tom\n last: Preston-Werner\n",
|
expected: "name:\n first: Tom\n last: Preston-Werner\n",
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
@ -162,13 +163,13 @@ var tomlScenarios = []formatScenario{
|
|||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
skipDoc: true,
|
description: "Parse: Array Table",
|
||||||
input: sampleArrayTable,
|
input: sampleArrayTable,
|
||||||
expected: sampleArrayTableExpected,
|
expected: sampleArrayTableExpected,
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
description: "example with header",
|
description: "Parse: with header",
|
||||||
input: sampleWithHeader,
|
input: sampleWithHeader,
|
||||||
expected: expectedSampleWithHeader,
|
expected: expectedSampleWithHeader,
|
||||||
scenarioType: "decode",
|
scenarioType: "decode",
|
||||||
@ -189,13 +190,50 @@ func testTomlScenario(t *testing.T, s formatScenario) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func documentTomlDecodeScenario(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.toml file of:\n")
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```toml\n%v\n```\n", s.input))
|
||||||
|
|
||||||
|
writeOrPanic(w, "then\n")
|
||||||
|
expression := s.expression
|
||||||
|
if expression == "" {
|
||||||
|
expression = "."
|
||||||
|
}
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```bash\nyq -oy '%v' sample.toml\n```\n", expression))
|
||||||
|
writeOrPanic(w, "will output\n")
|
||||||
|
|
||||||
|
writeOrPanic(w, fmt.Sprintf("```yaml\n%v```\n\n", mustProcessFormatScenario(s, NewTomlDecoder(), NewYamlEncoder(2, false, ConfiguredYamlPreferences))))
|
||||||
|
}
|
||||||
|
|
||||||
|
func documentTomlScenario(t *testing.T, w *bufio.Writer, i interface{}) {
|
||||||
|
s := i.(formatScenario)
|
||||||
|
|
||||||
|
if s.skipDoc {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch s.scenarioType {
|
||||||
|
case "", "decode":
|
||||||
|
documentTomlDecodeScenario(w, s)
|
||||||
|
|
||||||
|
default:
|
||||||
|
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTomlScenarios(t *testing.T) {
|
func TestTomlScenarios(t *testing.T) {
|
||||||
for _, tt := range tomlScenarios {
|
for _, tt := range tomlScenarios {
|
||||||
testTomlScenario(t, tt)
|
testTomlScenario(t, tt)
|
||||||
}
|
}
|
||||||
// genericScenarios := make([]interface{}, len(xmlScenarios))
|
genericScenarios := make([]interface{}, len(tomlScenarios))
|
||||||
// for i, s := range xmlScenarios {
|
for i, s := range tomlScenarios {
|
||||||
// genericScenarios[i] = s
|
genericScenarios[i] = s
|
||||||
// }
|
}
|
||||||
// documentScenarios(t, "usage", "xml", genericScenarios, documentXMLScenario)
|
documentScenarios(t, "usage", "toml", genericScenarios, documentTomlScenario)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user