Skip to content

Commit 05315b0

Browse files
committed
Add CLI tool to infer JTD
1 parent 4d764f5 commit 05315b0

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

.golangci.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ linters:
4040
- errname
4141
- errorlint
4242
- exhaustive
43-
- forbidigo
4443
- forcetypeassert
4544
- gochecknoglobals
4645
- gochecknoinits

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,83 @@ schema := InferStrings(rows, WithoutHints()).IntoSchema()
7676
// }
7777
```
7878

79+
## CLI tool
80+
81+
There's not really a need for yet another CLI tool since the original
82+
[jtd-infer] project contains one, but since it was just a couple of lines to
83+
achieve this there's a CLI tool bundled here as well.
84+
85+
### Installation
86+
87+
```sh
88+
go install github.com/bombsimon/jtd-infer-go/cmd/jtd-infer-go
89+
```
90+
91+
### CLI usage
92+
93+
The CLI tool reads from STDIN so something like this is the intended usage.
94+
95+
```sh
96+
echo '{"foo": "bar", "biz": 1}' | jtd-infer-go
97+
{
98+
"properties": {
99+
"biz": {
100+
"type": "uint8"
101+
},
102+
"foo": {
103+
"type": "string"
104+
}
105+
}
106+
}
107+
```
108+
109+
```sh
110+
› cat example.json
111+
{"name":"Joe", "age": 52, "something_optional": true, "something_nullable": 1.1}
112+
{"name":"Jane", "age": 48, "something_nullable": null}
113+
114+
› cat example.json | jtd-infer-go
115+
{
116+
"properties": {
117+
"age": {
118+
"type": "uint8"
119+
},
120+
"name": {
121+
"type": "string"
122+
},
123+
"something_nullable": {
124+
"nullable": true,
125+
"type": "float64"
126+
}
127+
},
128+
"optionalProperties": {
129+
"something_optional": {
130+
"type": "boolean"
131+
}
132+
}
133+
}
134+
135+
› jtd-infer-go < cmd/jtd-infer-go/example.json
136+
{
137+
"properties": {
138+
"age": {
139+
"type": "uint8"
140+
},
141+
"name": {
142+
"type": "string"
143+
},
144+
"something_nullable": {
145+
"nullable": true,
146+
"type": "float64"
147+
}
148+
},
149+
"optionalProperties": {
150+
"something_optional": {
151+
"type": "boolean"
152+
}
153+
}
154+
}
155+
```
156+
79157
[jtd-infer]: https://github.com/jsontypedef/json-typedef-infer/
80158
[examples]: examples

cmd/jtd-infer-go/example.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"name":"Joe", "age": 52, "something_optional": true, "something_nullable": 1.1}
2+
{"name":"Jane", "age": 48, "something_nullable": null}

cmd/jtd-infer-go/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
9+
jtdinfer "github.com/bombsimon/jtd-infer-go"
10+
)
11+
12+
func main() {
13+
inferrer := jtdinfer.NewInferrer(jtdinfer.WithoutHints())
14+
scanner := bufio.NewScanner(os.Stdin)
15+
16+
for scanner.Scan() && scanner.Text() != "" {
17+
var toInfer any
18+
if err := json.Unmarshal(scanner.Bytes(), &toInfer); err != nil {
19+
fmt.Printf("failed to infer string: %s\n", err)
20+
os.Exit(1)
21+
}
22+
23+
inferrer = inferrer.Infer(toInfer)
24+
}
25+
26+
schema := inferrer.IntoSchema()
27+
j, _ := json.MarshalIndent(schema, "", " ") //nolint:errchkjson
28+
fmt.Println(string(j))
29+
}

0 commit comments

Comments
 (0)