-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
316 lines (258 loc) · 7.91 KB
/
map.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
package structure
import (
"errors"
"fmt"
"reflect"
"strings"
"sync"
)
type Mapper func(from reflect.Value, to reflect.Value, option *Option) error
type typeKey struct {
from reflect.Type
to reflect.Type
}
var typeMappers = make(map[typeKey]Mapper)
var typeMappersLock = &sync.RWMutex{}
type kindKey struct {
from reflect.Kind
to reflect.Kind
}
var kindMappers = make(map[kindKey]Mapper)
var kindMappersLock = &sync.RWMutex{}
func RegisterMapper[From any, To any](mapper Mapper) {
registerMapper(reflect.TypeOf((*From)(nil)).Elem(), reflect.TypeOf((*To)(nil)).Elem(), mapper)
}
func registerMapper(fromType reflect.Type, toType reflect.Type, mapper Mapper) {
key := typeKey{from: fromType, to: toType}
typeMappersLock.Lock()
defer typeMappersLock.Unlock()
if _, got := typeMappers[key]; got {
panic(fmt.Sprintf("type %+v already registed", key))
} else {
typeMappers[key] = mapper
}
}
func RegisterKindMapper(from reflect.Kind, to reflect.Kind, mapper Mapper) {
key := kindKey{from: from, to: to}
kindMappersLock.Lock()
defer kindMappersLock.Unlock()
if _, got := kindMappers[key]; got {
panic(fmt.Sprintf("type %+v already registed", key))
} else {
kindMappers[key] = mapper
}
}
func ReplaceMapper[From any, To any](mapper Mapper) {
fromType := reflect.TypeOf((*From)(nil)).Elem()
toType := reflect.TypeOf((*To)(nil)).Elem()
typeMappersLock.Lock()
defer typeMappersLock.Unlock()
typeMappers[typeKey{from: fromType, to: toType}] = mapper
}
func NewOption() *Option {
return &Option{
ZeroFields: true,
WeaklyTypedInput: true,
StringSplitSeparator: ",",
TagName: "mapping",
MatchName: strings.EqualFold,
}
}
var defaultOption = NewOption()
func Map(from, to any) error {
return MapWithOption(from, to, defaultOption)
}
func MapWithOption(from, to any, option *Option) error {
return MapToValueWithOption(from, reflect.Indirect(reflect.ValueOf(to)), option)
}
func MapToValue(from any, to reflect.Value) error {
return MapToValueWithOption(from, to, defaultOption)
}
func MapToValueWithOption(from any, to reflect.Value, option *Option) error {
if option == nil {
option = defaultOption
}
if to.Kind() != reflect.Map {
if !to.CanSet() {
return errors.New("to value can't be set")
}
}
var fromVal reflect.Value
if from != nil {
fromVal = reflect.ValueOf(from)
// We need to check here if input is a typed nil. Typed nils won't
// match the "input == nil" below so we check that here.
if fromVal.Kind() == reflect.Ptr && fromVal.IsNil() {
from = nil
}
}
if from == nil {
// If the data is nil, then we don't set anything, unless ZeroFields is set
// to true.
if option.ZeroFields {
to.Set(reflect.Zero(to.Type()))
}
return nil
}
return Value2ValueWithOption(fromVal, to, option)
}
func value2valuePtrWithOption(from reflect.Value, to reflect.Value, option *Option) error {
toElemType := to.Type()
if toElemType.Kind() == reflect.Ptr {
toElemType = toElemType.Elem()
}
if to.CanSet() {
realTo := to
if to.Type().Kind() == reflect.Ptr && realTo.IsNil() {
realTo = reflect.New(toElemType)
}
if err := Value2ValueWithOption(reflect.Indirect(from), reflect.Indirect(realTo), option); err != nil {
return err
}
to.Set(realTo)
} else {
if err := Value2ValueWithOption(reflect.Indirect(from), reflect.Indirect(to), option); err != nil {
return err
}
}
return nil
}
func value2valueSliceWithOption(from reflect.Value, to reflect.Value, option *Option) error {
dataVal := from
fromKind := dataVal.Kind()
valType := to.Type()
valElemType := valType.Elem()
sliceType := reflect.SliceOf(valElemType)
// If we have a non array/slice type then we first attempt to convert.
if fromKind != reflect.Array && fromKind != reflect.Slice {
if option.WeaklyTypedInput {
switch {
// Slice and array we use the normal logic
case fromKind == reflect.Slice, fromKind == reflect.Array:
break
// Empty maps turn into empty slices
case fromKind == reflect.Map:
//if dataVal.Len() == 0 {
// to.Set(reflect.MakeSlice(sliceType, 0, 0))
// return nil
//}
//// Create slice of maps of other sizes
//return d.decodeSlice(name, []interface{}{data}, val)
//case fromKind == reflect.String && valElemType.Kind() == reflect.Uint8:
//return d.decodeSlice(name, []byte(dataVal.String()), val)
case fromKind == reflect.String:
return Value2ValueWithOption(reflect.ValueOf(strings.Split(from.String(), option.StringSplitSeparator)), to, option)
// All other types we try to convert to the slice type
// and "lift" it into it. i.e. a string becomes a string slice.
default:
// Just re-try this function with data as a slice.
//return d.decodeSlice(name, []interface{}{data}, val)
}
}
return fmt.Errorf("source data must be an array or slice, got %s", fromKind)
}
// If the input value is nil, then don't allocate since empty != nil
if fromKind != reflect.Array && dataVal.IsNil() {
return nil
}
valSlice := to
if valSlice.IsNil() || option.ZeroFields {
// Make a new slice to hold our result, same size as the original data.
valSlice = reflect.MakeSlice(sliceType, dataVal.Len(), dataVal.Len())
} else if valSlice.Len() > dataVal.Len() {
valSlice = valSlice.Slice(0, dataVal.Len())
}
// Accumulate any errors
errors := make([]error, 0)
for i := 0; i < dataVal.Len(); i++ {
currentData := dataVal.Index(i)
for valSlice.Len() <= i {
valSlice = reflect.Append(valSlice, reflect.Zero(valElemType))
}
currentField := valSlice.Index(i)
if err := Value2ValueWithOption(currentData, currentField, option); err != nil {
errors = appendErrors(errors, err)
}
}
// Finally, set the value to the slice we built up
to.Set(valSlice)
// If there were errors, we return those
if len(errors) > 0 {
return &Error{errors}
}
return nil
}
func Value2ValueWithOption(from reflect.Value, to reflect.Value, option *Option) error {
if !from.IsValid() {
// If the input value is invalid, then we just set the value
// to be the zero value.
to.Set(reflect.Zero(to.Type()))
return nil
}
if from.CanInterface() {
if wrapper, ok := from.Interface().(ValueWrapper); ok {
return MapToValueWithOption(wrapper.Value(), to, option)
}
}
if to.Kind() == reflect.Ptr || from.Kind() == reflect.Ptr {
return value2valuePtrWithOption(from, to, option)
}
if to.Kind() == reflect.Slice {
return value2valueSliceWithOption(from, to, option)
}
fromType := from.Type()
toType := to.Type()
kindMappersLock.RLock()
mapper, ok := kindMappers[kindKey{from: fromType.Kind(), to: toType.Kind()}]
kindMappersLock.RUnlock()
if ok {
if err := mapper(from, to, option); err != nil {
return err
}
return nil
}
// if to kind is a interface, and from type can convert to , convert and return
if toType.Kind() == reflect.Interface && fromType.ConvertibleTo(toType) {
to.Set(from.Convert(toType))
return nil
}
// use type and to, get mapper
typeMappersLock.RLock()
mapper, ok = typeMappers[typeKey{from: fromType, to: toType}]
typeMappersLock.RUnlock()
if !ok {
// do type alias
typeAliasMapLock.RLock()
if fromTypeInMap, ok := typeAliasMap[fromType]; ok {
fromType = fromTypeInMap
}
if toTypeInMap, ok := typeAliasMap[toType]; ok {
toType = toTypeInMap
}
typeAliasMapLock.RUnlock()
typeMappersLock.RLock()
mapper, ok = typeMappers[typeKey{from: fromType, to: toType}]
typeMappersLock.RUnlock()
}
if ok {
if err := mapper(from, to, option); err != nil {
return err
}
return nil
}
// get all implements interface, no err, return direct
for k, v := range typeMappers {
if k.to.Kind() == reflect.Interface &&
(toType.Implements(k.to) || reflect.PtrTo(toType).Implements(k.to)) {
if err := v(from, to, option); err == nil {
return nil
}
}
}
// if the from and to type is same, set and return direct
if fromType == toType {
to.Set(from)
return nil
}
return errors.New(fmt.Sprintf("no mapper found for type %+v to %+v", fromType, toType))
}