utils.go
967 Bytes
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
package utils
import (
"reflect"
"strings"
)
func GetCollName(schema interface{}) string {
s := reflect.TypeOf(schema)
if s.Kind() == reflect.Ptr {
s = reflect.TypeOf(schema).Elem()
}
return strings.ToLower(s.Name())
}
func GetPriKey(schema interface{}) string {
s := reflect.TypeOf(schema)
if s.Kind() == reflect.Ptr {
s = reflect.TypeOf(schema).Elem()
}
var pri string
for i := 0; i < s.NumField(); i++ {
if s.Field(i).Tag.Get("pri") == "1" {
pri = strings.ToLower(s.Field(i).Name)
break
}
}
return pri
}
func FindIndex(schema interface{}) (string, []string){
s := reflect.TypeOf(schema)
if s.Kind() == reflect.Ptr {
s = reflect.TypeOf(schema).Elem()
}
var index []string
for i := 0; i < s.NumField(); i++ {
if s.Field(i).Tag.Get("index") != "" {
js := strings.Split(s.Field(i).Tag.Get("json"), ",")
if len(js) == 0 {
continue
}
index = append(index, js[0])
}
}
return strings.ToLower(s.Name()), index
}