Skip to content

Commit

Permalink
Format JSON with jfmt
Browse files Browse the repository at this point in the history
More compact and nicer output, IMHO.
  • Loading branch information
arp242 committed May 24, 2024
1 parent eff8d3a commit 5194334
Show file tree
Hide file tree
Showing 187 changed files with 1,411 additions and 3,632 deletions.
9 changes: 3 additions & 6 deletions cmd/toml-test/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/BurntSushi/toml"
tomltest "github.com/toml-lang/toml-test"
"zgo.at/jfmt"
"zgo.at/zli"
)

Expand Down Expand Up @@ -273,13 +274,9 @@ func detailed(r tomltest.Runner, t tomltest.Test) string {
}
showStream(b, "input sent to parser-cmd", t.Input)

var j map[string]any
err := json.Unmarshal([]byte(t.Output), &j)
out, err := jfmt.NewFormatter(0, " ").FormatString(t.Output)
if err == nil {
out, err := json.MarshalIndent(j, "", " ")
if err == nil {
t.Output = string(out)
}
t.Output = out
}

if t.OutputFromStderr {
Expand Down
7 changes: 4 additions & 3 deletions gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ def write_valid_case(decoder, header, index, block):
block = re.sub(r'(:\d\d)\.9999+', r'\1.999', block)

path = VALID_ROOT / f"{header}-{index}.toml"
path_json = VALID_ROOT / f"{header}-{index}.json"

path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(block.strip() + '\n')

subprocess.run([decoder],
stdin=open(path),
stdout=open(VALID_ROOT / f"{header}-{index}.json", mode='w'))
subprocess.run([decoder], stdin=open(path), stdout=open(path_json, mode='w'))
subprocess.run(['jfmt', '-w', path_json])

invalid_index = 0
lines = block.splitlines()
Expand Down
11 changes: 9 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ go 1.18

require (
github.com/BurntSushi/toml v1.4.0
// no_term branch, which doesn't depend on x/term and x/sys
zgo.at/zli v0.0.0-20231011155724-865ee344d4b3
zgo.at/jfmt v0.0.0-20240428154335-af97c9b4f565
zgo.at/zli v0.0.0-20240524153330-59815e8f337c
)

require (
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
zgo.at/termtext v1.4.0 // indirect
zgo.at/zstd v0.0.0-20240425000522-78bcf900e0a4 // indirect
)
15 changes: 13 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
zgo.at/zli v0.0.0-20231011155724-865ee344d4b3 h1:c6mOqJByLZGs0hmCK0Pr+XuDU6XoNcccUROX/8nPP6g=
zgo.at/zli v0.0.0-20231011155724-865ee344d4b3/go.mod h1:HLAc12TjNGT+VRXr76JnsNE3pbooQtwKWhX+RlDjQ2Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
zgo.at/jfmt v0.0.0-20240428154335-af97c9b4f565 h1:R1hOXEVUmtBounxBlnSQ81ayL0PyUH2TmW5DcU5VtIQ=
zgo.at/jfmt v0.0.0-20240428154335-af97c9b4f565/go.mod h1:Mw4wYfEKH2WXV/MOCWU8dLQHLomFfqGfuyomcXzm0pQ=
zgo.at/termtext v1.4.0 h1:r7fzyT5jsdRw9Z0uCQh+A+TWhWk+pC5HDJd1o8EH8NI=
zgo.at/termtext v1.4.0/go.mod h1:CBx+HpG2ijB3W0Bd4CK8UtzcsXw+EmvvlhQUyQpZO+E=
zgo.at/zli v0.0.0-20240524153330-59815e8f337c h1:++s1u+nwbnFFe9iiiQj5WFz7Sgv7q9o05ZWhrN9yWbM=
zgo.at/zli v0.0.0-20240524153330-59815e8f337c/go.mod h1:0jjx+AGEkWOOQ0NtzbMnpko+H2G+aTg8mfCKqoc/BuA=
zgo.at/zstd v0.0.0-20240425000522-78bcf900e0a4 h1:/13W9C05Uwi49SX7aKN7O8uI5zZcbt2CXq+qHDD73Wo=
zgo.at/zstd v0.0.0-20240425000522-78bcf900e0a4/go.mod h1:o/Q8+EtSahHnfkbB3t8wXE0FnoDTmJ0sBDlzezv9XeM=
28 changes: 11 additions & 17 deletions tests/valid/array/array-subtables.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
{
"arr": [
{
"subtab": {
"val": {
"type": "integer",
"value": "1"
"arr": [
{
"subtab": {
"val": {"type": "integer", "value": "1"}
}
},
{
"subtab": {
"val": {"type": "integer", "value": "2"}
}
}
}
},
{
"subtab": {
"val": {
"type": "integer",
"value": "2"
}
}
}
]
]
}
90 changes: 24 additions & 66 deletions tests/valid/array/array.json
Original file line number Diff line number Diff line change
@@ -1,68 +1,26 @@
{
"comments": [
{
"type": "integer",
"value": "1"
},
{
"type": "integer",
"value": "2"
}
],
"dates": [
{
"type": "datetime",
"value": "1987-07-05T17:45:00Z"
},
{
"type": "datetime",
"value": "1979-05-27T07:32:00Z"
},
{
"type": "datetime",
"value": "2006-06-01T11:00:00Z"
}
],
"floats": [
{
"type": "float",
"value": "1.1"
},
{
"type": "float",
"value": "2.1"
},
{
"type": "float",
"value": "3.1"
}
],
"ints": [
{
"type": "integer",
"value": "1"
},
{
"type": "integer",
"value": "2"
},
{
"type": "integer",
"value": "3"
}
],
"strings": [
{
"type": "string",
"value": "a"
},
{
"type": "string",
"value": "b"
},
{
"type": "string",
"value": "c"
}
]
"comments": [
{"type": "integer", "value": "1"},
{"type": "integer", "value": "2"}
],
"dates": [
{"type": "datetime", "value": "1987-07-05T17:45:00Z"},
{"type": "datetime", "value": "1979-05-27T07:32:00Z"},
{"type": "datetime", "value": "2006-06-01T11:00:00Z"}
],
"floats": [
{"type": "float", "value": "1.1"},
{"type": "float", "value": "2.1"},
{"type": "float", "value": "3.1"}
],
"ints": [
{"type": "integer", "value": "1"},
{"type": "integer", "value": "2"},
{"type": "integer", "value": "3"}
],
"strings": [
{"type": "string", "value": "a"},
{"type": "string", "value": "b"},
{"type": "string", "value": "c"}
]
}
14 changes: 4 additions & 10 deletions tests/valid/array/bool.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"a": [
{
"type": "bool",
"value": "true"
},
{
"type": "bool",
"value": "false"
}
]
"a": [
{"type": "bool", "value": "true"},
{"type": "bool", "value": "false"}
]
}
10 changes: 1 addition & 9 deletions tests/valid/array/empty.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
{
"thevoid": [
[
[
[
[]
]
]
]
]
"thevoid": [[[[[]]]]]
}
44 changes: 13 additions & 31 deletions tests/valid/array/hetergeneous.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
{
"mixed": [
[
{
"type": "integer",
"value": "1"
},
{
"type": "integer",
"value": "2"
}
],
[
{
"type": "string",
"value": "a"
},
{
"type": "string",
"value": "b"
}
],
[
{
"type": "float",
"value": "1.1"
},
{
"type": "float",
"value": "2.1"
}
"mixed": [
[
{"type": "integer", "value": "1"},
{"type": "integer", "value": "2"}
],
[
{"type": "string", "value": "a"},
{"type": "string", "value": "b"}
],
[
{"type": "float", "value": "1.1"},
{"type": "float", "value": "2.1"}
]
]
]
}
14 changes: 3 additions & 11 deletions tests/valid/array/mixed-int-array.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
{
"arrays-and-ints": [
{
"type": "integer",
"value": "1"
},
[
{
"type": "string",
"value": "Arrays are not integers."
}
"arrays-and-ints": [
{"type": "integer", "value": "1"},
[{"type": "string", "value": "Arrays are not integers."}]
]
]
}
14 changes: 4 additions & 10 deletions tests/valid/array/mixed-int-float.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"ints-and-floats": [
{
"type": "integer",
"value": "1"
},
{
"type": "float",
"value": "1.1"
}
]
"ints-and-floats": [
{"type": "integer", "value": "1"},
{"type": "float", "value": "1.1"}
]
}
14 changes: 4 additions & 10 deletions tests/valid/array/mixed-int-string.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
{
"strings-and-ints": [
{
"type": "string",
"value": "hi"
},
{
"type": "integer",
"value": "42"
}
]
"strings-and-ints": [
{"type": "string", "value": "hi"},
{"type": "integer", "value": "42"}
]
}
51 changes: 15 additions & 36 deletions tests/valid/array/mixed-string-table.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,17 @@
{
"contributors": [
{
"type": "string",
"value": "Foo Bar \u003c[email protected]\u003e"
},
{
"email": {
"type": "string",
"value": "[email protected]"
},
"name": {
"type": "string",
"value": "Baz Qux"
},
"url": {
"type": "string",
"value": "https://example.com/bazqux"
}
}
],
"mixed": [
{
"k": {
"type": "string",
"value": "a"
}
},
{
"type": "string",
"value": "b"
},
{
"type": "integer",
"value": "1"
}
]
"contributors": [
{"type": "string", "value": "Foo Bar \u003c[email protected]\u003e"},
{
"email": {"type": "string", "value": "[email protected]"},
"name": {"type": "string", "value": "Baz Qux"},
"url": {"type": "string", "value": "https://example.com/bazqux"}
}
],
"mixed": [
{
"k": {"type": "string", "value": "a"}
},
{"type": "string", "value": "b"},
{"type": "integer", "value": "1"}
]
}
Loading

0 comments on commit 5194334

Please sign in to comment.