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

This commit is contained in:
Mike Farah 2023-10-17 14:57:07 +11:00
commit 20fdc250d3
8 changed files with 363 additions and 157 deletions

View File

@ -1,4 +1,4 @@
FROM golang:1.21.1 as builder
FROM golang:1.21.3 as builder
WORKDIR /go/src/mikefarah/yq

View File

@ -1,4 +1,4 @@
FROM golang:1.21.1
FROM golang:1.21.3
COPY scripts/devtools.sh /opt/devtools.sh
@ -6,5 +6,7 @@ RUN set -e -x \
&& /opt/devtools.sh
ENV PATH=/go/bin:$PATH
RUN apt-get update && apt-get install -y npm && npm install -g npx cspell@latest
ENV CGO_ENABLED 0
ENV GOPATH /go:/yq

View File

@ -271,4 +271,43 @@ EOM
assertEquals "$expected" "$X"
}
source ./scripts/shunit2
testLuaOutputPretty() {
cat >test.yml <<EOL
animals:
cat: meow
EOL
read -r -d '' expected << EOM
return {
["animals"] = {
["cat"] = "meow";
};
};
EOM
X=$(./yq e --output-format=lua test.yml)
assertEquals "$expected" "$X"
X=$(./yq e --output-format=lua --prettyPrint test.yml)
assertEquals "$expected" "$X"
}
testLuaOutputSubset() {
cat >test.yml <<EOL
animals:
cat: meow
EOL
read -r -d '' expected << EOM
return {
["cat"] = "meow";
};
EOM
X=$(./yq e --output-format=lua '.animals' test.yml)
assertEquals "$expected" "$X"
}
source ./scripts/shunit2

6
go.mod
View File

@ -3,7 +3,7 @@ module github.com/mikefarah/yq/v4
require (
github.com/a8m/envsubst v1.4.2
github.com/alecthomas/participle/v2 v2.1.0
github.com/alecthomas/repr v0.2.0
github.com/alecthomas/repr v0.3.0
github.com/dimchansky/utfbom v1.1.1
github.com/elliotchance/orderedmap v1.5.0
github.com/fatih/color v1.15.0
@ -16,7 +16,7 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/yuin/gopher-lua v1.1.0
golang.org/x/net v0.15.0
golang.org/x/net v0.17.0
golang.org/x/text v0.13.0
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
gopkg.in/yaml.v3 v3.0.1
@ -26,7 +26,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
)

4
go.sum
View File

@ -3,8 +3,8 @@ github.com/a8m/envsubst v1.4.2/go.mod h1:MVUTQNGQ3tsjOOtKCNd+fl8RzhsXcDvvAEzkhGt
github.com/alecthomas/assert/v2 v2.3.0 h1:mAsH2wmvjsuvyBvAmCtm7zFsBlb8mIHx5ySLVdDZXL0=
github.com/alecthomas/participle/v2 v2.1.0 h1:z7dElHRrOEEq45F2TG5cbQihMtNTv8vwldytDj7Wrz4=
github.com/alecthomas/participle/v2 v2.1.0/go.mod h1:Y1+hAs8DHPmc3YUFzqllV+eSQ9ljPTk0ZkPMtEdAx2c=
github.com/alecthomas/repr v0.2.0 h1:HAzS41CIzNW5syS8Mf9UwXhNH1J9aix/BvDRf1Ml2Yk=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/alecthomas/repr v0.3.0 h1:NeYzUPfjjlqHY4KtzgKJiWd6sVq2eNUPTi34PiFGjY8=
github.com/alecthomas/repr v0.3.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=

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);
},
};
};
```

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)")

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) {