tool_funs.go
1.6 KB
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
package tool_funs
import (
"fmt"
"github.com/linmadan/egglib-go/utils/string_convert"
"reflect"
"strings"
)
func ParseStructTag(tagStr string, supportTag map[string]int) (map[string]bool, map[string]string, error) {
attrs := make(map[string]bool)
tags := make(map[string]string)
for _, v := range strings.Split(tagStr, `;`) {
if v == "" {
continue
}
v = strings.TrimSpace(v)
if t := strings.ToLower(v); supportTag[t] == 1 {
attrs[t] = true
} else if i := strings.Index(v, "("); i > 0 && strings.Index(v, ")") == len(v)-1 {
name := t[:i]
if supportTag[name] == 2 {
v = v[i+1 : len(v)-1]
tags[name] = v
}
} else {
return attrs, tags, fmt.Errorf("不支持的转化标签")
}
}
return attrs, tags, nil
}
func SimpleStructToMap(toMapStruct interface{}) map[string]interface{} {
elem := reflect.ValueOf(toMapStruct).Elem()
relType := elem.Type()
m := make(map[string]interface{})
for i := 0; i < relType.NumField(); i++ {
m[string_convert.CamelCase(relType.Field(i).Name, false, false)] = elem.Field(i).Interface()
}
return m
}
func QueryOptionsStructToMap(toMapQueryOptionsStruct interface{}) map[string]interface{} {
elem := reflect.ValueOf(toMapQueryOptionsStruct).Elem()
relType := elem.Type()
m := make(map[string]interface{})
for i := 0; i < relType.NumField(); i++ {
if relType.Field(i).Name == "BaseQueryOptions" {
for j := 0; j < elem.Field(i).Type().NumField(); j++ {
m[elem.Field(i).Type().Field(j).Name] = elem.Field(i).Field(j).Interface()
}
} else {
m[relType.Field(i).Name] = elem.Field(i).Interface()
}
}
return m
}