-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
69 lines (60 loc) · 1.66 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"encoding/json"
"fmt"
jtdinfer "github.com/bombsimon/jtd-infer-go"
)
func main() {
inferSimpleValue()
inferMultipleStringRows()
inferWithHints()
}
func inferSimpleValue() {
schema := jtdinfer.
NewInferrer(jtdinfer.WithoutHints()).
Infer("my-string").
IntoSchema()
j, _ := json.MarshalIndent(schema, "", " ")
fmt.Println(string(j))
fmt.Println()
}
func inferMultipleStringRows() {
rows := []string{
`{"name":"Joe", "age": 52, "something_optional": true, "something_nullable": 1.1}`,
`{"name":"Jane", "age": 48, "something_nullable": null}`,
}
schema := jtdinfer.
InferStrings(rows, jtdinfer.WithoutHints()).
IntoSchema()
j, _ := json.MarshalIndent(schema, "", " ")
fmt.Println(string(j))
fmt.Println()
}
func inferWithHints() {
rows := []string{
`{
"name":"Joe",
"age":52,
"work":{"department": "sales"},
"values":{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, 9]},
"discriminator":[{"type":"s", "value":"foo"},{"type":"n", "value":3.14}]
}`,
`{
"name":"Jane",
"age":48,
"work":{"department": "engineering"},
"values":{"x": [1, 2, 3], "y": [4, 5, 6], "z": [7, 8, -2000]},
"discriminator":[{"type":"s", "value":"foo"},{"type":"n", "value":3.14}]
}`,
}
hints := jtdinfer.Hints{
DefaultNumType: jtdinfer.NumTypeUint32,
Enums: jtdinfer.NewHintSet().Add([]string{"work", "department"}),
Values: jtdinfer.NewHintSet().Add([]string{"values"}),
Discriminator: jtdinfer.NewHintSet().Add([]string{"discriminator", "-", "type"}),
}
schema := jtdinfer.InferStrings(rows, hints).IntoSchema()
j, _ := json.MarshalIndent(schema, "", " ")
fmt.Println(string(j))
fmt.Println()
}