-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconverter.go
84 lines (72 loc) · 2.18 KB
/
converter.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
package convert
import (
"reflect"
)
// NilValue represents a nil value to convert (from/to)
type NilValue struct {
reflect.Value
}
// MapValue represents a map value to convert (from/to)
type MapValue struct {
reflect.Value
}
// StructValue represents a struct value to convert (from/to)
type StructValue struct {
reflect.Value
}
// SliceValue represents a slice value to convert (from/to)
type SliceValue struct {
reflect.Value
}
// NilType can be used to specify a recipe with the source/destination with a nil value
var NilType = reflect.TypeOf((*NilValue)(nil)).Elem()
// MapType can be used to specify a recipe with the source/destination with a map type
var MapType = reflect.TypeOf((*MapValue)(nil)).Elem()
// StructType can be used to specify a recipe with the source/destination with a struct type
var StructType = reflect.TypeOf((*StructValue)(nil)).Elem()
// SliceType can be used to specify a recipe with the source/destination with a slice type
var SliceType = reflect.TypeOf((*SliceValue)(nil)).Elem()
// Converter is the instance that will be used to convert values
type Converter interface {
Options() *Options
Convert(src, dst interface{}, options ...Options) error
MustConvert(src, dst interface{}, options ...Options)
ConvertReflectValue(src, dst reflect.Value, options ...Options) error
MustConvertReflectValue(src, dst reflect.Value, options ...Options)
}
func isGenericType(t reflect.Type) bool {
switch t {
case NilType:
return true
case MapType:
return true
case StructType:
return true
case SliceType:
return true
}
return false
}
func getGenericWrapper(v reflect.Type) (wrapParam, bool) {
switch v {
case NilType:
return func(value reflect.Value) reflect.Value {
return reflect.ValueOf(NilValue{value})
}, true
case MapType:
return func(value reflect.Value) reflect.Value {
return reflect.ValueOf(MapValue{value})
}, true
case StructType:
return func(value reflect.Value) reflect.Value {
return reflect.ValueOf(StructValue{value})
}, true
case SliceType:
return func(value reflect.Value) reflect.Value {
return reflect.ValueOf(SliceValue{value})
}, true
}
return func(value reflect.Value) reflect.Value {
return value
}, false
}