forked from RichardKnop/machinery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect.go
170 lines (140 loc) · 4.22 KB
/
reflect.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
package tasks
import (
"context"
"fmt"
"reflect"
"strings"
)
var (
typesMap = map[string]reflect.Type{
"bool": reflect.TypeOf(true),
"int": reflect.TypeOf(int(1)),
"int8": reflect.TypeOf(int8(1)),
"int16": reflect.TypeOf(int16(1)),
"int32": reflect.TypeOf(int32(1)),
"int64": reflect.TypeOf(int64(1)),
"uint": reflect.TypeOf(uint(1)),
"uint8": reflect.TypeOf(uint8(1)),
"uint16": reflect.TypeOf(uint16(1)),
"uint32": reflect.TypeOf(uint32(1)),
"uint64": reflect.TypeOf(uint64(1)),
"float32": reflect.TypeOf(float32(0.5)),
"float64": reflect.TypeOf(float64(0.5)),
"string": reflect.TypeOf(string("")),
}
ctxType = reflect.TypeOf((*context.Context)(nil)).Elem()
typeConversionError = func(argValue interface{}, argTypeStr string) error {
return fmt.Errorf("%v is not %v", argValue, argTypeStr)
}
)
// ErrUnsupportedType ...
type ErrUnsupportedType struct {
valueType string
}
// NewErrUnsupportedType returns new ErrUnsupportedType
func NewErrUnsupportedType(valueType string) ErrUnsupportedType {
return ErrUnsupportedType{valueType}
}
// Error method so we implement the error interface
func (e ErrUnsupportedType) Error() string {
return fmt.Sprintf("%v is not one of supported types", e.valueType)
}
// ReflectValue converts interface{} to reflect.Value based on string type
func ReflectValue(valueType string, value interface{}) (reflect.Value, error) {
theType, ok := typesMap[valueType]
if !ok {
return reflect.Value{}, NewErrUnsupportedType(valueType)
}
theValue := reflect.New(theType)
// Booleans
if theType.String() == "bool" {
boolValue, ok := value.(bool)
if !ok {
return reflect.Value{}, typeConversionError(value, theType.String())
}
theValue.Elem().SetBool(boolValue)
return theValue.Elem(), nil
}
// Integers
if strings.HasPrefix(theType.String(), "int") {
intValue, err := getIntValue(theType.String(), value)
if err != nil {
return reflect.Value{}, err
}
theValue.Elem().SetInt(intValue)
return theValue.Elem(), err
}
// Unbound integers
if strings.HasPrefix(theType.String(), "uint") {
uintValue, err := getUintValue(theType.String(), value)
if err != nil {
return reflect.Value{}, err
}
theValue.Elem().SetUint(uintValue)
return theValue.Elem(), err
}
// Floating point numbers
if strings.HasPrefix(theType.String(), "float") {
floatValue, err := getFloatValue(theType.String(), value)
if err != nil {
return reflect.Value{}, err
}
theValue.Elem().SetFloat(floatValue)
return theValue.Elem(), err
}
// Strings
if theType.String() == "string" {
stringValue, ok := value.(string)
if !ok {
return reflect.Value{}, typeConversionError(value, theType.String())
}
theValue.Elem().SetString(stringValue)
return theValue.Elem(), nil
}
return reflect.Value{}, NewErrUnsupportedType(valueType)
}
func getIntValue(theType string, value interface{}) (int64, error) {
if strings.HasPrefix(fmt.Sprintf("%T", value), "float") {
// Any numbers from unmarshalled JSON will be float64 by default
// So we first need to do a type conversion to float64
n, ok := value.(float64)
if !ok {
return 0, typeConversionError(value, typesMap[theType].String())
}
// Now we can cast the float64 to int64
return int64(n), nil
}
n, ok := value.(int64)
if !ok {
return 0, typeConversionError(value, typesMap[theType].String())
}
return n, nil
}
func getUintValue(theType string, value interface{}) (uint64, error) {
if strings.HasPrefix(fmt.Sprintf("%T", value), "float") {
// Any numbers from unmarshalled JSON will be float64 by default
// So we first need to do a type conversion to float64
n, ok := value.(float64)
if !ok {
return 0, typeConversionError(value, typesMap[theType].String())
}
// Now we can cast the float64 to uint64
return uint64(n), nil
}
n, ok := value.(uint64)
if !ok {
return 0, typeConversionError(value, typesMap[theType].String())
}
return n, nil
}
func getFloatValue(theType string, value interface{}) (float64, error) {
n, ok := value.(float64)
if !ok {
return 0, typeConversionError(value, typesMap[theType].String())
}
return n, nil
}
// IsContextType checks to see if the type is a context.Context
func IsContextType(t reflect.Type) bool {
return t == ctxType
}