-
Notifications
You must be signed in to change notification settings - Fork 2
/
api_internal_hook.go
106 lines (97 loc) · 2.23 KB
/
api_internal_hook.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package cosy
import (
"github.com/elliotchance/orderedmap/v3"
"github.com/uozi-tech/cosy/model"
)
func getHook[T any]() func(core *Ctx[T]) {
resolved := model.GetResolvedModel[T]()
return func(core *Ctx[T]) {
var preloads []string
for _, field := range resolved.OrderedFields {
dir := field.CosyTag.GetItem()
switch dir {
case Preload:
preloads = append(preloads, field.Name)
}
}
if len(preloads) > 0 {
core.SetPreloads(preloads...)
}
}
}
func getListHook[T any]() func(core *Ctx[T]) {
resolved := model.GetResolvedModel[T]()
return func(core *Ctx[T]) {
var (
in []string
eq []string
fussy []string
orIn []string
orEq []string
orFussy []string
preload []string
search []string
between []string
customFilters = orderedmap.NewOrderedMap[string, string]() // key=>filter
)
for _, field := range resolved.OrderedFields {
dirs := field.CosyTag.GetList()
for _, dir := range dirs {
switch dir {
case In:
in = append(in, field.JsonTag)
case Equal:
eq = append(eq, field.JsonTag)
case Fussy:
fussy = append(fussy, field.JsonTag)
case OrIn:
orIn = append(orIn, field.JsonTag)
case OrEqual:
orEq = append(orEq, field.JsonTag)
case OrFussy:
orFussy = append(orFussy, field.JsonTag)
case Preload:
preload = append(preload, field.Name)
case Search:
search = append(search, field.JsonTag)
case Between:
between = append(between, field.JsonTag)
default:
customFilters.Set(field.JsonTag, dir)
}
}
}
if len(in) > 0 {
core.SetIn(in...)
}
if len(eq) > 0 {
core.SetEqual(eq...)
}
if len(fussy) > 0 {
core.SetFussy(fussy...)
}
if len(orIn) > 0 {
core.SetOrIn(orIn...)
}
if len(orEq) > 0 {
core.SetOrEqual(orEq...)
}
if len(orFussy) > 0 {
core.SetOrFussy(orFussy...)
}
if len(preload) > 0 {
core.SetPreloads(preload...)
}
if len(search) > 0 {
core.SetSearchFussyKeys(search...)
}
if len(between) > 0 {
core.SetBetween(between...)
}
if customFilters.Len() > 0 {
for key, filterName := range customFilters.AllFromFront() {
core.SetCustomFilter(key, filterName)
}
}
}
}