-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconverter.go
More file actions
365 lines (312 loc) · 8.54 KB
/
converter.go
File metadata and controls
365 lines (312 loc) · 8.54 KB
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
package converter
import (
"fmt"
"reflect"
"strconv"
)
type conversion struct {
errors []error
chain *funcChain
}
func (c *conversion) err(err error) {
c.errors = append(c.errors, err)
}
func (c *conversion) errf(format string, args ...any) {
c.err(fmt.Errorf(format, args...))
}
// Convert takes two objects, e.g. v2_1.Document and &v2_2.Document{} and attempts to map all the properties from one
// to the other. After the automatic mapping, if an explicit conversion function is provided, this will be called to
// perform any additional conversion logic necessary.
func (c *conversion) convert(fromValue reflect.Value, toValuePtr reflect.Value) {
toTypePtr := toValuePtr.Type()
if !isPtr(toTypePtr) {
c.errf("TO value provided was not a pointer, unable to set value: %+v", toValuePtr)
return
}
toValue := c.getValue(fromValue, toTypePtr)
// don't set nil values
if toValue == nilValue {
return
}
// toValuePtr is the passed-in pointer, toValue is also the same type of pointer
toValuePtr.Elem().Set(toValue.Elem())
}
func (c *conversion) getValue(fromValue reflect.Value, targetType reflect.Type) reflect.Value {
var err error
fromType := fromValue.Type()
var toValue reflect.Value
// handle incoming pointer Types
if isPtr(fromType) {
if fromValue.IsNil() {
return nilValue
}
fromValue = fromValue.Elem()
if !fromValue.IsValid() || fromValue.IsZero() {
return nilValue
}
fromType = fromValue.Type()
}
baseTargetType := targetType
if isPtr(targetType) {
baseTargetType = targetType.Elem()
}
switch {
case isInterface(baseTargetType):
satisfyingType := c.findConvertableType(fromType, baseTargetType)
if satisfyingType != nil {
return c.getValue(fromValue, satisfyingType)
}
case isStruct(fromType) && isStruct(baseTargetType):
// this always creates a pointer type
toValue = reflect.New(baseTargetType)
toValue = toValue.Elem()
for i := 0; i < fromType.NumField(); i++ {
fromField := fromType.Field(i)
fromFieldValue := fromValue.Field(i)
toField, exists := baseTargetType.FieldByName(fromField.Name)
if !exists {
continue
}
toFieldType := toField.Type
toFieldValue := toValue.FieldByName(toField.Name)
newValue := c.getValue(fromFieldValue, toFieldType)
if newValue == nilValue {
continue
}
toFieldValue.Set(newValue)
}
// check for custom convert functions from previous/next version struct
value, done := c.callConversionFunc(fromValue, fromType, baseTargetType, err, toValue)
if done {
return value
}
case isSlice(fromType) && isSlice(baseTargetType):
if fromValue.IsNil() {
return nilValue
}
length := fromValue.Len()
targetElementType := baseTargetType.Elem()
toValue = reflect.MakeSlice(baseTargetType, length, length)
for i := 0; i < length; i++ {
v := c.getValue(fromValue.Index(i), targetElementType)
if v.IsValid() {
toValue.Index(i).Set(v)
}
}
case isMap(fromType) && isMap(baseTargetType):
if fromValue.IsNil() {
return nilValue
}
keyType := baseTargetType.Key()
elementType := baseTargetType.Elem()
toValue = reflect.MakeMap(baseTargetType)
for _, fromKey := range fromValue.MapKeys() {
fromVal := fromValue.MapIndex(fromKey)
k := c.getValue(fromKey, keyType)
v := c.getValue(fromVal, elementType)
if k == nilValue || v == nilValue {
continue
}
if v == nilValue {
continue
}
if k.IsValid() && v.IsValid() {
toValue.SetMapIndex(k, v)
}
}
default:
toValue = fromValue
}
if !toValue.IsValid() {
return nilValue
}
// handle non-pointer returns -- the reflect.New earlier always creates a pointer
if !isPtr(baseTargetType) {
toValue = fromPtr(toValue)
}
toValue = c.convertValueTypes(toValue, baseTargetType)
// handle elements which are now pointers
if isPtr(targetType) {
toValue = toPtr(toValue)
}
return toValue
}
func (c *conversion) callConversionFunc(fromValue reflect.Value, fromType reflect.Type, baseTargetType reflect.Type, _ error, toValue reflect.Value) (reflect.Value, bool) {
if c.chain.funcs[fromType] != nil && c.chain.funcs[fromType][baseTargetType] != nil {
convertFunc := c.chain.funcs[fromType][baseTargetType]
err := convertFunc(fromValue, toValue.Addr())
if err != nil {
c.errf("an error occurred calling %s.%s: %v", baseTargetType.Name(), convertFromName, err)
return nilValue, true
}
}
return reflect.Value{}, false
}
// convertValueTypes takes a value and a target type, and attempts to convert
// between the Types - e.g. string -> int. when this function is called the value
func (c *conversion) convertValueTypes(value reflect.Value, targetType reflect.Type) reflect.Value {
typ := value.Type()
switch {
// if the Types are the same, just return the value
case typ == targetType:
return value
case typ.Kind() == targetType.Kind() && typ.ConvertibleTo(targetType):
return value.Convert(targetType)
case value.IsZero() && isPrimitive(targetType):
// do nothing, will return nilValue
case isPrimitive(typ) && isPrimitive(targetType):
// get a string representation of the value
str := fmt.Sprintf("%v", value.Interface()) // TODO is there a better way to get a string representation?
var err error
var out any
switch {
case isString(targetType):
out = str
case isBool(targetType):
out, err = strconv.ParseBool(str)
case isInt(targetType):
out, err = strconv.Atoi(str)
case isUint(targetType):
out, err = strconv.ParseUint(str, 10, 64)
case isFloat(targetType):
out, err = strconv.ParseFloat(str, 64)
}
if err != nil {
c.err(err)
return nilValue
}
v := reflect.ValueOf(out)
v = v.Convert(targetType)
return v
case isSlice(typ) && isSlice(targetType):
// this should already be handled in getValue
case isSlice(typ):
// this may be lossy
if value.Len() > 0 {
v := value.Index(0)
return c.convertValueTypes(v, targetType)
}
return c.convertValueTypes(nilValue, targetType)
case isSlice(targetType):
elementType := targetType.Elem()
v := c.convertValueTypes(value, elementType)
if v == nilValue {
return v
}
slice := reflect.MakeSlice(targetType, 1, 1)
slice.Index(0).Set(v)
return slice
}
c.errf("unable to convert from: %v to %v", value.Interface(), targetType.Name())
return nilValue
}
func (c *conversion) findConvertableType(fromType reflect.Type, targetType reflect.Type) reflect.Type {
converters := c.chain.funcs[fromType]
if converters == nil {
return nil
}
if v, ok := converters[targetType]; ok {
if v == nil {
return nil
}
return targetType
}
var found *reflect.Type
for target := range converters {
if target.AssignableTo(targetType) {
if found == nil {
found = &target
} else {
// found multiple
found = nil
break
}
}
}
if found == nil {
// if we didn't find exactly 1, don't check again
converters[targetType] = nil
return nil
}
converters[targetType] = converters[*found]
return *found
}
func isPtr(typ reflect.Type) bool {
return typ.Kind() == reflect.Ptr
}
func isPrimitive(typ reflect.Type) bool {
return isString(typ) || isBool(typ) || isInt(typ) || isUint(typ) || isFloat(typ)
}
func isString(typ reflect.Type) bool {
return typ.Kind() == reflect.String
}
func isBool(typ reflect.Type) bool {
return typ.Kind() == reflect.Bool
}
func isInt(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64:
return true
default:
return false
}
}
func isUint(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64:
return true
default:
return false
}
}
func isFloat(typ reflect.Type) bool {
switch typ.Kind() {
case reflect.Float32,
reflect.Float64:
return true
default:
return false
}
}
func isStruct(typ reflect.Type) bool {
return typ.Kind() == reflect.Struct
}
func isSlice(typ reflect.Type) bool {
return typ.Kind() == reflect.Slice
}
func isMap(typ reflect.Type) bool {
return typ.Kind() == reflect.Map
}
func isInterface(targetType reflect.Type) bool {
return targetType.Kind() == reflect.Interface
}
func toPtr(val reflect.Value) reflect.Value {
typ := val.Type()
if !isPtr(typ) {
// this creates a pointer type inherently
ptrVal := reflect.New(typ)
ptrVal.Elem().Set(val)
val = ptrVal
}
return val
}
func fromPtr(val reflect.Value) reflect.Value {
if isPtr(val.Type()) {
val = val.Elem()
}
return val
}
// convertFromName constant to find the ConvertFrom method
const convertFromName = "ConvertFrom"
var (
// nilValue is returned in a number of cases when a value should not be set
nilValue = reflect.ValueOf(nil)
)