Skip to content

Commit d302d75

Browse files
authored
Implement basic Lua output support (#1745)
* Implement basic Lua output support Ref #1700 Basic but working serialization to Lua tables. * Escape larger set of characters in Lua output Started with a minimum of replacements, this should be more complete, tho not all substitutions are strictly required in Lua. * Print simple keys unquoted in Lua output String keys that satisfy the requirements for variable names can be used as keys without quotes in tables. * Quote Lua keywords in table keys Keywords are not valid as unquoted keys, thus must be quoted * Make output of unquoted Lua table keys optional Generally safer and simpler to not do it. * Hook up settings for Lua output * Allow special characters in Lua prefix and suffix --lua-suffix='});^M' didn't work, so taking this approach instead * Panic on unhandled YAML Kind in Lua encoder * Handle YAML case varied booleans in Lua encoder * Handle special-case numbers in Lua encoder * Reject unhandled scalar Tags in Lua encoder * Add note about how Lua nil is unsuitable as table key Could add some context tracking in the future to allow rejecting nil in a table key context. * Return error instead of panic in Lua encoder * Add initial test for Lua encoder Boilerplate mostly copied from toml_test.go * Additional Lua output tests * Generate Lua encoder documentation Mostly just for the boilerplate * Convert octal for Lua output Lua doesn't have the 0oNNN syntax for octal integers, only decimal and hexadecimal, hence those can be passed trough as is while octal needs special treatment. * Implement indentation in in Lua output * Respect string Style in Lua encoder Lua has 'single', "double" and [[ long ]] strings. * Expand Lua examples * Output line comments in Lua output * Implement Lua globals output mode
1 parent 9b40829 commit d302d75

File tree

7 files changed

+759
-1
lines changed

7 files changed

+759
-1
lines changed

cmd/root.go

+5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ yq -P sample.json
7878
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipProcInst, "xml-skip-proc-inst", yqlib.ConfiguredXMLPreferences.SkipProcInst, "skip over process instructions (e.g. <?xml version=\"1\"?>)")
7979
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredXMLPreferences.SkipDirectives, "xml-skip-directives", yqlib.ConfiguredXMLPreferences.SkipDirectives, "skip over directives (e.g. <!DOCTYPE thing cat>)")
8080

81+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredLuaPreferences.DocPrefix, "lua-prefix", yqlib.ConfiguredLuaPreferences.DocPrefix, "prefix")
82+
rootCmd.PersistentFlags().StringVar(&yqlib.ConfiguredLuaPreferences.DocSuffix, "lua-suffix", yqlib.ConfiguredLuaPreferences.DocSuffix, "suffix")
83+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredLuaPreferences.UnquotedKeys, "lua-unquoted", yqlib.ConfiguredLuaPreferences.UnquotedKeys, "output unquoted string keys (e.g. {foo=\"bar\"})")
84+
rootCmd.PersistentFlags().BoolVar(&yqlib.ConfiguredLuaPreferences.Globals, "lua-globals", yqlib.ConfiguredLuaPreferences.Globals, "output keys as top-level global variables")
85+
8186
rootCmd.PersistentFlags().BoolVarP(&nullInput, "null-input", "n", false, "Don't read input, simply evaluate the expression given. Useful for creating docs from scratch.")
8287
rootCmd.PersistentFlags().BoolVarP(&noDocSeparators, "no-doc", "N", false, "Don't print document separators (---)")
8388

cmd/utils.go

+2
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ func createEncoder(format yqlib.PrinterOutputFormat) (yqlib.Encoder, error) {
197197
return yqlib.NewTomlEncoder(), nil
198198
case yqlib.ShellVariablesOutputFormat:
199199
return yqlib.NewShellVariablesEncoder(), nil
200+
case yqlib.LuaOutputFormat:
201+
return yqlib.NewLuaEncoder(yqlib.ConfiguredLuaPreferences), nil
200202
}
201203
return nil, fmt.Errorf("invalid encoder: %v", format)
202204
}

pkg/yqlib/doc/usage/lua.md

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
## Basic example
3+
Given a sample.yml file of:
4+
```yaml
5+
---
6+
country: Australia # this place
7+
cities:
8+
- Sydney
9+
- Melbourne
10+
- Brisbane
11+
- Perth
12+
```
13+
then
14+
```bash
15+
yq -o=lua '.' sample.yml
16+
```
17+
will output
18+
```lua
19+
return {
20+
["country"] = "Australia"; -- this place
21+
["cities"] = {
22+
"Sydney",
23+
"Melbourne",
24+
"Brisbane",
25+
"Perth",
26+
};
27+
};
28+
```
29+
30+
## Unquoted keys
31+
Uses the `--lua-unquoted` option to produce a nicer-looking output.
32+
33+
Given a sample.yml file of:
34+
```yaml
35+
---
36+
country: Australia # this place
37+
cities:
38+
- Sydney
39+
- Melbourne
40+
- Brisbane
41+
- Perth
42+
```
43+
then
44+
```bash
45+
yq -o=lua '.' sample.yml
46+
```
47+
will output
48+
```lua
49+
return {
50+
country = "Australia"; -- this place
51+
cities = {
52+
"Sydney",
53+
"Melbourne",
54+
"Brisbane",
55+
"Perth",
56+
};
57+
};
58+
```
59+
60+
## Globals
61+
Uses the `--lua-globals` option to export the values into the global scope.
62+
63+
Given a sample.yml file of:
64+
```yaml
65+
---
66+
country: Australia # this place
67+
cities:
68+
- Sydney
69+
- Melbourne
70+
- Brisbane
71+
- Perth
72+
```
73+
then
74+
```bash
75+
yq -o=lua '.' sample.yml
76+
```
77+
will output
78+
```lua
79+
country = "Australia"; -- this place
80+
cities = {
81+
"Sydney",
82+
"Melbourne",
83+
"Brisbane",
84+
"Perth",
85+
};
86+
```
87+
88+
## Elaborate example
89+
Given a sample.yml file of:
90+
```yaml
91+
---
92+
hello: world
93+
tables:
94+
like: this
95+
keys: values
96+
? look: non-string keys
97+
: True
98+
numbers:
99+
- decimal: 12345
100+
- hex: 0x7fabc123
101+
- octal: 0o30
102+
- float: 123.45
103+
- infinity: .inf
104+
- not: .nan
105+
106+
```
107+
then
108+
```bash
109+
yq -o=lua '.' sample.yml
110+
```
111+
will output
112+
```lua
113+
return {
114+
["hello"] = "world";
115+
["tables"] = {
116+
["like"] = "this";
117+
["keys"] = "values";
118+
[{
119+
["look"] = "non-string keys";
120+
}] = true;
121+
};
122+
["numbers"] = {
123+
{
124+
["decimal"] = 12345;
125+
},
126+
{
127+
["hex"] = 0x7fabc123;
128+
},
129+
{
130+
["octal"] = 24;
131+
},
132+
{
133+
["float"] = 123.45;
134+
},
135+
{
136+
["infinity"] = (1/0);
137+
},
138+
{
139+
["not"] = (0/0);
140+
},
141+
};
142+
};
143+
```
144+

0 commit comments

Comments
 (0)