Merge remote-tracking branch 'origin/master' into generic_ast

This commit is contained in:
Mike Farah
2023-10-17 14:57:07 +11:00
8 changed files with 363 additions and 157 deletions
+175
View File
@@ -1 +1,176 @@
## Basic input example
Given a sample.lua file of:
```lua
return {
["country"] = "Australia"; -- this place
["cities"] = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
};
```
then
```bash
yq -oy '.' sample.lua
```
will output
```yaml
country: Australia
cities:
- Sydney
- Melbourne
- Brisbane
- Perth
```
## Basic output example
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"; -- this place
["cities"] = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
};
```
## Unquoted keys
Uses the `--lua-unquoted` option to produce a nicer-looking output.
Given a sample.yml file of:
```yaml
---
country: Australia # this place
cities:
- Sydney
- Melbourne
- Brisbane
- Perth
```
then
```bash
yq -o=lua --lua-unquoted '.' sample.yml
```
will output
```lua
return {
country = "Australia"; -- this place
cities = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
};
```
## Globals
Uses the `--lua-globals` option to export the values into the global scope.
Given a sample.yml file of:
```yaml
---
country: Australia # this place
cities:
- Sydney
- Melbourne
- Brisbane
- Perth
```
then
```bash
yq -o=lua --lua-globals '.' sample.yml
```
will output
```lua
country = "Australia"; -- this place
cities = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
```
## Elaborate example
Given a sample.yml file of:
```yaml
---
hello: world
tables:
like: this
keys: values
? look: non-string keys
: True
numbers:
- decimal: 12345
- hex: 0x7fabc123
- octal: 0o30
- float: 123.45
- infinity: .inf
plus_infinity: +.inf
minus_infinity: -.inf
- not: .nan
```
then
```bash
yq -o=lua '.' sample.yml
```
will output
```lua
return {
["hello"] = "world";
["tables"] = {
["like"] = "this";
["keys"] = "values";
[{
["look"] = "non-string keys";
}] = true;
};
["numbers"] = {
{
["decimal"] = 12345;
},
{
["hex"] = 0x7fabc123;
},
{
["octal"] = 24;
},
{
["float"] = 123.45;
},
{
["infinity"] = (1/0);
["plus_infinity"] = (1/0);
["minus_infinity"] = (-1/0);
},
{
["not"] = (0/0);
},
};
};
```
+1 -1
View File
@@ -289,7 +289,7 @@ func (le *luaEncoder) encodeAny(writer io.Writer, node *CandidateNode) error {
return writeString(writer, strings.ToLower(node.Value))
case "!!float":
switch strings.ToLower(node.Value) {
case ".inf":
case ".inf", "+.inf":
return writeString(writer, "(1/0)")
case "-.inf":
return writeString(writer, "(-1/0)")
+138 -148
View File
@@ -78,154 +78,144 @@ var luaScenarios = []formatScenario{
};
`,
},
// {
// description: "Unquoted keys",
// subdescription: "Uses the `--lua-unquoted` option to produce a nicer-looking output.",
// scenarioType: "unquoted-encode",
// input: `---
//
// country: Australia # this place
// cities:
// - Sydney
// - Melbourne
// - Brisbane
// - Perth`,
// expected: `return {
// country = "Australia"; -- this place
// cities = {
// "Sydney",
// "Melbourne",
// "Brisbane",
// "Perth",
// };
// };
//
// `,
//
// },
// {
// description: "Globals",
// subdescription: "Uses the `--lua-globals` option to export the values into the global scope.",
// scenarioType: "globals-encode",
// input: `---
//
// country: Australia # this place
// cities:
// - Sydney
// - Melbourne
// - Brisbane
// - Perth`,
// expected: `country = "Australia"; -- this place
//
// cities = {
// "Sydney",
// "Melbourne",
// "Brisbane",
// "Perth",
// };
//
// `,
//
// },
// {
// description: "Elaborate example",
// input: `---
//
// hello: world
// 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";
// ["tables"] = {
// ["like"] = "this";
// ["keys"] = "values";
// [{
// ["look"] = "non-string keys";
// }] = true;
// };
// ["numbers"] = {
// {
// ["decimal"] = 12345;
// },
// {
// ["hex"] = 0x7fabc123;
// },
// {
// ["octal"] = 24;
// },
// {
// ["float"] = 123.45;
// },
// {
// ["infinity"] = (1/0);
// },
// {
// ["not"] = (0/0);
// },
// };
// };
//
// `,
//
// scenarioType: "encode",
// },
// {
// skipDoc: true,
// description: "Sequence",
// input: "- a\n- b\n- c\n",
// expected: "return {\n\t\"a\",\n\t\"b\",\n\t\"c\",\n};\n",
// scenarioType: "encode",
// },
// {
// skipDoc: true,
// description: "Mapping",
// input: "a: b\nc:\n d: e\nf: 0\n",
// expected: "return {\n\t[\"a\"] = \"b\";\n\t[\"c\"] = {\n\t\t[\"d\"] = \"e\";\n\t};\n\t[\"f\"] = 0;\n};\n",
// scenarioType: "encode",
// },
// {
// skipDoc: true,
// description: "Scalar str",
// input: "str: |\n foo\n bar\nanother: 'single'\nand: \"double\"",
// expected: "return {\n\t[\"str\"] = [[\nfoo\nbar\n]];\n\t[\"another\"] = 'single';\n\t[\"and\"] = \"double\";\n};\n",
// scenarioType: "encode",
// },
// {
// skipDoc: true,
// description: "Scalar null",
// input: "x: null\n",
// expected: "return {\n\t[\"x\"] = nil;\n};\n",
// scenarioType: "encode",
// },
// {
// skipDoc: true,
// description: "Scalar int",
// input: "- 1\n- 2\n- 0x10\n- 0o30\n- -999\n",
// expected: "return {\n\t1,\n\t2,\n\t0x10,\n\t24,\n\t-999,\n};\n",
// scenarioType: "encode",
// },
// {
// skipDoc: true,
// description: "Scalar float",
// input: "- 1.0\n- 3.14\n- 1e100\n- .Inf\n- .NAN\n",
// expected: "return {\n\t1.0,\n\t3.14,\n\t1e100,\n\t(1/0),\n\t(0/0),\n};\n",
// scenarioType: "encode",
// },
{
description: "Unquoted keys",
subdescription: "Uses the `--lua-unquoted` option to produce a nicer-looking output.",
scenarioType: "unquoted-encode",
input: `---
country: Australia # this place
cities:
- Sydney
- Melbourne
- Brisbane
- Perth`,
expected: `return {
country = "Australia"; -- this place
cities = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
};
`,
},
{
description: "Globals",
subdescription: "Uses the `--lua-globals` option to export the values into the global scope.",
scenarioType: "globals-encode",
input: `---
country: Australia # this place
cities:
- Sydney
- Melbourne
- Brisbane
- Perth`,
expected: `country = "Australia"; -- this place
cities = {
"Sydney",
"Melbourne",
"Brisbane",
"Perth",
};
`,
},
{
description: "Elaborate example",
input: `---
hello: world
tables:
like: this
keys: values
? look: non-string keys
: True
numbers:
- decimal: 12345
- hex: 0x7fabc123
- octal: 0o30
- float: 123.45
- infinity: .inf
plus_infinity: +.inf
minus_infinity: -.inf
- not: .nan
`,
expected: `return {
["hello"] = "world";
["tables"] = {
["like"] = "this";
["keys"] = "values";
[{
["look"] = "non-string keys";
}] = true;
};
["numbers"] = {
{
["decimal"] = 12345;
},
{
["hex"] = 0x7fabc123;
},
{
["octal"] = 24;
},
{
["float"] = 123.45;
},
{
["infinity"] = (1/0);
["plus_infinity"] = (1/0);
["minus_infinity"] = (-1/0);
},
{
["not"] = (0/0);
},
};
};
`,
scenarioType: "encode",
},
{
skipDoc: true,
description: "Sequence",
input: "- a\n- b\n- c\n",
expected: "return {\n\t\"a\",\n\t\"b\",\n\t\"c\",\n};\n",
scenarioType: "encode",
},
{
skipDoc: true,
description: "Mapping",
input: "a: b\nc:\n d: e\nf: 0\n",
expected: "return {\n\t[\"a\"] = \"b\";\n\t[\"c\"] = {\n\t\t[\"d\"] = \"e\";\n\t};\n\t[\"f\"] = 0;\n};\n",
scenarioType: "encode",
},
{
skipDoc: true,
description: "Scalar str",
input: "str: |\n foo\n bar\nanother: 'single'\nand: \"double\"",
expected: "return {\n\t[\"str\"] = [[\nfoo\nbar\n]];\n\t[\"another\"] = 'single';\n\t[\"and\"] = \"double\";\n};\n",
scenarioType: "encode",
},
{
skipDoc: true,
description: "Scalar null",
input: "x: null\n",
expected: "return {\n\t[\"x\"] = nil;\n};\n",
scenarioType: "encode",
},
{
skipDoc: true,
description: "Scalar int",
input: "- 1\n- 2\n- 0x10\n- 0o30\n- -999\n",
expected: "return {\n\t1,\n\t2,\n\t0x10,\n\t24,\n\t-999,\n};\n",
scenarioType: "encode",
},
{
skipDoc: true,
description: "Scalar float",
input: "- 1.0\n- 3.14\n- 1e100\n- .Inf\n- .NAN\n",
expected: "return {\n\t1.0,\n\t3.14,\n\t1e100,\n\t(1/0),\n\t(0/0),\n};\n",
scenarioType: "encode",
},
}
func testLuaScenario(t *testing.T, s formatScenario) {