-
Notifications
You must be signed in to change notification settings - Fork 34
/
mapper_test.go
523 lines (474 loc) · 12.5 KB
/
mapper_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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
package mapper
import (
"reflect"
"strconv"
"sync"
"testing"
"time"
)
type (
testStruct struct {
Name string
Sex bool
Age int
Time time.Time
Time2 JSONTime
Time3 JSONTime
}
FromStruct struct {
Name string `mapper:"UserName"`
Sex bool
AA string `mapper:"BB"`
}
ToStruct struct {
Name string `mapper:"UserName"`
Sex bool
BB string
}
TagStruct struct {
Name string `mapper:"UserName"`
Sex bool `json:"UserSex"`
}
)
var testValue reflect.Value
func init() {
testValue = reflect.ValueOf(&testStruct{})
}
func TestPackageVersion(t *testing.T) {
v := PackageVersion()
if v != packageVersion {
t.Error("PackageVersion error: not equal with packageVersion[" + packageVersion + "]")
} else {
t.Log("PackageVersion success")
}
}
func Test_CheckIsTypeWrapper(t *testing.T) {
v := TagStruct{}
if standardMapper.CheckIsTypeWrapper(reflect.ValueOf(v)) == true {
t.Error("CheckIsTypeWrapper error: set true but query is not true")
} else {
t.Log("CheckIsTypeWrapper success")
}
}
func Test_SetEnabledMapperStructField(t *testing.T) {
SetEnabledMapperStructField(true)
if standardMapper.IsEnabledMapperStructField() != true {
t.Error("SetEnabledMapperStructField error: set true but query is not true")
} else {
t.Log("SetEnabledMapperStructField success")
}
}
func Test_SetEnabledAutoTypeConvert(t *testing.T) {
SetEnabledAutoTypeConvert(true)
if standardMapper.IsEnabledAutoTypeConvert() != true {
t.Error("SetEnabledAutoTypeConvert error: set true but query is not true")
} else {
t.Log("SetEnabledAutoTypeConvert success")
}
}
func Test_SetEnabledTypeChecking(t *testing.T) {
SetEnabledTypeChecking(true)
if standardMapper.IsEnabledTypeChecking() != true {
t.Error("SetEnabledTypeChecking error: set true but query is not true")
} else {
t.Log("SetEnabledTypeChecking success")
}
}
func Test_GetTypeName(t *testing.T) {
name := GetTypeName(&testStruct{})
if name == "" {
t.Error("RunResult error: name is empty")
} else {
t.Log("RunResult success:", name)
}
}
func BenchmarkGetTypeName(b *testing.B) {
for i := 0; i < b.N; i++ {
GetTypeName(&testStruct{})
}
}
func Test_GetFieldNameFromMapperTag(t *testing.T) {
v := TagStruct{}
fieldName := GetFieldName(reflect.ValueOf(v), 0)
if fieldName == "UserName" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func Test_GetFieldNameFromJsonTag(t *testing.T) {
v := TagStruct{}
fieldName := GetFieldName(reflect.ValueOf(v), 1)
if fieldName == "UserSex" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func Test_SetEnableMapperTag(t *testing.T) {
v := TagStruct{}
SetEnabledMapperTag(false)
fieldName := GetFieldName(reflect.ValueOf(v), 0)
if fieldName == "Name" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
SetEnabledMapperTag(true)
fieldName = GetFieldName(reflect.ValueOf(v), 0)
if fieldName == "UserName" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func Test_SetEnableJsonTag(t *testing.T) {
v := TagStruct{}
SetEnabledJsonTag(false)
fieldName := GetFieldName(reflect.ValueOf(v), 1)
if fieldName == "Sex" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
SetEnabledJsonTag(true)
fieldName = GetFieldName(reflect.ValueOf(v), 1)
if fieldName == "UserSex" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func Test_GetFieldNameWithElem(t *testing.T) {
fieldName := GetFieldName(testValue.Elem(), 0)
if fieldName == "Name" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func BenchmarkGetFieldNameWithElem(b *testing.B) {
for i := 0; i < b.N; i++ {
GetFieldName(testValue.Elem(), 0)
}
}
func Test_CheckExistsField(t *testing.T) {
Register(&testStruct{})
fieldName := "Name"
_, isOk := CheckExistsField(testValue.Elem(), fieldName)
if isOk {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not exists", fieldName)
}
}
func BenchmarkCheckExistsField(b *testing.B) {
Register(&testStruct{})
elem := testValue.Elem()
fieldName := "Name"
for i := 0; i < b.N; i++ {
CheckExistsField(elem, fieldName)
}
}
func Test_Mapper(t *testing.T) {
SetEnabledTypeChecking(true)
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := &ToStruct{}
err := Mapper(from, to)
if err != nil {
t.Error("RunResult error: mapper error", err)
} else {
t.Log("RunResult success:", to)
}
}
func Test_MapperSlice(t *testing.T) {
SetEnabledTypeChecking(true)
var fromSlice []*FromStruct
var toSlice []*ToStruct
for i := 0; i < 10; i++ {
fromSlice = append(fromSlice, &FromStruct{Name: "From" + strconv.Itoa(i), Sex: true, AA: "AA" + strconv.Itoa(i)})
}
err := MapperSlice(fromSlice, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
for i := 0; i < len(fromSlice); i++ {
if !reflect.DeepEqual(fromSlice[i].Name, toSlice[i].Name) ||
!reflect.DeepEqual(fromSlice[i].Sex, toSlice[i].Sex) ||
!reflect.DeepEqual(fromSlice[i].AA, toSlice[i].BB) {
t.Fail()
}
}
}
}
func Test_MapperSlice2(t *testing.T) {
SetEnabledTypeChecking(true)
var fromSlice []*FromStruct
var toSlice []*ToStruct
for i := 0; i < 10; i++ {
fromSlice = append(fromSlice, &FromStruct{Name: "From" + strconv.Itoa(i), Sex: true, AA: "AA" + strconv.Itoa(i)})
}
err := MapperSlice(&fromSlice, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
for i := 0; i < len(fromSlice); i++ {
if !reflect.DeepEqual(fromSlice[i].Name, toSlice[i].Name) ||
!reflect.DeepEqual(fromSlice[i].Sex, toSlice[i].Sex) ||
!reflect.DeepEqual(fromSlice[i].AA, toSlice[i].BB) {
t.Fail()
}
}
}
}
func Test_MapperStructSlice(t *testing.T) {
SetEnabledTypeChecking(true)
var fromSlice []FromStruct
var toSlice []ToStruct
for i := 0; i < 10; i++ {
fromSlice = append(fromSlice, FromStruct{Name: "From" + strconv.Itoa(i), Sex: true, AA: "AA" + strconv.Itoa(i)})
}
err := MapperSlice(fromSlice, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
for i := 0; i < len(fromSlice); i++ {
if !reflect.DeepEqual(fromSlice[i].Name, toSlice[i].Name) ||
!reflect.DeepEqual(fromSlice[i].Sex, toSlice[i].Sex) ||
!reflect.DeepEqual(fromSlice[i].AA, toSlice[i].BB) {
t.Fail()
}
}
}
}
func Test_MapperStructSlice2(t *testing.T) {
SetEnabledTypeChecking(true)
var fromSlice []FromStruct
var toSlice []ToStruct
for i := 0; i < 10; i++ {
fromSlice = append(fromSlice, FromStruct{Name: "From" + strconv.Itoa(i), Sex: true, AA: "AA" + strconv.Itoa(i)})
}
err := MapperSlice(&fromSlice, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
for i := 0; i < len(fromSlice); i++ {
if !reflect.DeepEqual(fromSlice[i].Name, toSlice[i].Name) ||
!reflect.DeepEqual(fromSlice[i].Sex, toSlice[i].Sex) ||
!reflect.DeepEqual(fromSlice[i].AA, toSlice[i].BB) {
t.Fail()
}
}
}
}
func BenchmarkMapperSlice(b *testing.B) {
var fromSlice []*FromStruct
var toSlice []*ToStruct
for i := 0; i < 10; i++ {
fromSlice = append(fromSlice, &FromStruct{Name: "From" + strconv.Itoa(i), Sex: true, AA: "AA" + strconv.Itoa(i)})
}
for i := 0; i < b.N; i++ {
MapperSlice(fromSlice, &toSlice)
}
}
func Test_AutoMapper(t *testing.T) {
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := &ToStruct{}
err := AutoMapper(from, to)
if err != nil {
t.Error("RunResult error: mapper error", err)
} else {
t.Log("RunResult success:", to)
}
}
func Test_AutoMapper_StructToMap(t *testing.T) {
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := make(map[string]interface{})
err := AutoMapper(from, &to)
if err != nil {
t.Error("RunResult error: mapper error", err)
} else {
if to["UserName"] == "From" {
t.Log("RunResult success:", to)
} else {
t.Error("RunResult failed: map[UserName]", to["UserName"])
}
}
}
func Test_MapperMap(t *testing.T) {
validateTime, _ := time.Parse("2006-01-02 15:04:05", "2017-01-01 10:00:00")
fromMap := make(map[string]interface{})
fromMap["Name"] = "test"
fromMap["Sex"] = true
fromMap["Age"] = 10
fromMap["Time"] = validateTime
fromMap["Time2"] = validateTime
toObj := &testStruct{}
err := MapperMap(fromMap, toObj)
if err != nil && toObj.Time != validateTime {
t.Error("RunResult error: mapper error", err)
} else {
t.Log("RunResult success:", toObj)
}
}
func Test_MapToSlice(t *testing.T) {
var toSlice []*testStruct
/*fromMaps := make(map[string]interface{})
for i := 0; i < 10; i++ {
from := new(testStruct)
from.Name = "s" + strconv.Itoa(i)
from.Sex = true
from.Age = i
fromMaps[strconv.Itoa(i)] = from
}*/
fromMaps := make(map[string]interface{})
for i := 0; i < 10; i++ {
fromMap := make(map[string]interface{})
fromMap["Name"] = "s" + strconv.Itoa(i)
fromMap["Sex"] = true
fromMap["Age"] = i
fromMaps[strconv.Itoa(i)] = fromMap
}
err := MapToSlice(fromMaps, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
}
}
func Test_MapperMapSlice(t *testing.T) {
var toSlice []*testStruct
fromMaps := make(map[string]map[string]interface{})
for i := 0; i < 10; i++ {
fromMap := make(map[string]interface{})
fromMap["Name"] = "s" + strconv.Itoa(i)
fromMap["Sex"] = true
fromMap["Age"] = i
fromMaps[strconv.Itoa(i)] = fromMap
}
err := MapperMapSlice(fromMaps, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
}
}
func Test_MapperStructMapSlice(t *testing.T) {
var toSlice []testStruct
fromMaps := make(map[string]map[string]interface{})
for i := 0; i < 10; i++ {
fromMap := make(map[string]interface{})
fromMap["Name"] = "s" + strconv.Itoa(i)
fromMap["Sex"] = true
fromMap["Age"] = i
fromMaps[strconv.Itoa(i)] = fromMap
}
err := MapperMapSlice(fromMaps, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
}
}
func Test_IsTimeField(t *testing.T) {
t1 := time.Now()
if standardMapper.GetDefaultTimeWrapper().IsType(reflect.ValueOf(t1)) {
t.Log("check time.Now ok")
} else {
t.Error("check time.Now error")
}
var t2 JSONTime
t2 = JSONTime(time.Now())
if standardMapper.GetDefaultTimeWrapper().IsType(reflect.ValueOf(t2)) {
t.Log("check mapper.Time ok")
} else {
t.Error("check mapper.Time error")
}
}
func Test_MapToJson_JsonToMap(t *testing.T) {
fromMap := createMap()
data, err := MapToJson(fromMap)
if err != nil {
t.Error("MapToJson error", err)
} else {
var retMap map[string]interface{}
err := JsonToMap(data, &retMap)
if err != nil {
t.Error("MapToJson.JsonToMap error", err)
}
if len(retMap) != len(fromMap) {
t.Error("MapToJson failed, not match length")
}
t.Log("MapToJson success", fromMap, retMap)
}
}
func BenchmarkMapperMapSlice(b *testing.B) {
var s []*testStruct
fromMaps := make(map[string]map[string]interface{})
for i := 0; i < 10; i++ {
fromMap := make(map[string]interface{})
fromMap["Name"] = "s" + strconv.Itoa(i)
fromMap["Sex"] = true
fromMap["Age"] = i
fromMaps[strconv.Itoa(i)] = fromMap
}
for i := 0; i < b.N; i++ {
MapperMapSlice(fromMaps, s)
}
}
func BenchmarkMapper(b *testing.B) {
Register(&FromStruct{})
Register(&ToStruct{})
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := &ToStruct{}
for i := 0; i < b.N; i++ {
Mapper(from, to)
}
}
func BenchmarkAutoMapper(b *testing.B) {
Register(&FromStruct{})
Register(&ToStruct{})
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := &ToStruct{}
for i := 0; i < b.N; i++ {
Mapper(from, to)
}
}
func BenchmarkAutoMapper_Map(b *testing.B) {
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := make(map[string]interface{})
for i := 0; i < b.N; i++ {
Mapper(from, &to)
}
}
func BenchmarkMapperMap(b *testing.B) {
Register(&testStruct{})
fromMap := make(map[string]interface{})
fromMap["Name"] = "test"
fromMap["Sex"] = true
fromMap["Age"] = 10
fromMap["time"] = time.Now()
toObj := &testStruct{}
for i := 0; i < b.N; i++ {
MapperMap(fromMap, toObj)
}
}
func BenchmarkSyncMap(b *testing.B) {
var sMap sync.Map
for i := 0; i < b.N; i++ {
sMap.Load("1")
}
}
func createMap() map[string]interface{} {
validateTime, _ := time.Parse("2006-01-02 15:04:05", "2017-01-01 10:00:00")
fromMap := make(map[string]interface{})
fromMap["Name"] = "test"
fromMap["Sex"] = true
fromMap["Age"] = 10
fromMap["Time"] = validateTime
return fromMap
}