-
Notifications
You must be signed in to change notification settings - Fork 43
/
filter.go
62 lines (54 loc) · 1.57 KB
/
filter.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
package models
import "strings"
type ListView struct {
Total int `json:"total"`
PageNo int `json:"pageNo,omitempty"`
PageSize int `json:"pageSize,omitempty"`
Items interface{} `json:"items"`
}
type Filter struct {
PageNo int `form:"pageNo" json:"pageNo,omitempty"`
PageSize int `form:"pageSize" json:"pageSize,omitempty"`
Name string `form:"name,omitempty" json:"name,omitempty"`
}
type ListOptions struct {
LabelSelector string `form:"selector,omitempty" json:"selector,omitempty"`
NodeSelector string `form:"nodeSelector,omitempty" json:"nodeSelector,omitempty"`
FieldSelector string `form:"fieldSelector,omitempty" json:"fieldSelector,omitempty"`
Limit int64 `form:"limit,omitempty" json:"limit,omitempty"`
Continue string `form:"continue,omitempty" json:"continue,omitempty"`
Filter `json:",inline"`
}
func (f *Filter) GetLimitOffset() int {
if f.PageNo <= 0 {
f.PageNo = 1
}
return (f.PageNo - 1) * f.GetLimitNumber()
}
func (f *Filter) GetLimitNumber() int {
return f.PageSize
}
func (f *Filter) GetFuzzyName() string {
if f.Name == "" {
return "%"
} else if !strings.Contains(f.Name, "%") {
return "%" + f.Name + "%"
}
return f.Name
}
func GetPagingParam(listOptions *ListOptions, resLen int) (start, end int) {
start = 0
end = resLen
if listOptions.GetLimitNumber() > 0 {
start = listOptions.GetLimitOffset()
end = listOptions.GetLimitOffset() + listOptions.GetLimitNumber()
if end > resLen {
end = resLen
}
if start > resLen {
start = 0
end = 0
}
}
return start, end
}