-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
80 lines (71 loc) · 1.8 KB
/
api.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package kit
import (
"reflect"
"strings"
reflections "gopkg.in/oleiade/reflections.v1"
)
func CheckQueryCondition(dto *APIParam, checkStruct interface{}) (apiResult *Result) {
var err error
if len(dto.Fields) != 0 {
var ok bool
fields := strings.Split(dto.Fields, ",")
for _, v := range fields {
if ok, err = reflections.HasField(checkStruct, PascalCase(v)); err != nil || ok == false {
dto = nil
apiResult = NewApiMessage(10011, "", v)
return
}
}
}
if len(dto.Sort) != 0 {
sorts := strings.Split(dto.Sort, ",")
for _, v := range sorts {
if strings.HasPrefix(v, "-") {
v = string(v[1:])
dto.SortDesc += "," + v
} else {
dto.SortAsc += "," + v
}
if ok, err := reflections.HasField(checkStruct, PascalCase(v)); err != nil || ok == false {
dto = nil
apiResult = NewApiMessage(10011, "", v)
return
}
}
if len(dto.SortDesc) != 0 {
dto.SortDesc = dto.SortDesc[1:]
}
if len(dto.SortAsc) != 0 {
dto.SortAsc = dto.SortAsc[1:]
}
}
apiResult = nil
return
}
func FilterFieldsMap(objArray interface{}, fields []string) []map[string]interface{} {
slice, _ := takeArg(objArray, reflect.Slice)
returnMaps := []map[string]interface{}{}
fieldMap := ResultMap(fields)
for i := 0; i < slice.Len(); i++ {
returnMap := make(map[string]interface{})
for k, v := range fieldMap {
returnMap[k], _ = reflections.GetField(slice.Index(i).Interface(), v)
}
returnMaps = append(returnMaps, returnMap)
}
return returnMaps
}
func ResultMap(keys []string) map[string]string {
result := make(map[string]string, 0)
for _, v := range keys {
result[CamelCase(v)] = PascalCase(v)
}
return result
}
func takeArg(arg interface{}, kind reflect.Kind) (val reflect.Value, ok bool) {
val = reflect.ValueOf(arg)
if val.Kind() == kind {
ok = true
}
return
}