forked from devfeel/mapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.go
515 lines (467 loc) · 13.4 KB
/
mapper.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
package mapper
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"time"
)
var (
ZeroValue reflect.Value
DefaultTimeWrapper *TimeWrapper
fieldNameMap sync.Map
registerMap sync.Map
enabledTypeChecking bool
enabledMapperStructField bool
enabledAutoTypeConvert bool
timeType = reflect.TypeOf(time.Now())
jsonTimeType = reflect.TypeOf(JSONTime(time.Now()))
typeWrappers []TypeWrapper
)
const (
packageVersion = "0.6"
mapperTagKey = "mapper"
jsonTagKey = "json"
IgnoreTagValue = "-"
nameConnector = "_"
formatTime = "15:04:05"
formatDate = "2006-01-02"
formatDateTime = "2006-01-02 15:04:05"
)
func init() {
ZeroValue = reflect.Value{}
DefaultTimeWrapper = NewTimeWrapper()
typeWrappers = []TypeWrapper{}
UseWrapper(DefaultTimeWrapper)
enabledTypeChecking = false
enabledMapperStructField = true
enabledAutoTypeConvert = true
}
func PackageVersion() string {
return packageVersion
}
// UseWrapper register a type wrapper
func UseWrapper(w TypeWrapper) {
if len(typeWrappers) > 0 {
typeWrappers[len(typeWrappers)-1].SetNext(w)
}
typeWrappers = append(typeWrappers, w)
}
// CheckIsTypeWrapper check value is in type wrappers
func CheckIsTypeWrapper(value reflect.Value) bool {
for _, w := range typeWrappers {
if w.IsType(value) {
return true
}
}
return false
}
// SetEnabledTypeChecking set enabled flag for TypeChecking
// if set true, the field type will be checked for consistency during mapping
// default is false
func SetEnabledTypeChecking(isEnabled bool) {
enabledTypeChecking = isEnabled
}
// SetEnabledAutoTypeConvert set enabled flag for auto type convert
// if set true, field will auto convert in Time and Unix
// default is true
func SetEnabledAutoTypeConvert(isEnabled bool) {
enabledAutoTypeConvert = isEnabled
}
// SetEnabledMapperStructField set enabled flag for MapperStructField
// if set true, the reflect.Struct field will auto mapper
// must follow premises:
// 1. fromField and toField type must be reflect.Struct and not time.Time
// 2. fromField and toField must be not same type
// default is enabled
func SetEnabledMapperStructField(isEnabled bool) {
enabledMapperStructField = isEnabled
}
// Register register struct to init Map
func Register(obj interface{}) error {
objValue := reflect.ValueOf(obj)
if objValue == ZeroValue {
return errors.New("no exists this value")
}
return registerValue(objValue.Elem())
}
// registerValue register Value to init Map
func registerValue(objValue reflect.Value) error {
regValue := objValue
if objValue == ZeroValue {
return errors.New("no exists this value")
}
if regValue.Type().Kind() == reflect.Ptr {
regValue = regValue.Elem()
}
typeName := regValue.Type().String()
for i := 0; i < regValue.NumField(); i++ {
mapFieldName := typeName + nameConnector + GetFieldName(regValue, i)
realFieldName := regValue.Type().Field(i).Name
fieldNameMap.Store(mapFieldName, realFieldName)
}
//store register flag
registerMap.Store(typeName, nil)
return nil
}
// GetTypeName get type name
func GetTypeName(obj interface{}) string {
object := reflect.ValueOf(obj)
return object.String()
}
// CheckExistsField check field is exists by name
func CheckExistsField(elem reflect.Value, fieldName string) (realFieldName string, exists bool) {
typeName := elem.Type().String()
fileKey := typeName + nameConnector + fieldName
realName, isOk := fieldNameMap.Load(fileKey)
if !isOk {
return "", isOk
} else {
return realName.(string), isOk
}
}
// GetFieldName get fieldName with ElemValue and index
// if config tag string, return tag value
func GetFieldName(objElem reflect.Value, index int) string {
fieldName := ""
field := objElem.Type().Field(index)
tag := getStructTag(field)
if tag != "" {
fieldName = tag
} else {
fieldName = field.Name
}
return fieldName
}
// MapperMap mapper and set value from map to object
// support auto register struct
// now support field type:
// 1.reflect.Bool
// 2.reflect.String
// 3.reflect.Int8\16\32\64
// 4.reflect.Uint8\16\32\64
// 5.reflect.Float32\64
// 6.time.Time
func MapperMap(fromMap map[string]interface{}, toObj interface{}) error {
toElem := reflect.ValueOf(toObj).Elem()
if toElem == ZeroValue {
return errors.New("to obj is not legal value")
}
//check register flag
//if not register, register it
if !checkIsRegister(toElem) {
Register(toObj)
}
for k, v := range fromMap {
fieldName := k
//check field is exists
realFieldName, exists := CheckExistsField(toElem, fieldName)
if !exists {
continue
}
fieldInfo, exists := toElem.Type().FieldByName(realFieldName)
if !exists {
continue
}
fieldKind := fieldInfo.Type.Kind()
fieldValue := toElem.FieldByName(realFieldName)
setFieldValue(fieldValue, fieldKind, v)
}
return nil
}
// MapperMapSlice mapper from map[string]map[string]interface{} to a slice of any type's ptr
// toSlice must be a slice of any type's ptr.
func MapperMapSlice(fromMaps map[string]map[string]interface{}, toSlice interface{}) error {
var err error
toValue := reflect.ValueOf(toSlice)
if toValue.Kind() != reflect.Ptr {
return errors.New("toSlice must pointer of slice")
}
if toValue.IsNil() {
return errors.New("toSlice must not nil pointer")
}
toElemType := reflect.TypeOf(toSlice).Elem().Elem()
if toElemType.Kind() != reflect.Ptr {
return errors.New("slice elem must ptr ")
}
direct := reflect.Indirect(toValue)
//3 elem parse: 1.[]*type 2.*type 3.type
toElemType = toElemType.Elem()
for _, v := range fromMaps {
elem := reflect.New(toElemType)
err = MapperMap(v, elem.Interface())
if err == nil {
direct.Set(reflect.Append(direct, elem))
}
}
return err
}
// MapToJson mapper from map[string]interface{} to json []byte
func MapToJson(fromMap map[string]interface{}) ([]byte, error) {
json, err := json.Marshal(fromMap)
if err != nil {
return nil, err
}
return json, nil
}
// JsonToMap mapper from json []byte to map[string]interface{}
func JsonToMap(body []byte, toMap *map[string]interface{}) error {
err := json.Unmarshal(body, toMap)
return err
}
// Mapper mapper and set value from struct fromObj to toObj
// not support auto register struct
func Mapper(fromObj, toObj interface{}) error {
fromElem := reflect.ValueOf(fromObj).Elem()
toElem := reflect.ValueOf(toObj).Elem()
if fromElem == ZeroValue {
return errors.New("from obj is not legal value")
}
if toElem == ZeroValue {
return errors.New("to obj is not legal value")
}
return elemMapper(fromElem, toElem)
}
// MapperSlice mapper from slice of struct to a slice of any type
// fromSlice and toSlice must be a slice of any type.
func MapperSlice(fromSlice, toSlice interface{}) error {
var err error
toValue := reflect.ValueOf(toSlice)
if toValue.Kind() != reflect.Ptr {
return errors.New("toSlice must pointer of slice")
}
if toValue.IsNil() {
return errors.New("toSlice must not nil pointer")
}
elemType := reflect.TypeOf(toSlice).Elem().Elem()
if elemType.Kind() != reflect.Ptr {
return errors.New("slice elem must ptr ")
}
direct := reflect.Indirect(toValue)
//3 elem parse: 1.[]*type 2.*type 3.type
elemType = elemType.Elem()
fromElems := convertToSlice(fromSlice)
for _, v := range fromElems {
elem := reflect.New(elemType)
err = elemMapper(reflect.ValueOf(v).Elem(), elem.Elem())
if err == nil {
direct.Set(reflect.Append(direct, elem))
}
}
return err
}
// Mapper mapper and set value from struct fromObj to toObj
// support auto register struct
func AutoMapper(fromObj, toObj interface{}) error {
return Mapper(fromObj, toObj)
}
func elemMapper(fromElem, toElem reflect.Value) error {
//check register flag
//if not register, register it
if !checkIsRegister(fromElem) {
registerValue(fromElem)
}
if !checkIsRegister(toElem) {
registerValue(toElem)
}
for i := 0; i < fromElem.NumField(); i++ {
fromFieldInfo := fromElem.Field(i)
fieldName := GetFieldName(fromElem, i)
//check field is exists
realFieldName, exists := CheckExistsField(toElem, fieldName)
if !exists {
continue
}
toFieldInfo := toElem.FieldByName(realFieldName)
//check field is same type
if enabledTypeChecking {
if fromFieldInfo.Kind() != toFieldInfo.Kind() {
continue
}
}
if enabledMapperStructField &&
toFieldInfo.Kind() == reflect.Struct && fromFieldInfo.Kind() == reflect.Struct &&
toFieldInfo.Type() != fromFieldInfo.Type() &&
!CheckIsTypeWrapper(toFieldInfo) && !CheckIsTypeWrapper(fromFieldInfo) {
x := reflect.New(toFieldInfo.Type()).Elem()
err := elemMapper(fromFieldInfo, x)
if err != nil {
fmt.Println("auto mapper field", fromFieldInfo, "=>", toFieldInfo, "error", err.Error())
} else {
toFieldInfo.Set(x)
}
} else {
isSet := false
if enabledAutoTypeConvert {
if DefaultTimeWrapper.IsType(fromFieldInfo) && toFieldInfo.Kind() == reflect.Int64 {
fromTime := fromFieldInfo.Interface().(time.Time)
toFieldInfo.Set(reflect.ValueOf(TimeToUnix(fromTime)))
isSet = true
} else if DefaultTimeWrapper.IsType(toFieldInfo) && fromFieldInfo.Kind() == reflect.Int64 {
fromTime := fromFieldInfo.Interface().(int64)
toFieldInfo.Set(reflect.ValueOf(UnixToTime(fromTime)))
isSet = true
}
}
if !isSet {
toFieldInfo.Set(fromFieldInfo)
}
}
}
return nil
}
func setFieldValue(fieldValue reflect.Value, fieldKind reflect.Kind, value interface{}) error {
switch fieldKind {
case reflect.Bool:
if value == nil {
fieldValue.SetBool(false)
} else if v, ok := value.(bool); ok {
fieldValue.SetBool(v)
} else {
v, _ := Convert(ToString(value)).Bool()
fieldValue.SetBool(v)
}
case reflect.String:
if value == nil {
fieldValue.SetString("")
} else {
fieldValue.SetString(ToString(value))
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if value == nil {
fieldValue.SetInt(0)
} else {
val := reflect.ValueOf(value)
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetInt(val.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fieldValue.SetInt(int64(val.Uint()))
default:
v, _ := Convert(ToString(value)).Int64()
fieldValue.SetInt(v)
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if value == nil {
fieldValue.SetUint(0)
} else {
val := reflect.ValueOf(value)
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
fieldValue.SetUint(uint64(val.Int()))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
fieldValue.SetUint(val.Uint())
default:
v, _ := Convert(ToString(value)).Uint64()
fieldValue.SetUint(v)
}
}
case reflect.Float64, reflect.Float32:
if value == nil {
fieldValue.SetFloat(0)
} else {
val := reflect.ValueOf(value)
switch val.Kind() {
case reflect.Float64:
fieldValue.SetFloat(val.Float())
default:
v, _ := Convert(ToString(value)).Float64()
fieldValue.SetFloat(v)
}
}
case reflect.Struct:
if value == nil {
fieldValue.Set(reflect.Zero(fieldValue.Type()))
} else if DefaultTimeWrapper.IsType(fieldValue) {
var timeString string
if fieldValue.Type() == timeType {
timeString = ""
fieldValue.Set(reflect.ValueOf(value))
}
if fieldValue.Type() == jsonTimeType {
timeString = ""
fieldValue.Set(reflect.ValueOf(JSONTime(value.(time.Time))))
}
switch d := value.(type) {
case []byte:
timeString = string(d)
case string:
timeString = d
case int64:
if enabledAutoTypeConvert {
//try to transform Unix time to local Time
t, err := UnixToTimeLocation(value.(int64), time.UTC.String())
if err != nil {
return err
}
fieldValue.Set(reflect.ValueOf(t))
}
}
if timeString != "" {
if len(timeString) >= 19 {
//满足yyyy-MM-dd HH:mm:ss格式
timeString = timeString[:19]
t, err := time.ParseInLocation(formatDateTime, timeString, time.UTC)
if err == nil {
t = t.In(time.UTC)
fieldValue.Set(reflect.ValueOf(t))
}
} else if len(timeString) >= 10 {
//满足yyyy-MM-dd格式
timeString = timeString[:10]
t, err := time.ParseInLocation(formatDate, timeString, time.UTC)
if err == nil {
fieldValue.Set(reflect.ValueOf(t))
}
}
}
}
default:
if reflect.ValueOf(value).Type() == fieldValue.Type() {
fieldValue.Set(reflect.ValueOf(value))
}
}
return nil
}
func getStructTag(field reflect.StructField) string {
tagValue := ""
//1.check mapperTagKey
tagValue = field.Tag.Get(mapperTagKey)
if checkTagValidity(tagValue) {
return tagValue
}
//2.check jsonTagKey
tagValue = field.Tag.Get(jsonTagKey)
if checkTagValidity(tagValue) {
// support more tag property, as json tag omitempty 2018-07-13
return strings.Split(tagValue, ",")[0]
}
return ""
}
func checkTagValidity(tagValue string) bool {
if tagValue != "" && tagValue != IgnoreTagValue {
return true
}
return false
}
func checkIsRegister(objElem reflect.Value) bool {
typeName := objElem.Type().String()
_, isOk := registerMap.Load(typeName)
return isOk
}
//convert slice interface{} to []interface{}
func convertToSlice(arr interface{}) []interface{} {
v := reflect.ValueOf(arr)
if v.Kind() != reflect.Slice {
panic("toslice arr not slice")
}
l := v.Len()
ret := make([]interface{}, l)
for i := 0; i < l; i++ {
ret[i] = v.Index(i).Interface()
}
return ret
}