-
Notifications
You must be signed in to change notification settings - Fork 8
/
cvte.go
335 lines (304 loc) · 6.79 KB
/
cvte.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
package cvt
import (
"errors"
"fmt"
"reflect"
"sort"
"strings"
)
var errConvFail = errors.New("convert failed")
var errFieldNotFound = errors.New("field not found")
var errUnsupportedTypeNil = errors.New("unsupported type: nil")
var formatOutOfLimitInt = "%w, out of max limit value(%d)"
var formatOutOfLimitFloat = "%w, out of max limit value(%f)"
var formatExtend = "%v, %w"
// Len return size of string, slice, array or map
func Len(v interface{}) int {
if v == nil {
return 0
}
switch vv := v.(type) {
case string:
return len(vv)
case []bool:
return len(vv)
case []byte:
return len(vv)
case []rune:
return len(vv)
case []string:
return len(vv)
case []int:
return len(vv)
case []int8:
return len(vv)
case []int16:
return len(vv)
case []int64:
return len(vv)
case []uint:
return len(vv)
case []uint16:
return len(vv)
case []uint32:
return len(vv)
case []uint64:
return len(vv)
case []float32:
return len(vv)
case []float64:
return len(vv)
case []interface{}:
return len(vv)
case map[string]interface{}:
return len(vv)
case map[string]string:
return len(vv)
case map[string]int:
return len(vv)
}
_, rv := Indirect(v)
switch rv.Kind() {
case reflect.Slice, reflect.Array, reflect.Map:
return rv.Len()
}
return -1
}
// IsEmpty checks value for empty state
func IsEmpty(v interface{}) bool {
switch vv := v.(type) {
case nil:
return true
case bool:
return !vv
case int:
return vv == 0
case int8:
return vv == 0
case int16:
return vv == 0
case int32:
return vv == 0
case int64:
return vv == 0
case uint:
return vv == 0
case uint8:
return vv == 0
case uint16:
return vv == 0
case uint32:
return vv == 0
case uint64:
return vv == 0
case float32:
return vv == 0
case float64:
return vv == 0
case string:
return vv == ""
case []int:
return len(vv) == 0
case []int8:
return len(vv) == 0
case []int16:
return len(vv) == 0
case []int32:
return len(vv) == 0
case []int64:
return len(vv) == 0
case []uint:
return len(vv) == 0
case []uint8:
return len(vv) == 0
case []uint16:
return len(vv) == 0
case []uint32:
return len(vv) == 0
case []uint64:
return len(vv) == 0
case []float32:
return len(vv) == 0
case []float64:
return len(vv) == 0
case []bool:
return len(vv) == 0
case []interface{}:
return len(vv) == 0
case []string:
return len(vv) == 0
}
_, rv := Indirect(v)
return rv.IsZero()
}
// Field return the field value from map/struct, with default value
func Field(v interface{}, field interface{}, def ...interface{}) interface{} {
if v, err := FieldE(v, field); err == nil {
return v
}
if len(def) > 0 {
return def[0]
}
return nil
}
// FieldE return the field value from map/struct, ignore the field type
func FieldE(val interface{}, field interface{}) (interface{}, error) {
if val == nil {
return nil, errUnsupportedTypeNil
}
sf := String(field) // match with the String of field, so field can be any type
_, rv := Indirect(val)
switch rv.Kind() {
case reflect.Map: // key of map
for _, key := range rv.MapKeys() {
if String(key.Interface()) == sf {
return rv.MapIndex(key).Interface(), nil
}
}
case reflect.Struct: // field of struct
vv := rv.FieldByName(sf)
if vv.IsValid() {
return vv.Interface(), nil
}
}
return nil, fmt.Errorf("%w(%s)", errFieldNotFound, sf)
}
// Typeof returns a string containing the name of the type of `val`.
func Typeof(val interface{}) string {
return fmt.Sprintf("%T", val)
}
// return the values of struct fields, and deep find the embedded fields
func deepStructValues(rv reflect.Value) (sl []interface{}) {
for j := 0; j < rv.NumField(); j++ {
if rv.Type().Field(j).Anonymous {
sl = append(sl, deepStructValues(rv.Field(j))...)
} else if rv.Field(j).CanInterface() {
sl = append(sl, rv.Field(j).Interface())
}
}
return
}
// return the name of struct fields, and deep find the embedded fields
func deepStructFields(rt reflect.Type) (sl []string) {
rt = ptrType(rt)
type field struct {
level int8
index int
}
var exists = make(map[string]field)
fn := func(v string, level int8) {
ff, ok := exists[v]
if ok && ff.level <= level {
return
} else if ok && ff.level > level {
sl = append(sl[:ff.index], sl[ff.index+1:]...)
}
sl = append(sl, v)
exists[v] = field{level, len(sl) - 1}
}
// sort by field definition order, include embed field
for j := 0; j < rt.NumField(); j++ {
f := rt.Field(j)
t := ptrType(f.Type)
// embed struct, include pointer struct
if f.Anonymous && t.Kind() == reflect.Struct {
for _, v := range deepStructFields(t) {
fn(v, 1)
}
} else { // single field, include pointer field
fn(f.Name, 0)
}
}
return
}
// return the map keys, which sorted by asc
func sortedMapKeys(v reflect.Value) (s []reflect.Value) {
s = v.MapKeys()
sort.Slice(s, func(i, j int) bool {
return strings.Compare(String(s[i].Interface()), String(s[j].Interface())) < 0
})
return
}
func ptrType(rt reflect.Type) reflect.Type {
if rt.Kind() == reflect.Ptr {
for rt.Kind() == reflect.Ptr {
rt = rt.Elem()
}
}
return rt
}
func ptrValue(rv reflect.Value) reflect.Value {
if rv.Kind() == reflect.Ptr {
for rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
}
return rv
}
// Indirect returns the value with base type
func Indirect(a interface{}) (val interface{}, rv reflect.Value) {
if a == nil {
return
}
rv = reflect.ValueOf(a)
val = rv.Interface()
switch rv.Kind() {
case reflect.Ptr: // indirect the base type, if has been referenced many times
for rv.Kind() == reflect.Ptr {
// stop indirect until nil, avoid stack overflow
if rv.IsNil() {
val = nil
return
}
rv = rv.Elem()
}
return Indirect(rv.Interface())
case reflect.Bool:
val = rv.Bool()
case reflect.Int:
val = int(rv.Int())
case reflect.Int8:
val = int8(rv.Int())
case reflect.Int16:
val = int16(rv.Int())
case reflect.Int32:
val = int32(rv.Int())
case reflect.Int64:
val = rv.Int()
case reflect.Uint:
val = uint(rv.Uint())
case reflect.Uint8:
val = uint8(rv.Uint())
case reflect.Uint16:
val = uint16(rv.Uint())
case reflect.Uint32:
val = uint32(rv.Uint())
case reflect.Uint64:
val = rv.Uint()
case reflect.Float32:
val = float32(rv.Float())
case reflect.Float64:
val = rv.Float()
case reflect.String:
val = rv.String()
case reflect.Slice:
// []byte
if rv.Type().Elem().Kind() == reflect.Uint8 {
val = rv.Bytes()
}
}
return
}
func newErr(val interface{}, t string) error {
return fmt.Errorf("unable to convert %#v of type %T to %s", val, val, t)
}
// catching an error and return a new
func catch(t string, val interface{}, e error) error {
if e != nil {
if errors.Is(e, errConvFail) || errors.Is(e, errFieldNotFound) || errors.Is(e, errUnsupportedTypeNil) {
return newErr(val, t)
}
return fmt.Errorf(formatExtend, newErr(val, t), e)
}
return nil
}