-
Notifications
You must be signed in to change notification settings - Fork 8
/
cvte.go
205 lines (181 loc) · 4.63 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
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"
// 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)
}
// 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
}