-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonstruct_test.go
216 lines (192 loc) · 5.89 KB
/
jsonstruct_test.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package jsonstruct_test
import (
"github.com/berlincount/jsonstruct"
"reflect"
"strings"
"testing"
"github.com/davecgh/go-spew/spew"
)
type testStruct struct {
myTestInt int
myTestField string
}
func TestMapType(t *testing.T) {
jsonstruct.MapType("testStruct", reflect.TypeOf(testStruct{}))
if len(jsonstruct.TypeMap)%4 != 0 {
t.Errorf("jsonstruct.TypeMap expected to have four elements for each type")
}
if rtype, present := jsonstruct.TypeMap["testStruct"]; present {
if rtype != reflect.TypeOf(testStruct{}) {
t.Errorf("wrong basic type was registered")
}
} else {
t.Errorf("basic type was not registered")
}
if rtype, present := jsonstruct.TypeMap["*testStruct"]; present {
if rtype != reflect.TypeOf(new(testStruct)) {
t.Errorf("wrong * type was registered")
}
} else {
t.Errorf("* type was not registered")
}
if rtype, present := jsonstruct.TypeMap["[]testStruct"]; present {
if rtype != reflect.TypeOf([]testStruct{}) {
t.Errorf("wrong [] type was registered")
}
} else {
t.Errorf("[] type was not registered")
}
if rtype, present := jsonstruct.TypeMap["[]*testStruct"]; present {
if rtype != reflect.TypeOf([]*testStruct{}) {
t.Errorf("wrong []* type was registered")
}
} else {
t.Errorf("[]* type was not registered")
}
}
func TestDecode1(t *testing.T) {
testStructJSON := `
{"struct": "test",
"fields": [
{"name": "TestInt", "type": "int", "tags": "testTag:\"first_field\""},
{"name": "TestString", "type": "string", "tags": "testTag:\"second_field\""}
]}
`
decodedStructs, err := jsonstruct.Decode(strings.NewReader(testStructJSON))
if err != nil {
panic("something went terribly wrong decoding the structures")
}
if len(decodedStructs) != 1 {
panic("something went rather unexpected decoding the structures")
}
if decodedStructs["test"].NumField() != 2 {
t.Errorf("wrong number of fields was decoded")
}
firstField := decodedStructs["test"].Field(0)
if firstField.Name != "TestInt" {
t.Errorf("first field name was unpacked wrongly")
}
if firstField.Type != reflect.TypeOf(0) {
t.Errorf("first field type was unpacked wrongly")
}
tag, ok := firstField.Tag.Lookup("testTag")
if !ok {
t.Errorf("first field tag was unpacked wrongly")
}
if tag != "first_field" {
t.Errorf("first field tag was unpacked badly")
}
if firstField.Anonymous {
t.Errorf("first field is anonymous")
}
secondField := decodedStructs["test"].Field(1)
if secondField.Name != "TestString" {
t.Errorf("second field name was unpacked wrongly")
}
if secondField.Type != reflect.TypeOf("") {
t.Errorf("second field type was unpacked wrongly")
}
tag, ok = secondField.Tag.Lookup("testTag")
if !ok {
t.Errorf("second field tag was unpacked wrongly")
}
if tag != "second_field" {
t.Errorf("second field tag was unpacked badly")
}
if secondField.Anonymous {
t.Errorf("second field is anonymous")
}
if rtype, present := jsonstruct.TypeMap["test"]; present {
if rtype != decodedStructs["test"] {
t.Errorf("wrong basic type was registered")
}
} else {
t.Errorf("basic type was not registered")
}
}
func TestDecode2(t *testing.T) {
testStructJSON := `
{"struct": "test1",
"fields": [
{"name": "TestField1", "type": "int", "tags": ""}
]}
{"struct": "test2",
"fields": [
{"name": "TestField2", "type": "int", "tags": ""}
]}
{"struct": "test3",
"fields": [
{"name": "TestField3", "type": "int", "tags": ""}
]}
`
decodedStructs, err := jsonstruct.Decode(strings.NewReader(testStructJSON))
if err != nil {
panic("something went terribly wrong decoding the structures")
}
if len(decodedStructs) != 3 {
panic("something went rather unexpected decoding the structures")
}
if decodedStructs["test1"].NumField() != 1 || decodedStructs["test2"].NumField() != 1 || decodedStructs["test3"].NumField() != 1 {
t.Errorf("wrong number of fields was decoded")
}
if rtype, present := jsonstruct.TypeMap["test1"]; present {
if rtype != decodedStructs["test1"] {
t.Errorf("wrong basic type1 was registered")
}
} else {
t.Errorf("basic type1 was not registered")
}
if rtype, present := jsonstruct.TypeMap["test2"]; present {
if rtype != decodedStructs["test2"] {
t.Errorf("wrong basic type2 was registered")
}
} else {
t.Errorf("basic type2 was not registered")
}
if rtype, present := jsonstruct.TypeMap["test3"]; present {
if rtype != decodedStructs["test3"] {
t.Errorf("wrong basic type3 was registered")
}
} else {
t.Errorf("basic type3 was not registered")
}
}
func TestDecode3(t *testing.T) {
testStructJSON := `
`
decodedStructs, err := jsonstruct.Decode(strings.NewReader(testStructJSON))
if err != nil {
panic("something went terribly wrong decoding the structures")
}
if len(decodedStructs) != 0 {
panic("something went rather unexpected decoding the structures")
}
}
func TestDecode4(t *testing.T) {
testStructJSON := `
{"struct": "testP",
"fields": [
{"name": "private", "type": "int", "tags": ""}
]}
`
_, err := jsonstruct.Decode(strings.NewReader(testStructJSON))
if err == nil {
panic("not getting an error when trying to create struct with private field")
}
}
func ExampleDecode() {
testStructJSON := `
{"struct": "test",
"fields": [
{"name": "TestInt", "type": "int", "tags": "testTag:\"first_field\""},
{"name": "TestString", "type": "string", "tags": "testTag:\"second_field\""}
]}
`
decodedStructs, _ := jsonstruct.Decode(strings.NewReader(testStructJSON))
spewWithoutAddresses := spew.ConfigState{DisablePointerAddresses: true}
spewWithoutAddresses.Dump(decodedStructs)
// Output:
// (map[string]reflect.Type) (len=1) {
// (string) (len=4) "test": (*reflect.rtype)(struct { TestInt int "testTag:\"first_field\""; TestString string "testTag:\"second_field\"" })
// }
}