-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlimit.go
90 lines (75 loc) · 1.38 KB
/
limit.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
package gosql
const (
LimitKeyLimit string = "%l" // 数量限制
LimitKeySkip = "%s" // 位移数量
LimitKeyPage = "%p" // 页数,从0开始
)
func IsLimitKey(str string) bool {
switch str {
case LimitKeyLimit, LimitKeySkip, LimitKeyPage:
return true
}
return false
}
type ILimit interface {
IsLimited() bool
}
type LimitRoot struct {
Values []ILimit
}
func (l *LimitRoot) IsLimited() bool {
return false
}
type LimitValue struct {
Key string
Value int
}
func (l *LimitValue) IsLimited() bool {
switch l.Key {
case LimitKeyLimit:
return l.Value > 0
case LimitKeySkip:
return true
case LimitKeyPage:
return l.Value >= 0
}
return false
}
type LimitValues struct {
Limit int
Skip int
Page int
}
func (l *LimitValues) IsLimited() bool {
return l.Limit > 0 && l.GetSkip() >= 0
}
func (l *LimitValues) GetSkip() int {
return l.Skip + l.Limit*l.Page
}
func (l *LimitValues) GetValues() (int, int, int) {
return l.Limit, l.Skip, l.Page
}
type IRuleLimit interface {
SetMaxLimit(int) IRuleLimit
GetLimit(int) int
}
type RuleLimit struct {
maxLimit int
}
func (l *RuleLimit) SetMaxLimit(lmt int) IRuleLimit {
if lmt >= 0 {
l.maxLimit = lmt
} else {
l.maxLimit = 0
}
return l
}
func (l *RuleLimit) GetLimit(lmt int) int {
if lmt <= 0 {
return 0
} else if l.maxLimit <= 0 {
} else if lmt > l.maxLimit {
return l.maxLimit
}
return lmt
}