Repository navigation
Expand file tree
/
Copy pathjson2struct_test.go
More file actions
151 lines (138 loc) · 4.46 KB
/
Copy pathjson2struct_test.go
File metadata and controls
151 lines (138 loc) · 4.46 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package json2struct_test
import (
"encoding/json"
"strings"
"sync"
"testing"
json2struct "github.com/EmptyZeroRain/json2struct"
"github.com/EmptyZeroRain/json2struct/inference"
"github.com/EmptyZeroRain/json2struct/schema"
)
func TestGenerateMergedStruct(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "User", Merge: true, Omitempty: true})
if err := g.AddJSON([]byte(`{"id":1,"user_name":"a","profile":{"ip_address":"127.0.0.1"}}`)); err != nil {
t.Fatal(err)
}
if err := g.AddJSON([]byte(`{"id":2,"email":"a@b.test","profile":{"ip_address":"127.0.0.2"}}`)); err != nil {
t.Fatal(err)
}
code, err := g.Generate()
if err != nil {
t.Fatal(err)
}
out := string(code)
for _, want := range []string{"type User struct", "ID string `json:\"id\"`", "Email string `json:\"email,omitempty\"`", "IPAddress string"} {
if !strings.Contains(out, want) {
t.Errorf("generated code missing %q:\n%s", want, out)
}
}
}
func TestNDJSON(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "Event", Merge: true})
if err := g.AddNDJSON(strings.NewReader("{\"a\":1}\n{\"b\":true}\n")); err != nil {
t.Fatal(err)
}
if g.Schema().Children["a"].Required {
t.Error("a should be optional after merge")
}
}
func TestGenerateFromSchema(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "Thing"})
if _, err := g.GenerateFromSchema(nil); err == nil {
t.Error("nil schema should fail")
}
}
func TestInvalidAndEmptyInputs(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "Thing"})
if err := g.AddJSON([]byte(`{"a":1}{"b":2}`)); err == nil {
t.Error("expected trailing JSON error")
}
if _, err := g.Generate(); err == nil {
t.Error("expected no schema error")
}
}
func TestUnusualJSONKeysProduceValidIdentifiers(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "Weird"})
if err := g.AddJSON([]byte(`{"123 name":1,"a.b":2,"type":3,"a-b":4,"a_b":5}`)); err != nil {
t.Fatal(err)
}
code, err := g.Generate()
if err != nil {
t.Fatal(err)
}
out := string(code)
for _, want := range []string{"Field123Name", "AB", "Type string", "AB2", "AB3", "json:\"123 name\"", "json:\"a.b\""} {
if !strings.Contains(out, want) {
t.Errorf("missing %q in generated code:\n%s", want, out)
}
}
}
func TestTimeAndUnknownValuesDefaultToString(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "Record"})
if err := g.AddJSON([]byte(`{"created_at":"2025-01-01T00:00:00Z"}`)); err != nil {
t.Fatal(err)
}
code, err := g.Generate()
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(code), "CreatedAt string") {
t.Fatalf("time value should remain string:\n%s", code)
}
if got := inference.Infer(struct{ Value int }{Value: 1}).Type; got != schema.TypeString {
t.Fatalf("unknown value type = %q, want string", got)
}
if got := inference.Infer(json.Number("123")).Type; got != schema.TypeString {
t.Fatalf("number type = %q, want string", got)
}
}
func TestConcurrentUse(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "Concurrent", Merge: true})
var wg sync.WaitGroup
for i := 0; i < 32; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if err := g.AddJSON([]byte(`{"value":"x"}`)); err != nil {
t.Error(err)
}
if _, err := g.Generate(); err != nil {
t.Error(err)
}
_ = g.Schema()
}(i)
}
wg.Wait()
}
func TestInferBatch(t *testing.T) {
s, err := json2struct.InferBatch([][]byte{[]byte(`{"id":1}`), []byte(`{"name":"x"}`)}, json2struct.BatchOptions{Workers: 2})
if err != nil {
t.Fatal(err)
}
if s.Children["id"].Required {
t.Error("id should be optional")
}
}
func TestAddBatchWithOptions(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "Batch", Merge: true})
if err := g.AddBatchWithOptions([][]byte{[]byte(`{"a":1}`), []byte(`{"b":2}`)}, json2struct.BatchOptions{Workers: 2}); err != nil {
t.Fatal(err)
}
if g.Schema().Children["a"].Required {
t.Error("a should be optional")
}
}
func TestSchemaCycleIsRejected(t *testing.T) {
f := &json2struct.Field{Name: "root", Type: schema.TypeObject, Children: map[string]*json2struct.Field{}}
f.Children["self"] = f
g := json2struct.New(json2struct.Options{Name: "Cycle"})
if _, err := g.GenerateFromSchema(f); err == nil {
t.Fatal("expected cycle error")
}
}
func TestAddFileUnderRejectsTraversal(t *testing.T) {
g := json2struct.New(json2struct.Options{Name: "Safe"})
if err := g.AddFileUnder("/tmp", "../etc/passwd"); err == nil {
t.Fatal("expected path traversal rejection")
}
}