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 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 COPY scripts/devtools.sh /opt/devtools.sh
@ -6,5 +6,7 @@ RUN set -e -x \
&& /opt/devtools.sh && /opt/devtools.sh
ENV PATH=/go/bin:$PATH 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 CGO_ENABLED 0
ENV GOPATH /go:/yq ENV GOPATH /go:/yq

View File

@ -271,4 +271,43 @@ EOM
assertEquals "$expected" "$X" assertEquals "$expected" "$X"
} }
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 source ./scripts/shunit2

6
go.mod
View File

@ -3,7 +3,7 @@ module github.com/mikefarah/yq/v4
require ( require (
github.com/a8m/envsubst v1.4.2 github.com/a8m/envsubst v1.4.2
github.com/alecthomas/participle/v2 v2.1.0 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/dimchansky/utfbom v1.1.1
github.com/elliotchance/orderedmap v1.5.0 github.com/elliotchance/orderedmap v1.5.0
github.com/fatih/color v1.15.0 github.com/fatih/color v1.15.0
@ -16,7 +16,7 @@ require (
github.com/spf13/cobra v1.7.0 github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5
github.com/yuin/gopher-lua v1.1.0 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 golang.org/x/text v0.13.0
gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473 gopkg.in/op/go-logging.v1 v1.0.0-20160211212156-b2cb9fa56473
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
@ -26,7 +26,7 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // 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 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/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 h1:z7dElHRrOEEq45F2TG5cbQihMtNTv8vwldytDj7Wrz4=
github.com/alecthomas/participle/v2 v2.1.0/go.mod h1:Y1+hAs8DHPmc3YUFzqllV+eSQ9ljPTk0ZkPMtEdAx2c= 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.3.0 h1:NeYzUPfjjlqHY4KtzgKJiWd6sVq2eNUPTi34PiFGjY8=
github.com/alecthomas/repr v0.2.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= 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/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.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 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)) return writeString(writer, strings.ToLower(node.Value))
case "!!float": case "!!float":
switch strings.ToLower(node.Value) { switch strings.ToLower(node.Value) {
case ".inf": case ".inf", "+.inf":
return writeString(writer, "(1/0)") return writeString(writer, "(1/0)")
case "-.inf": case "-.inf":
return writeString(writer, "(-1/0)") return writeString(writer, "(-1/0)")

View File

@ -78,154 +78,144 @@ var luaScenarios = []formatScenario{
}; };
`, `,
}, },
// { {
// description: "Unquoted keys", description: "Unquoted keys",
// subdescription: "Uses the `--lua-unquoted` option to produce a nicer-looking output.", subdescription: "Uses the `--lua-unquoted` option to produce a nicer-looking output.",
// scenarioType: "unquoted-encode", scenarioType: "unquoted-encode",
// input: `--- input: `---
// country: Australia # this place
// country: Australia # this place cities:
// cities: - Sydney
// - Sydney - Melbourne
// - Melbourne - Brisbane
// - Brisbane - Perth`,
// - Perth`, expected: `return {
// expected: `return { country = "Australia"; -- this place
// country = "Australia"; -- this place cities = {
// cities = { "Sydney",
// "Sydney", "Melbourne",
// "Melbourne", "Brisbane",
// "Brisbane", "Perth",
// "Perth", };
// }; };
// }; `,
// },
// `, {
// description: "Globals",
// }, subdescription: "Uses the `--lua-globals` option to export the values into the global scope.",
// { scenarioType: "globals-encode",
// description: "Globals", input: `---
// subdescription: "Uses the `--lua-globals` option to export the values into the global scope.", country: Australia # this place
// scenarioType: "globals-encode", cities:
// input: `--- - Sydney
// - Melbourne
// country: Australia # this place - Brisbane
// cities: - Perth`,
// - Sydney expected: `country = "Australia"; -- this place
// - Melbourne cities = {
// - Brisbane "Sydney",
// - Perth`, "Melbourne",
// expected: `country = "Australia"; -- this place "Brisbane",
// "Perth",
// cities = { };
// "Sydney", `,
// "Melbourne", },
// "Brisbane", {
// "Perth", description: "Elaborate example",
// }; input: `---
// hello: world
// `, tables:
// like: this
// }, keys: values
// { ? look: non-string keys
// description: "Elaborate example", : True
// input: `--- numbers:
// - decimal: 12345
// hello: world - hex: 0x7fabc123
// tables: - octal: 0o30
// - float: 123.45
// like: this - infinity: .inf
// keys: values plus_infinity: +.inf
// ? look: non-string keys minus_infinity: -.inf
// : True - not: .nan
// `,
// numbers: expected: `return {
// - decimal: 12345 ["hello"] = "world";
// - hex: 0x7fabc123 ["tables"] = {
// - octal: 0o30 ["like"] = "this";
// - float: 123.45 ["keys"] = "values";
// - infinity: .inf [{
// - not: .nan ["look"] = "non-string keys";
// }] = true;
// `, };
// ["numbers"] = {
// expected: `return { {
// ["hello"] = "world"; ["decimal"] = 12345;
// ["tables"] = { },
// ["like"] = "this"; {
// ["keys"] = "values"; ["hex"] = 0x7fabc123;
// [{ },
// ["look"] = "non-string keys"; {
// }] = true; ["octal"] = 24;
// }; },
// ["numbers"] = { {
// { ["float"] = 123.45;
// ["decimal"] = 12345; },
// }, {
// { ["infinity"] = (1/0);
// ["hex"] = 0x7fabc123; ["plus_infinity"] = (1/0);
// }, ["minus_infinity"] = (-1/0);
// { },
// ["octal"] = 24; {
// }, ["not"] = (0/0);
// { },
// ["float"] = 123.45; };
// }, };
// { `,
// ["infinity"] = (1/0); scenarioType: "encode",
// }, },
// { {
// ["not"] = (0/0); 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",
// `, },
// {
// scenarioType: "encode", skipDoc: true,
// }, description: "Mapping",
// { input: "a: b\nc:\n d: e\nf: 0\n",
// skipDoc: true, expected: "return {\n\t[\"a\"] = \"b\";\n\t[\"c\"] = {\n\t\t[\"d\"] = \"e\";\n\t};\n\t[\"f\"] = 0;\n};\n",
// description: "Sequence", scenarioType: "encode",
// 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: "Scalar str",
// { input: "str: |\n foo\n bar\nanother: 'single'\nand: \"double\"",
// skipDoc: true, expected: "return {\n\t[\"str\"] = [[\nfoo\nbar\n]];\n\t[\"another\"] = 'single';\n\t[\"and\"] = \"double\";\n};\n",
// description: "Mapping", scenarioType: "encode",
// 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 null",
// { input: "x: null\n",
// skipDoc: true, expected: "return {\n\t[\"x\"] = nil;\n};\n",
// description: "Scalar str", scenarioType: "encode",
// 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 int",
// { input: "- 1\n- 2\n- 0x10\n- 0o30\n- -999\n",
// skipDoc: true, expected: "return {\n\t1,\n\t2,\n\t0x10,\n\t24,\n\t-999,\n};\n",
// description: "Scalar null", scenarioType: "encode",
// input: "x: null\n", },
// expected: "return {\n\t[\"x\"] = nil;\n};\n", {
// scenarioType: "encode", skipDoc: true,
// }, description: "Scalar float",
// { input: "- 1.0\n- 3.14\n- 1e100\n- .Inf\n- .NAN\n",
// skipDoc: true, expected: "return {\n\t1.0,\n\t3.14,\n\t1e100,\n\t(1/0),\n\t(0/0),\n};\n",
// description: "Scalar int", scenarioType: "encode",
// 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) { func testLuaScenario(t *testing.T, s formatScenario) {