-
Notifications
You must be signed in to change notification settings - Fork 3
/
convert.go
346 lines (301 loc) · 9.69 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
package convert
import (
"errors"
"fmt"
"reflect"
)
//nolint:golint // silence consider calling this Recipes
// ConvertRecipes can be used to add recipes to a type in a convenient way
type ConvertRecipes interface {
ConvertRecipes() []Recipe
}
var convertRecipesType = reflect.TypeOf((*ConvertRecipes)(nil)).Elem()
// Converter is the instance that will be used to convert values
type defaultConverter struct {
options Options
}
var defaultConverterInstance = defaultConverter{
options: Options{
SkipUnknownFields: false,
Recipes: getStdRecipes(),
},
}
// Convert converts the specified value to the specified type.
// The behavior can be influenced by using the options
//
// Example:
// var str string
// err := Convert(8, &str)
// if err != nil {
// panic(err)
// }
// fmt.Printf("%s\n", str)
func (conv defaultConverter) Convert(src, dst interface{}, options ...Options) error {
if dst == nil {
return errors.New("destination type cannot be nil")
}
if src == nil {
src = NilValue{}
}
return conv.ConvertReflectValue(reflect.ValueOf(src), reflect.ValueOf(dst), options...)
}
// MustConvert calls Convert() but panics if there is an error
func (conv defaultConverter) MustConvert(src, dstTyp interface{}, options ...Options) {
if err := conv.Convert(src, dstTyp, options...); err != nil {
panic(err)
}
}
// ConvertReflectValue converts the specified reflect value to the specified type
// The behavior can be influenced by using the options
func (conv defaultConverter) ConvertReflectValue(src, dst reflect.Value, options ...Options) error {
// prepare conversion
if !src.IsValid() {
return errors.New("source is invalid")
}
if !dst.IsValid() {
return errors.New("destination is invalid")
}
// unpackInterface src
src = unpackInterface(src)
if dst.Kind() != reflect.Ptr {
return errors.New("destination must be a pointer")
}
if !src.IsValid() {
// src was an interface to nothing
return nil
}
// interface(string) -> string
out := unpackInterface(dst.Elem())
if !out.IsValid() {
// dst was an interface to nothing
// if source was nil destination should also be nil
if src.Type() == NilType {
var n interface{}
dst.Elem().Set(reflect.ValueOf(&n).Elem())
return nil
}
dst.Elem().Set(src)
return nil
}
// create a new temporary variable (this variable is the real type that we want to convert to)
tmp := reflect.New(out.Type())
tmp.Elem().Set(out)
out = tmp
if len(options) > 0 {
conv.options.SkipUnknownFields = options[0].SkipUnknownFields
conv.options.Recipes = joinRecipes(conv.options.Recipes, options[0].Recipes)
}
return conv.convertNow(src, dst, out, options...)
}
// joinRecipes joins recipes and customRecipes it puts the generic recipes on the end
func joinRecipes(recipes, customRecipes []Recipe) []Recipe {
result := make([]Recipe, 0, len(recipes)+len(customRecipes))
// all normal recipes go to the front
for i := range customRecipes {
if isGenericType(customRecipes[i].From) || isGenericType(customRecipes[i].To) {
continue
}
result = append(result, customRecipes[i])
}
for i := range recipes {
if isGenericType(recipes[i].From) || isGenericType(recipes[i].To) {
continue
}
result = append(result, recipes[i])
}
// all generic recipes go to the end
for i := range customRecipes {
if !isGenericType(customRecipes[i].From) && !isGenericType(customRecipes[i].To) {
continue
}
result = append(result, customRecipes[i])
}
for i := range recipes {
if !isGenericType(recipes[i].From) && !isGenericType(recipes[i].To) {
continue
}
result = append(result, recipes[i])
}
return result
}
func (conv defaultConverter) convertNow(src, dst, out reflect.Value, options ...Options) error {
debug("converting %s `%v' to %s\n",
src.Type().String(),
printValue(src),
out.Elem().Type().String())
genericFrom := conv.getGenericType(src.Type())
genericTo := conv.getGenericType(out.Type().Elem())
if genericFrom != nil {
debug2(">> generic from: %s\n", genericFrom.String())
}
if genericTo != nil {
debug2(">> generic to: %s\n", genericTo.String())
}
// add convert recipes if available
if convertRecipes := getConvertRecipes(src); len(convertRecipes) > 0 {
conv.options.Recipes = joinRecipes(conv.options.Recipes, convertRecipes)
}
if convertRecipes := getConvertRecipes(dst); len(convertRecipes) > 0 {
conv.options.Recipes = joinRecipes(conv.options.Recipes, convertRecipes)
}
for _, recipe := range conv.options.Recipes {
if recipe.From != src.Type() && recipe.From != genericFrom {
debug2(">> skipping %v because src %s != %s\n", recipe.Func, recipe.From.String(), src.Type().String())
continue
}
if recipe.To != out.Type() && recipe.To != genericTo {
debug2(">> skipping %v because dst %s != %s\n", recipe.Func, recipe.To.String(), out.Type().String())
continue
}
// debug2("entering %s | %s==%s==%s %s==%s==%s", recipe.Func, recipe.From.String(), src.Type().String(), genericFrom.String(), recipe.To.String(), dst.Type().String(), genericTo.String())
if err := recipe.Func(&conv, src, out); err != nil {
return fmt.Errorf("unable to convert %s to %s: %s", src.Type().String(), out.Elem().Type().String(), err)
}
debug(">> successful (1) %s `%v' to %s `%v'\n", src.Type().String(), printValue(src), out.Elem().Type().String(), printValue(out.Elem()))
// everything was good, set the dst to the copy
dst.Elem().Set(out.Elem())
return nil
}
if src.Kind() == reflect.Ptr {
if src.Elem().IsValid() {
debug(">> following src because no recipe for %s to %s was found\n", src.Type().String(), dst.Type().String())
return conv.ConvertReflectValue(src.Elem(), dst, options...)
}
if src.IsNil() {
debug(">> src is nil\n")
return nil
}
}
if out.Elem().Kind() == reflect.Ptr {
if out.Elem().Elem().IsValid() {
debug(">> following dst because no recipe for %s to %s was found\n", src.Type().String(), out.Elem().Type().String())
return conv.ConvertReflectValue(src, out.Elem(), options...)
}
debug(">> Creating new %s\n", out.Type().Elem().Elem().String())
out = reflect.New(out.Type().Elem().Elem())
err := conv.ConvertReflectValue(src, out, options...)
if err != nil {
return err
}
dst.Elem().Set(out)
debug(">> successful (2) %s `%v' to %s `%v'\n", src.Type().String(), printValue(src), out.Elem().Type().String(), printValue(out.Elem()))
return nil
}
return fmt.Errorf("unable to convert %s to %s: no recipe", src.Type().String(), out.Elem().Type().String())
}
// MustConvertReflectValue calls ConvertReflectValue() but panics if there is an error
func (conv defaultConverter) MustConvertReflectValue(src, dstTyp reflect.Value, options ...Options) {
if err := conv.ConvertReflectValue(src, dstTyp, options...); err != nil {
panic(err)
}
}
func (conv *defaultConverter) getGenericType(p reflect.Type) reflect.Type {
if p == NilType {
return NilType
}
switch p.Kind() {
case reflect.Struct:
return StructType
case reflect.Map:
return MapType
case reflect.Slice, reflect.Array:
return SliceType
}
return nil
}
// Options returns the current Options for this converter
func (conv *defaultConverter) Options() *Options {
return &conv.options
}
// New creates a new converter that can be used multiple times
func New(options ...Options) Converter {
conv := defaultConverterInstance
if len(options) > 0 {
conv.options.SkipUnknownFields = options[0].SkipUnknownFields
conv.options.Recipes = joinRecipes(conv.options.Recipes, options[0].Recipes)
}
return &conv
}
// Convert converts the specified value to the specified type.
// The behavior can be influenced by using the options
//
// Example:
// var str string
// if err := Convert(8, &str); err != nil {
// panic(err)
// }
// fmt.Printf("%s\n", str)
func Convert(src, dst interface{}, options ...Options) error {
return defaultConverterInstance.Convert(src, dst, options...)
}
// MustConvert calls Convert() but panics if there is an error
func MustConvert(src, dst interface{}, options ...Options) {
defaultConverterInstance.MustConvert(src, dst, options...)
}
//nolint:golint // silence consider calling this ReflectValue
// ConvertReflectValue converts the specified reflect value to the specified type
// The behavior can be influenced by using the options
func ConvertReflectValue(src, dstTyp reflect.Value, options ...Options) error {
return defaultConverterInstance.ConvertReflectValue(src, dstTyp, options...)
}
// MustConvertReflectValue calls MustConvertReflectValue() but panics if there is an error
func MustConvertReflectValue(src, dstTyp reflect.Value, options ...Options) {
defaultConverterInstance.MustConvertReflectValue(src, dstTyp, options...)
}
func unpackInterface(value reflect.Value) reflect.Value {
for value.Kind() == reflect.Interface {
value = value.Elem()
}
return value
}
func printValue(value reflect.Value) string {
v := value
for v.IsValid() {
switch v.Kind() {
case reflect.Ptr, reflect.Interface:
if v.IsNil() {
return "<nil>"
}
v = v.Elem()
default:
if v.CanInterface() {
return fmt.Sprintf("%v", v.Interface())
}
return "unknown"
}
}
return ""
}
func getConvertRecipes(value reflect.Value) []Recipe {
if !value.IsValid() {
return nil
}
get := func(value reflect.Value) []Recipe {
if !value.IsValid() {
return nil
}
if !value.CanInterface() {
return nil
}
if !value.Type().Implements(convertRecipesType) {
return nil
}
if r, ok := value.Interface().(ConvertRecipes); ok {
return r.ConvertRecipes()
}
return nil
}
if recipes := get(value); len(recipes) > 0 {
return recipes
}
// maybe the pointer to this has it implemented?
v := reflect.New(value.Type())
if !v.Elem().CanSet() {
return nil
}
if !value.CanInterface() {
return nil
}
v.Elem().Set(value)
return get(v)
}