-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
68 lines (60 loc) · 1.38 KB
/
utils.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
package hqi
import (
"reflect"
"strings"
)
// Export this
// Convert struct to M
func Struct2M(obj interface{}) M {
if v, ok := obj.(M); ok { // already an M pass through
return v
}
var ret = M{}
//objTyp := reflect.TypeOf(obj)
objVal := reflect.ValueOf(obj)
for i := 0; i < objVal.Type().NumField(); i++ {
fieldTyp := objVal.Type().Field(i)
value := objVal.Field(i)
valI := value.Interface()
fName := fieldTyp.Name
omitEmpty := false
// PARSE struct TAGS
tagStr, ok := fieldTyp.Tag.Lookup("hqi")
if ok {
opts := strings.Split(tagStr, ",")
if opts[0] != "" {
fName = opts[0]
}
if len(opts) > 1 && opts[1] == "omitempty" {
omitEmpty = true
}
}
// Check nil or zero if omitEmpty
if valI == nil || (isZero(valI) && omitEmpty) {
continue
}
valKind := reflect.TypeOf(valI).Kind()
switch valKind {
case reflect.Slice:
var s = []M{} // new slice
for si := 0; si < value.Len(); si++ {
s = append(s, Struct2M(value.Index(si).Interface()))
}
ret[fName] = s
case reflect.Map:
var m = M{}
for _, k := range value.MapKeys() {
m[k.String()] = value.MapIndex(k).Interface()
}
ret[fName] = m
case reflect.Struct:
ret[fName] = Struct2M(valI) // recursive
default:
ret[fName] = valI
}
}
return ret
}
func isZero(x interface{}) bool {
return reflect.DeepEqual(x, reflect.Zero(reflect.TypeOf(x)).Interface())
}