utils.go
1.02 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
package utils
import (
"reflect"
"strings"
)
func GetSchemaType(schema interface{}) reflect.Type {
s := reflect.TypeOf(schema)
if s.Kind() == reflect.Ptr {
s = reflect.TypeOf(schema).Elem()
}
return s
}
func GetCollName(schema interface{}) string {
return strings.ToLower(GetSchemaType(schema).Name())
}
func GetPriKey(schema interface{}) string {
s := GetSchemaType(schema)
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 := GetSchemaType(schema)
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
}
func GetIdxBySlice(s []interface{}, i interface{}) (int){
for idx, v := range s {
if v == i {
return idx
}
}
return -1
}