-
Notifications
You must be signed in to change notification settings - Fork 34
/
mapper.go
209 lines (171 loc) · 6.58 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
package mapper
import (
"reflect"
)
var (
standardMapper IMapper
)
type IMapper interface {
Mapper(fromObj, toObj interface{}) error
AutoMapper(fromObj, toObj interface{}) error
MapperMap(fromMap map[string]interface{}, toObj interface{}) error
MapToSlice(fromMap map[string]interface{}, toSlice interface{}) error
MapperMapSlice(fromMaps map[string]map[string]interface{}, toSlice interface{}) error
MapperSlice(fromSlice, toSlice interface{}) error
MapToJson(fromMap map[string]interface{}) ([]byte, error)
JsonToMap(body []byte, toMap *map[string]interface{}) error
Register(obj interface{}) error
UseWrapper(w TypeWrapper)
GetTypeName(obj interface{}) string
GetFieldName(objElem reflect.Value, index int) string
GetCustomTagName() string
GetDefaultTimeWrapper() *TimeWrapper
CheckExistsField(elem reflect.Value, fieldName string) (realFieldName string, exists bool)
CheckIsTypeWrapper(value reflect.Value) bool
SetEnabledTypeChecking(isEnabled bool)
IsEnabledTypeChecking() bool
SetEnabledMapperTag(isEnabled bool)
IsEnabledMapperTag() bool
SetEnabledJsonTag(isEnabled bool)
IsEnabledJsonTag() bool
SetEnabledCustomTag(isEnabled bool)
IsEnabledCustomTag() bool
SetCustomTagName(tagName string)
SetEnabledAutoTypeConvert(isEnabled bool)
IsEnabledAutoTypeConvert() bool
SetEnabledMapperStructField(isEnabled bool)
IsEnabledMapperStructField() bool
SetEnableFieldIgnoreTag(isEnabled bool)
IsEnableFieldIgnoreTag() bool
}
func init() {
standardMapper = NewMapper()
}
func PackageVersion() string {
return packageVersion
}
// UseWrapper register a type wrapper
func UseWrapper(w TypeWrapper) {
standardMapper.UseWrapper(w)
}
// CheckIsTypeWrapper check value is in type wrappers
func CheckIsTypeWrapper(value reflect.Value) bool {
return standardMapper.CheckIsTypeWrapper(value)
}
// 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) {
standardMapper.SetEnabledTypeChecking(isEnabled)
}
// SetEnabledMapperTag set enabled flag for 'Mapper' tag check
// if set true, 'Mapper' tag will be check during mapping's GetFieldName
// default is true
func SetEnabledMapperTag(isEnabled bool) {
standardMapper.SetEnabledMapperTag(isEnabled)
}
// SetEnabledJsonTag set enabled flag for 'Json' tag check
// if set true, 'Json' tag will be check during mapping's GetFieldName
// default is true
func SetEnabledJsonTag(isEnabled bool) {
standardMapper.SetEnabledJsonTag(isEnabled)
}
// SetEnabledCustomTag set enabled flag for set custom tag name
// if set true and set customTagName, the custom tag will be check during mapping's GetFieldName
// default is false
func SetEnabledCustomTag(isEnabled bool) {
standardMapper.SetEnabledCustomTag(isEnabled)
}
func IsEnabledCustomTag() bool {
return standardMapper.IsEnabledCustomTag()
}
// SetCustomTagName
func SetCustomTagName(tagName string) {
standardMapper.SetCustomTagName(tagName)
}
// 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) {
standardMapper.SetEnabledAutoTypeConvert(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) {
standardMapper.SetEnabledMapperStructField(isEnabled)
}
// SetEnableFieldIgnoreTag set the enabled flag for the ignored tag
// in the version < 0.7.8, we use field name as the key when mapping structs if field tag is "-"
// from 0.7.8, we add switch enableFieldIgnoreTag which is false in default
// if caller enable this flag, the field will be ignored in the mapping process
func SetEnableFieldIgnoreTag(isEnabled bool) {
standardMapper.SetEnableFieldIgnoreTag(isEnabled)
}
// Register register struct to init Map
func Register(obj interface{}) error {
return standardMapper.Register(obj)
}
// 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) {
return standardMapper.CheckExistsField(elem, fieldName)
}
// GetFieldName get fieldName with ElemValue and index
// if config tag string, return tag value
func GetFieldName(objElem reflect.Value, index int) string {
return standardMapper.GetFieldName(objElem, index)
}
// 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 {
return standardMapper.MapperMap(fromMap, toObj)
}
// MapToSlice mapper from map[string]interface{} to a slice of any type's ptr
// toSlice must be a slice of any type.
func MapToSlice(fromMap map[string]interface{}, toSlice interface{}) error {
return standardMapper.MapToSlice(fromMap, toSlice)
}
// MapperMapSlice mapper from map[string]map[string]interface{} to a slice of any type's ptr
// toSlice must be a slice of any type.
// Deprecated: will remove on v1.0, please use MapToSlice instead
func MapperMapSlice(fromMaps map[string]map[string]interface{}, toSlice interface{}) error {
return standardMapper.MapperMapSlice(fromMaps, toSlice)
}
// 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 {
return standardMapper.MapperSlice(fromSlice, toSlice)
}
// MapToJson mapper from map[string]interface{} to json []byte
func MapToJson(fromMap map[string]interface{}) ([]byte, error) {
return standardMapper.MapToJson(fromMap)
}
// JsonToMap mapper from json []byte to map[string]interface{}
func JsonToMap(body []byte, toMap *map[string]interface{}) error {
return standardMapper.JsonToMap(body, toMap)
}
// Mapper mapper and set value from struct fromObj to toObj
// not support auto register struct
func Mapper(fromObj, toObj interface{}) error {
return standardMapper.Mapper(fromObj, toObj)
}
// AutoMapper mapper and set value from struct fromObj to toObj
// support auto register struct
func AutoMapper(fromObj, toObj interface{}) error {
return standardMapper.AutoMapper(fromObj, toObj)
}