From 447003080102a26a6ada85d0683ab68c577272c4 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Sat, 5 Aug 2023 15:04:23 +0200 Subject: [PATCH] Expand Lua examples --- pkg/yqlib/doc/usage/lua.md | 103 ++++++++++++++++++++++++++++++++--- pkg/yqlib/lua_test.go | 109 +++++++++++++++++++++++++++++++++---- 2 files changed, 193 insertions(+), 19 deletions(-) diff --git a/pkg/yqlib/doc/usage/lua.md b/pkg/yqlib/doc/usage/lua.md index adb3d5f1ae..20df449cfd 100644 --- a/pkg/yqlib/doc/usage/lua.md +++ b/pkg/yqlib/doc/usage/lua.md @@ -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 @@ -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); + }, }; }; ``` diff --git a/pkg/yqlib/lua_test.go b/pkg/yqlib/lua_test.go index 6c8b9916be..2b87128023 100644 --- a/pkg/yqlib/lua_test.go +++ b/pkg/yqlib/lua_test.go @@ -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); + }, }; }; `, @@ -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)) } @@ -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)) @@ -105,6 +184,14 @@ 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)) @@ -112,7 +199,7 @@ func documentLuaEncodeScenario(w *bufio.Writer, s formatScenario) { 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) {