Skip to content

Commit

Permalink
Expand Lua examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Zash committed Aug 5, 2023
1 parent f5715ee commit 4470030
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 19 deletions.
103 changes: 95 additions & 8 deletions pkg/yqlib/doc/usage/lua.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,77 @@
Given a sample.yml file of:
```yaml
---
country: Australia # this place
cities:
- Sydney
- Melbourne
- Brisbane
- Perth
```
then
```bash
yq -o=lua '.' sample.yml
```
will output
```lua
return {
["country"] = "Australia";
["cities"] = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
};
```

## Unquoted keys
Uses the `--lua-unquoted-keys` option to produce a nicer-looking outpt.

Given a sample.yml file of:
```yaml
---
country: Australia # this place
cities:
- Sydney
- Melbourne
- Brisbane
- Perth
```
then
```bash
yq -o=lua '.' sample.yml
```
will output
```lua
return {
country = "Australia";
cities = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
};
```

## Elaborate example
Given a sample.yml file of:
```yaml
---
hello: world
? look: non-string keys
: True
numbers: [123,456]
tables:
like: this
keys: values
? look: non-string keys
: True
numbers:
- decimal: 12345
- hex: 0x7fabc123
- octal: 0o30
- float: 123.45
- infinity: .inf
- not: .nan

```
then
Expand All @@ -17,12 +84,32 @@ will output
```lua
return {
["hello"] = "world";
[{
["look"] = "non-string keys";
}] = true;
["tables"] = {
["like"] = "this";
["keys"] = "values";
[{
["look"] = "non-string keys";
}] = true;
};
["numbers"] = {
123,
456,
{
["decimal"] = 12345;
},
{
["hex"] = 0x7fabc123;
},
{
["octal"] = 24;
},
{
["float"] = 123.45;
},
{
["infinity"] = (1/0);
},
{
["not"] = (0/0);
},
};
};
```
Expand Down
109 changes: 98 additions & 11 deletions pkg/yqlib/lua_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,94 @@ import (

var luaScenarios = []formatScenario{
{
description: "Basic example",
description: "Basic example",
scenarioType: "encode",
input: `---
country: Australia # this place
cities:
- Sydney
- Melbourne
- Brisbane
- Perth`,
// TODO comments, -- this place
expected: `return {
["country"] = "Australia";
["cities"] = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
};
`,
},
{
description: "Unquoted keys",
subdescription: "Uses the `--lua-unquoted-keys` option to produce a nicer-looking outpt.",
scenarioType: "unquoted-encode",
input: `---
country: Australia # this place
cities:
- Sydney
- Melbourne
- Brisbane
- Perth`,
expected: `return {
country = "Australia";
cities = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
};
`,
},
{
description: "Elaborate example",
input: `---
hello: world
? look: non-string keys
: True
numbers: [123,456]
tables:
like: this
keys: values
? look: non-string keys
: True
numbers:
- decimal: 12345
- hex: 0x7fabc123
- octal: 0o30
- float: 123.45
- infinity: .inf
- not: .nan
`,
expected: `return {
["hello"] = "world";
[{
["look"] = "non-string keys";
}] = true;
["tables"] = {
["like"] = "this";
["keys"] = "values";
[{
["look"] = "non-string keys";
}] = true;
};
["numbers"] = {
123,
456,
{
["decimal"] = 12345;
},
{
["hex"] = 0x7fabc123;
},
{
["octal"] = 24;
},
{
["float"] = 123.45;
},
{
["infinity"] = (1/0);
},
{
["not"] = (0/0);
},
};
};
`,
Expand Down Expand Up @@ -78,6 +151,12 @@ func testLuaScenario(t *testing.T, s formatScenario) {
switch s.scenarioType {
case "encode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewLuaEncoder(ConfiguredLuaPreferences)), s.description)
case "unquoted-encode":
test.AssertResultWithContext(t, s.expected, mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewLuaEncoder(LuaPreferences{
DocPrefix: "return ",
DocSuffix: ";\n",
UnquotedKeys: true,
})), s.description)
default:
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
}
Expand All @@ -90,7 +169,7 @@ func documentLuaScenario(t *testing.T, w *bufio.Writer, i interface{}) {
return
}
switch s.scenarioType {
case "encode":
case "encode", "unquoted-encode":
documentLuaEncodeScenario(w, s)
default:
panic(fmt.Sprintf("unhandled scenario type %q", s.scenarioType))
Expand All @@ -105,14 +184,22 @@ func documentLuaEncodeScenario(w *bufio.Writer, s formatScenario) {
writeOrPanic(w, "\n\n")
}

prefs := ConfiguredLuaPreferences
if s.scenarioType == "unquoted-encode" {
prefs = LuaPreferences{
DocPrefix: "return ",
DocSuffix: ";\n",
UnquotedKeys: true,
}
}
writeOrPanic(w, "Given a sample.yml file of:\n")
writeOrPanic(w, fmt.Sprintf("```yaml\n%v\n```\n", s.input))

writeOrPanic(w, "then\n")
writeOrPanic(w, "```bash\nyq -o=lua '.' sample.yml\n```\n")
writeOrPanic(w, "will output\n")

writeOrPanic(w, fmt.Sprintf("```lua\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewLuaEncoder(ConfiguredLuaPreferences))))
writeOrPanic(w, fmt.Sprintf("```lua\n%v```\n\n", mustProcessFormatScenario(s, NewYamlDecoder(ConfiguredYamlPreferences), NewLuaEncoder(prefs))))
}

func TestLuaScenarios(t *testing.T) {
Expand Down

0 comments on commit 4470030

Please sign in to comment.