-
Notifications
You must be signed in to change notification settings - Fork 8
/
cvte.go
132 lines (118 loc) · 3.22 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
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"
// FieldE return the field value from map/struct, ignore the filed 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
_, rt, rv := indirect(val)
switch rt.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)
}
// return the values of struct fields, and deep find the embedded fields
func deepStructValues(rt reflect.Type, rv reflect.Value) (sl []interface{}) {
for j := 0; j < rv.NumField(); j++ {
if rt.Field(j).Anonymous {
sl = append(sl, deepStructValues(rt.Field(j).Type, rv.Field(j))...)
} else if rv.Field(j).CanInterface() {
sl = append(sl, rv.Field(j).Interface())
}
}
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
}
// returns the value with base type
func indirect(a interface{}) (val interface{}, rt reflect.Type, rv reflect.Value) {
if a == nil {
return
}
rt = reflect.TypeOf(a)
rv = reflect.ValueOf(a)
val = rv.Interface()
switch rt.Kind() {
case reflect.Ptr: // indirect the base type, if is be referenced many times
for rv.Kind() == reflect.Ptr && !rv.IsNil() {
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
}