/
go.go
47 lines (39 loc) · 976 Bytes
/
go.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
package gmongo
import (
"reflect"
"strings"
)
func removeStringFromStringIfExists(str string, remove string) string {
// first check length
if len(str) < len(remove) {
return str
}
// Use strings.Replace to remove the substring
return strings.Replace(str, remove, "", -1)
}
func structToMapWithTags(obj interface{}, tag string) map[string]interface{} {
res := map[string]interface{}{}
if obj == nil {
return res
}
v := reflect.TypeOf(obj)
reflectValue := reflect.ValueOf(obj)
reflectValue = reflect.Indirect(reflectValue)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
for i := 0; i < v.NumField(); i++ {
key := v.Field(i).Tag.Get(tag)
// remove omitempty
key = removeStringFromStringIfExists(key, ",omitempty")
field := reflectValue.Field(i).Interface()
if key != "" && key != "-" {
if v.Field(i).Type.Kind() == reflect.Struct {
res[key] = structToMapWithTags(field, tag)
} else {
res[key] = field
}
}
}
return res
}