-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
52 lines (46 loc) · 1.14 KB
/
util.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
package accountsrv
import (
"reflect"
"strings"
)
// Function takes in a Struct and returns a Map of that struct using the tags associated
// to the struct fields to create the keys of the Map
func structToMapByTag(item interface{}, tagName string) map[string]interface{} {
// Declare the map that will be returned
res := map[string]interface{}{}
// If input item is null return the map
if item == nil {
return res
}
v := reflect.TypeOf(item)
reflectValue := reflect.ValueOf(item)
reflectValue = reflect.Indirect(reflectValue)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for i := 0; i < v.NumField(); i++ {
tag := v.Field(i).Tag.Get(tagName)
// remove omitEmpty
omitEmpty := false
if strings.HasSuffix(tag, "omitempty") {
omitEmpty = true
idx := strings.Index(tag, ",")
if idx > 0 {
tag = tag[:idx]
} else {
tag = ""
}
}
field := reflectValue.Field(i).Interface()
if tag != "" && tag != "-" {
if v.Field(i).Type.Kind() == reflect.Struct {
res[tag] = structToMapByTag(field, tagName)
} else {
if !(omitEmpty && reflectValue.Field(i).IsZero()) {
res[tag] = field
}
}
}
}
return res
}