-
Notifications
You must be signed in to change notification settings - Fork 34
/
mapper_object_test.go
471 lines (432 loc) · 11.5 KB
/
mapper_object_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
package mapper
import (
"fmt"
"reflect"
"strconv"
"sync"
"testing"
"time"
)
func TestMapperObject_NewMapper(t *testing.T) {
m := NewMapper(CTypeChecking(true), CCustomTagName("^"))
fmt.Println(m)
}
func Test_Object_SetEnabledTypeChecking(t *testing.T) {
m := NewMapper()
m.SetEnabledTypeChecking(true)
if m.IsEnabledTypeChecking() != true {
t.Error("SetEnabledTypeChecking error: set true but query is not true")
} else {
t.Log("SetEnabledTypeChecking success")
}
}
func Test_ObjectGetTypeName(t *testing.T) {
m := NewMapper()
name := m.GetTypeName(&testStruct{})
if name == "" {
t.Error("RunResult error: name is empty")
} else {
t.Log("RunResult success:", name)
}
}
func BenchmarkObjectGetTypeName(b *testing.B) {
m := NewMapper()
for i := 0; i < b.N; i++ {
m.GetTypeName(&testStruct{})
}
}
func Test_Object_GetFieldNameFromMapperTag(t *testing.T) {
m := NewMapper()
v := TagStruct{}
fieldName := m.GetFieldName(reflect.ValueOf(v), 0)
if fieldName == "UserName" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func Test_Object_GetFieldNameFromJsonTag(t *testing.T) {
m := NewMapper()
v := TagStruct{}
fieldName := m.GetFieldName(reflect.ValueOf(v), 1)
if fieldName == "UserSex" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func Test_Object_SetEnableMapperTag(t *testing.T) {
m := NewMapper()
v := TagStruct{}
m.SetEnabledMapperTag(false)
fieldName := m.GetFieldName(reflect.ValueOf(v), 0)
if fieldName == "Name" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
m.SetEnabledMapperTag(true)
fieldName = m.GetFieldName(reflect.ValueOf(v), 0)
if fieldName == "UserName" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func Test_Object_SetEnableJsonTag(t *testing.T) {
m := NewMapper()
v := TagStruct{}
m.SetEnabledJsonTag(false)
fieldName := m.GetFieldName(reflect.ValueOf(v), 1)
if fieldName == "Sex" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
m.SetEnabledJsonTag(true)
fieldName = m.GetFieldName(reflect.ValueOf(v), 1)
if fieldName == "UserSex" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func Test_Object_GetFieldNameWithElem(t *testing.T) {
m := NewMapper()
fieldName := m.GetFieldName(testValue.Elem(), 0)
if fieldName == "Name" {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not match", fieldName)
}
}
func BenchmarkObjectGetFieldNameWithElem(b *testing.B) {
m := NewMapper()
for i := 0; i < b.N; i++ {
m.GetFieldName(testValue.Elem(), 0)
}
}
func Test_Object_CheckExistsField(t *testing.T) {
m := NewMapper()
m.Register(&testStruct{})
fieldName := "Name"
_, isOk := m.CheckExistsField(testValue.Elem(), fieldName)
if isOk {
t.Log("RunResult success:", fieldName)
} else {
t.Error("RunResult error: fieldName not exists", fieldName)
}
}
func BenchmarkObjectCheckExistsField(b *testing.B) {
m := NewMapper()
m.Register(&testStruct{})
elem := testValue.Elem()
fieldName := "Name"
for i := 0; i < b.N; i++ {
m.CheckExistsField(elem, fieldName)
}
}
func Test_Object_Mapper(t *testing.T) {
m := NewMapper()
m.SetEnabledTypeChecking(true)
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := &ToStruct{}
err := m.Mapper(from, to)
if err != nil {
t.Error("RunResult error: mapper error", err)
} else {
t.Log("RunResult success:", to)
}
}
func Test_Object_MapperSlice(t *testing.T) {
m := NewMapper()
m.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 := m.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_Object_MapperSlice2(t *testing.T) {
m := NewMapper()
m.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 := m.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_Object_MapperStructSlice(t *testing.T) {
m := NewMapper()
m.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 := m.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_Object_MapperStructSlice2(t *testing.T) {
m := NewMapper()
m.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 := m.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 BenchmarkObjectMapperSlice(b *testing.B) {
m := NewMapper()
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++ {
m.MapperSlice(fromSlice, &toSlice)
}
}
func Test_Object_AutoMapper(t *testing.T) {
m := NewMapper()
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := &ToStruct{}
err := m.AutoMapper(from, to)
if err != nil {
t.Error("RunResult error: mapper error", err)
} else {
t.Log("RunResult success:", to)
}
}
func Test_Object_AutoMapper_StructToMap(t *testing.T) {
m := NewMapper()
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := make(map[string]interface{})
err := m.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_Object_MapperMap(t *testing.T) {
m := NewMapper()
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 := m.MapperMap(fromMap, toObj)
if err != nil && toObj.Time != validateTime {
t.Error("RunResult error: mapper error", err)
} else {
t.Log("RunResult success:", toObj)
}
}
func Test_Object_MapToSlice(t *testing.T) {
m := NewMapper()
var toSlice []*testStruct
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 := m.MapToSlice(fromMaps, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
}
}
func Test_Object_MapperMapSlice(t *testing.T) {
m := NewMapper()
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 := m.MapperMapSlice(fromMaps, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
}
}
func Test_Object_MapperStructMapSlice(t *testing.T) {
m := NewMapper()
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 := m.MapperMapSlice(fromMaps, &toSlice)
if err != nil {
t.Error(err)
} else {
t.Log(toSlice, len(toSlice))
}
}
func Test_Object_IsTimeField(t *testing.T) {
m := NewMapper()
t1 := time.Now()
if m.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 m.GetDefaultTimeWrapper().IsType(reflect.ValueOf(t2)) {
t.Log("check mapper.Time ok")
} else {
t.Error("check mapper.Time error")
}
}
func Test_Object_MapToJson_JsonToMap(t *testing.T) {
m := NewMapper()
fromMap := createMap()
data, err := m.MapToJson(fromMap)
if err != nil {
t.Error("MapToJson error", err)
} else {
var retMap map[string]interface{}
err = m.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 BenchmarkObjectMapperMapSlice(b *testing.B) {
m := NewMapper()
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++ {
m.MapperMapSlice(fromMaps, s)
}
}
func BenchmarkObjectMapper(b *testing.B) {
m := NewMapper()
m.Register(&FromStruct{})
m.Register(&ToStruct{})
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := &ToStruct{}
for i := 0; i < b.N; i++ {
m.Mapper(from, to)
}
}
func BenchmarkObjectAutoMapper(b *testing.B) {
m := NewMapper()
m.Register(&FromStruct{})
m.Register(&ToStruct{})
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := &ToStruct{}
for i := 0; i < b.N; i++ {
m.Mapper(from, to)
}
}
func BenchmarkObjectAutoMapper_Map(b *testing.B) {
m := NewMapper()
from := &FromStruct{Name: "From", Sex: true, AA: "AA"}
to := make(map[string]interface{})
for i := 0; i < b.N; i++ {
m.Mapper(from, &to)
}
}
func BenchmarkObjectMapperMap(b *testing.B) {
m := NewMapper()
m.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++ {
m.MapperMap(fromMap, toObj)
}
}
func BenchmarkObjectSyncMap(b *testing.B) {
var sMap sync.Map
for i := 0; i < b.N; i++ {
sMap.Load("1")
}
}