-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
407 lines (371 loc) · 7.87 KB
/
example_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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package reflectutils_test
import (
"encoding/json"
"fmt"
. "github.com/sunfmin/reflectutils"
)
// By given these structs
func ExampleSet_0init() {
type Person struct {
Name string
Score float64
Gender int
Company *Company
Departments []*Department
Projects []Project
Phones map[string]string
Languages map[string]Language
}
type Language struct {
Code string
Name string
}
type Company struct {
Name string
Phone *Phone
Phone2 **Phone
}
type Department struct {
Id int
Name string
}
type Project struct {
Id string
Name string
Members []*Person
}
type Phone struct {
Number string
}
}
// For How to set simple field
func ExampleSet_1setfield() {
var p *Person
Set(&p, "Name", "Felix")
Set(&p, "Score", 66.88)
Set(&p, "Gender", 1)
printJsonV(p)
fmt.Println(MustGet(&p, "Score"))
// Output:
// {
// "Name": "Felix",
// "Score": 66.88,
// "Gender": 1,
// "Company": null,
// "Departments": null,
// "Projects": null,
// "Phones": null,
// "Languages": null
// }
// 66.88
}
// For how to set a struct property
func ExampleSet_2setstructproperty() {
p := &Person{}
Set(p, ".Company.Name", "The Plant")
Set(p, ".Company.Phone.Number", "911")
printJsonV(p)
fmt.Println(MustGet(&p, ".Company.Phone.Number"))
// Output:
// {
// "Name": "",
// "Score": 0,
// "Gender": 0,
// "Company": {
// "Name": "The Plant",
// "Phone": {
// "Number": "911"
// }
// },
// "Departments": null,
// "Projects": null,
// "Phones": null,
// "Languages": null
// }
// 911
}
// For how to set slice and it's property
func ExampleSet_3setsliceproperty() {
var p *Person
Set(&p, "Departments[0].Id", 1)
Set(&p, "Departments[0].Name", "High Tech")
Set(&p, "Projects[0].Name", "UIBuilder")
// if you jump the index for an array, It will put nil in between
// So there will be no index out of range error.
Set(&p, "Departments[3].Id", 1)
Set(&p, "Departments[3].Name", "High Tech")
printJsonV(p)
fmt.Println(MustGet(&p, "Departments[3].Name"))
fmt.Println(MustGet(&p, "Departments[4].Name"))
fmt.Println(MustGet(&p, "Projects[0].Name"))
// Output:
// {
// "Name": "",
// "Score": 0,
// "Gender": 0,
// "Company": null,
// "Departments": [
// {
// "Id": 1,
// "Name": "High Tech"
// },
// null,
// null,
// {
// "Id": 1,
// "Name": "High Tech"
// }
// ],
// "Projects": [
// {
// "Id": "",
// "Name": "UIBuilder",
// "Members": null
// }
// ],
// "Phones": null,
// "Languages": null
// }
// High Tech
// <nil>
// UIBuilder
}
// For how to set map property
func ExampleSet_4setmapproperty() {
var p *Person
Set(&p, "Languages.en_US.Name", "United States")
Set(&p, "Languages.en_US.Code", "en_US")
Set(&p, "Languages.zh_CN.Name", "China")
Set(&p, "Languages.zh_CN.Code", "zh_CN")
printJsonV(p)
fmt.Println(MustGet(&p, "Languages.zh_CN.Code"))
// Output:
// {
// "Name": "",
// "Score": 0,
// "Gender": 0,
// "Company": null,
// "Departments": null,
// "Projects": null,
// "Phones": null,
// "Languages": {
// "en_US": {
// "Code": "en_US",
// "Name": "United States"
// },
// "zh_CN": {
// "Code": "zh_CN",
// "Name": "China"
// }
// }
// }
// zh_CN
}
// You can do whatever deeper you like
func ExampleSet_5setdeeper() {
var p *Person
Set(&p, "Projects[0].Members[0].Company.Phone.Number", "911")
printJsonV(p)
fmt.Println(MustGet(&p, "Projects[0].Members[0].Company.Phone.Number"))
// Output:
// {
// "Name": "",
// "Score": 0,
// "Gender": 0,
// "Company": null,
// "Departments": null,
// "Projects": [
// {
// "Id": "",
// "Name": "",
// "Members": [
// {
// "Name": "",
// "Score": 0,
// "Gender": 0,
// "Company": {
// "Name": "",
// "Phone": {
// "Number": "911"
// }
// },
// "Departments": null,
// "Projects": null,
// "Phones": null,
// "Languages": null
// }
// ]
// }
// ],
// "Phones": null,
// "Languages": null
// }
// 911
}
// A new way to append to an array
func ExampleSet_6appendtoarray() {
var p *Person
for i := 1; i < 6; i++ {
var d *Department
Set(&d, "Id", i)
Set(&d, "Name", fmt.Sprintf("Department of Energy %d", i))
Set(&p, "Departments[]", d)
}
printJsonV(p)
// Output:
// {
// "Name": "",
// "Score": 0,
// "Gender": 0,
// "Company": null,
// "Departments": [
// {
// "Id": 1,
// "Name": "Department of Energy 1"
// },
// {
// "Id": 2,
// "Name": "Department of Energy 2"
// },
// {
// "Id": 3,
// "Name": "Department of Energy 3"
// },
// {
// "Id": 4,
// "Name": "Department of Energy 4"
// },
// {
// "Id": 5,
// "Name": "Department of Energy 5"
// }
// ],
// "Projects": null,
// "Phones": null,
// "Languages": null
// }
}
// You could also set []byte data to string property, and vice versa.
// And set string value to int, float
func ExampleSet_6byteandstring() {
type Obj struct {
ByteProperty []byte
StringProperty string
IntValue int
FloatValue float64
IntValueForBytes int
}
var o *Obj
Set(&o, "ByteProperty", "hello")
Set(&o, "StringProperty", []byte{0x46, 0x65, 0x6c, 0x69, 0x78})
Set(&o, "IntValue", "22")
Set(&o, "IntValueForBytes", []byte{0x32, 0x32})
Set(&o, "FloatValue", "22.88")
fmt.Println(string(o.ByteProperty))
fmt.Println(o.StringProperty)
fmt.Println(o.IntValue)
fmt.Println(o.FloatValue)
fmt.Println(o.IntValueForBytes)
// Output:
// hello
// Felix
// 22
// 22.88
// 22
}
// If you set a property that don't exists, it gives you an error.
func ExampleSet_7notexists() {
var p *Person
err := Set(&p, "Whatever.Not.Exists", "911")
fmt.Println(err)
// Output:
// no such field
}
// Get Type of a deep nested object
func ExampleSet_8gettype() {
type Variant struct {
Name string
}
type Product struct {
Variants []*Variant
ByCode map[string]*Variant
}
type Obj struct {
MainProduct *Product
}
var o *Obj
fmt.Println(GetType(o, "MainProduct.Variants[0].Name"))
fmt.Println(GetType(o, "MainProduct.Variants[0]"))
fmt.Println(GetType(o, "MainProduct.Variants"))
fmt.Println(GetType(o, "MainProduct"))
fmt.Println(GetType(o, "MainProduct.ByCode.abc"))
fmt.Println(GetType(o, "MainProduct.ByCode"))
fmt.Println(GetType(o, "x123.ByCode"))
fmt.Println(GetType(o, "MainProduct.ByCode.abc.NotExist"))
// Output:
// string
// *reflectutils_test.Variant
// []*reflectutils_test.Variant
// *reflectutils_test.Product
// *reflectutils_test.Variant
// map[string]*reflectutils_test.Variant
// <nil>
// <nil>
}
// ForEach slice with any
func ExampleForEach_9_0_normal() {
var arr = []*Language{
{"en_US", "United States"},
{"zh_CN", "China"},
}
ForEach(arr, func(v any) {
ve := v.(*Language)
fmt.Printf("%s\n", ve.Name)
})
// Output:
// United States
// China
}
// ForEach slice with correct type
func ExampleForEach_9_1_with_type() {
var arr = []*Language{
{"en_US", "United States"},
{"zh_CN", "China"},
}
ForEach(arr, func(v *Language) {
fmt.Printf("%s\n", v.Name)
})
// Output:
// United States
// China
}
// ForEach map with any
func ExampleForEach_9_2_map_with_any() {
var m = map[string]*Language{
"en_US": {"en_US", "United States"},
"zh_CN": {"zh_CN", "China"},
}
ForEach(m, func(c any, v any) {
fmt.Printf("%s: %s\n", c.(string), v.(*Language).Name)
})
// Output:
// en_US: United States
// zh_CN: China
}
// ForEach map with any
func ExampleForEach_9_3_map_with_correct_type() {
var m = map[string]*Language{
"en_US": {"en_US", "United States"},
"zh_CN": {"zh_CN", "China"},
}
ForEach(m, func(c string, v *Language) {
fmt.Printf("%s: %s\n", c, v.Name)
})
// Output:
// en_US: United States
// zh_CN: China
}
func printJsonV(v interface{}) {
j, _ := json.MarshalIndent(v, "", "\t")
fmt.Printf("\n\n%s\n", j)
}