-
Notifications
You must be signed in to change notification settings - Fork 29
/
types.go
221 lines (197 loc) · 6.2 KB
/
types.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
package toolbox
import (
"fmt"
"reflect"
"time"
)
//Zeroable represents object that can call IsZero
type Zeroable interface {
//IsZero returns true, if value of object was zeroed.
IsZero() bool
}
//IsInt returns true if input is an int
func IsInt(input interface{}) bool {
switch input.(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return true
}
return false
}
//IsNumber returns true if type is either float or int
func IsNumber(input interface{}) bool {
return IsFloat(input) || IsInt(input)
}
//IsFloat returns true if input is a float
func IsFloat(input interface{}) bool {
switch input.(type) {
case float32, float64:
return true
}
return false
}
//IsBool returns true if input is a boolean
func IsBool(input interface{}) bool {
switch input.(type) {
case bool:
return true
}
return false
}
//IsString returns true if input is a string
func IsString(input interface{}) bool {
switch input.(type) {
case string:
return true
}
return false
}
//CanConvertToString checks if input can be converted to string
func CanConvertToString(input interface{}) bool {
return reflect.TypeOf(input).AssignableTo(reflect.TypeOf(""))
}
//IsTime returns true if input is a time
func IsTime(input interface{}) bool {
switch input.(type) {
case time.Time:
return true
case *time.Time:
return true
}
return false
}
//IsMap returns true if input is a map
func IsMap(input interface{}) bool {
switch input.(type) {
case map[string]interface{}:
return true
}
candidateType := DereferenceType(reflect.TypeOf(input))
return candidateType.Kind() == reflect.Map
}
//IsStruct returns true if input is a map
func IsStruct(input interface{}) bool {
if input == nil {
return false
}
inputType := DereferenceType(input)
return inputType.Kind() == reflect.Struct
}
//IsSlice returns true if input is a map
func IsSlice(input interface{}) bool {
switch input.(type) {
case []interface{}:
return true
case []string:
return true
}
candidateType := DereferenceType(reflect.TypeOf(input))
return candidateType.Kind() == reflect.Slice
}
//IsFunc returns true if input is a funct
func IsFunc(input interface{}) bool {
candidateType := DereferenceType(reflect.TypeOf(input))
return candidateType.Kind() == reflect.Func
}
//IsZero returns true if input is a zeroable
func IsZero(input interface{}) bool {
if zeroable, ok := input.(Zeroable); ok {
return zeroable.IsZero()
}
return false
}
//IsPointer returns true if input is a pointer
func IsPointer(input interface{}) bool {
if reflectType, ok := input.(reflect.Type); ok {
return reflectType.Kind() == reflect.Ptr
}
return reflect.TypeOf(input).Kind() == reflect.Ptr
}
//AssertPointerKind checks if input is a pointer of the passed in kind, if not it panic with message including name
func AssertPointerKind(input interface{}, kind reflect.Kind, name string) {
AssertTypeKind(reflect.TypeOf(input), reflect.Ptr, name)
AssertTypeKind(reflect.TypeOf(input).Elem(), kind, name)
}
//AssertKind checks if input is of the passed in kind, if not it panic with message including name
func AssertKind(input interface{}, kind reflect.Kind, name string) {
AssertTypeKind(reflect.TypeOf(input), kind, name)
}
//AssertTypeKind checks if dataType is of the passed in kind, if not it panic with message including name
func AssertTypeKind(dataType reflect.Type, kind reflect.Kind, name string) {
if dataType.Kind() != kind {
panic(fmt.Sprintf("failed to check: %v - expected kind: %v but found %v (%v)", name, kind.String(), dataType.Kind(), dataType.String()))
}
}
//DiscoverValueByKind returns unwrapped input that matches expected kind, or panic if this is not possible
func DiscoverValueByKind(input interface{}, expected reflect.Kind) reflect.Value {
result, err := TryDiscoverValueByKind(input, expected)
if err == nil {
return result
}
panic(err)
}
//TryDiscoverValueByKind returns unwrapped input that matches expected kind, or panic if this is not possible
func TryDiscoverValueByKind(input interface{}, expected reflect.Kind) (reflect.Value, error) {
value, ok := input.(reflect.Value)
if !ok {
value = reflect.ValueOf(input)
}
if value.Kind() == expected {
return value, nil
} else if value.Kind() == reflect.Ptr {
return TryDiscoverValueByKind(value.Elem(), expected)
} else if value.Kind() == reflect.Interface {
return TryDiscoverValueByKind(value.Elem(), expected)
}
return value, fmt.Errorf("failed to discover value by kind expected: %v, actual:%T on %v:", expected.String(), value.Type(), value)
}
//IsValueOfKind returns true if passed in input is of supplied kind.
func IsValueOfKind(input interface{}, kind reflect.Kind) bool {
value, ok := input.(reflect.Value)
if !ok {
value = reflect.ValueOf(input)
}
if value.Kind() == kind {
return true
} else if value.Kind() == reflect.Ptr {
return IsValueOfKind(value.Elem(), kind)
} else if value.Kind() == reflect.Interface {
return IsValueOfKind(value.Elem(), kind)
}
return false
}
//DiscoverTypeByKind returns unwrapped input type that matches expected kind, or panic if this is not possible
func DiscoverTypeByKind(input interface{}, expected reflect.Kind) reflect.Type {
result, err := TryDiscoverTypeByKind(input, expected)
if err != nil {
panic(err)
}
return result
}
//TryDiscoverTypeByKind returns unwrapped input type that matches expected kind, or error
func TryDiscoverTypeByKind(input interface{}, expected reflect.Kind) (reflect.Type, error) {
value, ok := input.(reflect.Type)
if !ok {
value = reflect.TypeOf(input)
}
if value.Kind() == expected {
return value, nil
} else if value.Kind() == reflect.Ptr || value.Kind() == reflect.Slice {
return TryDiscoverTypeByKind(value.Elem(), expected)
}
return nil, fmt.Errorf("failed to discover type by kind %v, on %v:", expected.String(), value)
}
//DiscoverComponentType returns type unwrapped from pointer, slice or map
func DiscoverComponentType(input interface{}) reflect.Type {
valueType, ok := input.(reflect.Type)
if !ok {
valueType = reflect.TypeOf(input)
}
if valueType.Kind() == reflect.Ptr {
return DiscoverComponentType(valueType.Elem())
} else if valueType.Kind() == reflect.Slice {
return valueType.Elem()
} else if valueType.Kind() == reflect.Map {
return valueType.Elem()
}
return valueType
}