-
Notifications
You must be signed in to change notification settings - Fork 7
/
convert.go
361 lines (323 loc) · 7.42 KB
/
convert.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
package portal
import (
"reflect"
"time"
"fmt"
"github.com/spf13/cast"
)
// convert converts value from src type to target type
// Conditions should be considered carefully:
// - from is value type, to is a pointer type
// - from is pointer type,to is value type
// - from is value type, to is value type
// - from and to are all pointer type
func convert(to, from interface{}) (out interface{}, err error) {
v := from
iv := reflect.ValueOf(from)
if iv.Type().Kind() == reflect.Ptr {
v = iv.Elem().Interface()
}
switch to.(type) {
case int:
out, err = cast.ToIntE(v)
case *int:
out, err = toIntPtrE(v)
case int64:
out, err = cast.ToInt64E(v)
case *int64:
out, err = toInt64PtrE(v)
case int32:
out, err = cast.ToInt32E(v)
case *int32:
out, err = toInt32PtrE(v)
case int16:
out, err = cast.ToInt16E(v)
case *int16:
out, err = toInt16PtrE(v)
case int8:
out, err = cast.ToInt8E(v)
case *int8:
out, err = toInt8PtrE(v)
case uint:
out, err = cast.ToUintE(v)
case *uint:
out, err = toUintPtrE(v)
case uint64:
out, err = cast.ToUint64E(v)
case *uint64:
out, err = toUint64PtrE(v)
case uint32:
out, err = cast.ToUint32E(v)
case *uint32:
out, err = toUint32PtrE(v)
case uint16:
out, err = cast.ToUint16E(v)
case *uint16:
out, err = toUint16PtrE(v)
case uint8:
out, err = cast.ToUint8E(v)
case *uint8:
out, err = toUint8PtrE(v)
case string:
out, err = cast.ToStringE(v)
case *string:
out, err = toStringPtrE(v)
case time.Time:
out, err = cast.ToTimeE(v)
case *time.Time:
out, err = toTimePtrE(v)
case time.Duration:
out, err = cast.ToDurationE(v)
case *time.Duration:
out, err = toDurationPtrE(v)
case bool:
out, err = cast.ToBoolE(v)
case *bool:
out, err = toBoolPtrE(v)
case float32:
out, err = cast.ToFloat32E(v)
case *float32:
out, err = toFloat32PtrE(v)
case float64:
out, err = cast.ToFloat64E(v)
case *float64:
out, err = toFloat64PtrE(v)
case map[string]string:
out, err = cast.ToStringMapStringE(v)
case *map[string]string:
out, err = toStringMapStringPtrE(v)
case map[string][]string:
out, err = cast.ToStringMapStringSliceE(v)
case *map[string][]string:
out, err = toStringMapStringSlicePtrE(v)
case map[string]bool:
out, err = cast.ToStringMapBoolE(v)
case *map[string]bool:
out, err = toStringMapBoolPtrE(v)
case map[string]interface{}:
out, err = cast.ToStringMapE(v)
case *map[string]interface{}:
out, err = toStringMapPtrE(v)
case []interface{}:
out, err = cast.ToSliceE(v)
case *[]interface{}:
out, err = toSlicePtrE(v)
case []bool:
out, err = cast.ToBoolSliceE(v)
case *[]bool:
out, err = toBoolSlicePtrE(v)
case []string:
out, err = cast.ToStringSliceE(v)
case *[]string:
out, err = toStringSlicePtrE(v)
case []int:
out, err = cast.ToIntSliceE(v)
case *[]int:
out, err = toIntSlicePtrE(v)
case []time.Duration:
out, err = cast.ToDurationSliceE(v)
case *[]time.Duration:
out, err = toDurationSlicePtrE(v)
default:
return convertWithReflect(to, from)
}
// die trying...
if err != nil {
out, err = convertWithReflect(to, from)
}
return
}
func convertWithReflect(to interface{}, from interface{}) (interface{}, error) {
expectedType := reflect.TypeOf(to)
value := reflect.ValueOf(from)
// type -> type
if value.Type().ConvertibleTo(expectedType) {
return value.Convert(expectedType).Interface(), nil
}
// type -> *type
if expectedType.Kind() == reflect.Ptr && value.Type().ConvertibleTo(expectedType.Elem()) {
expectedValue := reflect.New(expectedType.Elem())
expectedValue.Elem().Set(value.Convert(expectedType.Elem()))
return expectedValue.Interface(), nil
}
// *type -> type
if value.Type().Kind() == reflect.Ptr && value.Type().Elem().ConvertibleTo(expectedType) {
return value.Elem().Convert(expectedType).Interface(), nil
}
return nil, fmt.Errorf("failed to convert from type '%s' to '%s'", value.Type().String(), expectedType.String())
}
func toIntPtrE(v interface{}) (*int, error) {
cv, err := cast.ToIntE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toInt64PtrE(v interface{}) (*int64, error) {
cv, err := cast.ToInt64E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toInt32PtrE(v interface{}) (*int32, error) {
cv, err := cast.ToInt32E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toInt16PtrE(v interface{}) (*int16, error) {
cv, err := cast.ToInt16E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toInt8PtrE(v interface{}) (*int8, error) {
cv, err := cast.ToInt8E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toUintPtrE(v interface{}) (*uint, error) {
cv, err := cast.ToUintE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toUint64PtrE(v interface{}) (*uint64, error) {
cv, err := cast.ToUint64E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toUint32PtrE(v interface{}) (*uint32, error) {
cv, err := cast.ToUint32E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toUint16PtrE(v interface{}) (*uint16, error) {
cv, err := cast.ToUint16E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toUint8PtrE(v interface{}) (*uint8, error) {
cv, err := cast.ToUint8E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toStringPtrE(v interface{}) (*string, error) {
cv, err := cast.ToStringE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toTimePtrE(v interface{}) (*time.Time, error) {
cv, err := cast.ToTimeE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toDurationPtrE(v interface{}) (*time.Duration, error) {
cv, err := cast.ToDurationE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toBoolPtrE(v interface{}) (*bool, error) {
cv, err := cast.ToBoolE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toFloat32PtrE(v interface{}) (*float32, error) {
cv, err := cast.ToFloat32E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toFloat64PtrE(v interface{}) (*float64, error) {
cv, err := cast.ToFloat64E(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toStringMapStringPtrE(v interface{}) (*map[string]string, error) {
cv, err := cast.ToStringMapStringE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toStringMapStringSlicePtrE(v interface{}) (*map[string][]string, error) {
cv, err := cast.ToStringMapStringSliceE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toStringMapBoolPtrE(v interface{}) (*map[string]bool, error) {
cv, err := cast.ToStringMapBoolE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toStringMapPtrE(v interface{}) (*map[string]interface{}, error) {
cv, err := cast.ToStringMapE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toSlicePtrE(v interface{}) (*[]interface{}, error) {
cv, err := cast.ToSliceE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toBoolSlicePtrE(v interface{}) (*[]bool, error) {
cv, err := cast.ToBoolSliceE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toStringSlicePtrE(v interface{}) (*[]string, error) {
cv, err := cast.ToStringSliceE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toIntSlicePtrE(v interface{}) (*[]int, error) {
cv, err := cast.ToIntSliceE(v)
if err != nil {
return nil, err
}
return &cv, nil
}
func toDurationSlicePtrE(v interface{}) (*[]time.Duration, error) {
cv, err := cast.ToDurationSliceE(v)
if err != nil {
return nil, err
}
return &cv, nil
}