-
Notifications
You must be signed in to change notification settings - Fork 0
/
minmax.go
222 lines (198 loc) · 4.89 KB
/
minmax.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package validation
import (
"fmt"
"mime/multipart"
"reflect"
"time"
)
type ThresholdRule struct {
threshold interface{}
operator int
message string
}
const (
greaterThan = iota
greaterEqualThan
lessThan
lessEqualThan
)
// Min is a validation rule that checks if a value is greater or equal than the specified value.
// By calling Exclusive, the rule will check if the value is strictly greater than the specified value.
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Min(min interface{}) *ThresholdRule {
return &ThresholdRule{
threshold: min,
operator: greaterEqualThan,
message: fmt.Sprintf("must be no less than %v", min),
}
}
// Max is a validation rule that checks if a value is less or equal than the specified value.
// By calling Exclusive, the rule will check if the value is strictly less than the specified value.
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Max(max interface{}) *ThresholdRule {
return &ThresholdRule{
threshold: max,
operator: lessEqualThan,
message: fmt.Sprintf("must be no greater than %v", max),
}
}
// Exclusive sets the comparison to exclude the boundary value.
func (r *ThresholdRule) Exclusive() *ThresholdRule {
if r.operator == greaterEqualThan {
r.operator = greaterThan
r.message = fmt.Sprintf("must be greater than %v", r.threshold)
return r
}
if r.operator == lessEqualThan {
r.operator = lessThan
r.message = fmt.Sprintf("must be less than %v", r.threshold)
}
return r
}
// Validate checks if the given value is valid or not.
func (r *ThresholdRule) Validate(value interface{}) (code int, args []interface{}) {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return
}
rv := reflect.ValueOf(r.threshold)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
checkInt := true
slicePtrHeaders, ok := value.([]*multipart.FileHeader)
if ok {
checkInt = false
var sum int64
for i := range slicePtrHeaders {
sum += slicePtrHeaders[i].Size
}
if r.compareInt(rv.Int(), sum) {
return
}
}
sliceHeaders, ok := value.([]multipart.FileHeader)
if ok {
checkInt = false
var sum int64
for i := range sliceHeaders {
sum += sliceHeaders[i].Size
}
if r.compareInt(rv.Int(), sum) {
return
}
}
ptrHeader, ok := value.(*multipart.FileHeader)
if ok {
checkInt = false
if r.compareInt(rv.Int(), ptrHeader.Size) {
return
}
}
header, ok := value.(multipart.FileHeader)
if ok {
checkInt = false
if r.compareInt(rv.Int(), header.Size) {
return
}
}
if checkInt == true {
v, err := ToInt(value)
if err != nil {
code = 1000
return
}
if r.compareInt(rv.Int(), v) {
return
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
v, err := ToUint(value)
if err != nil {
code = 1000
return
}
if r.compareUint(rv.Uint(), v) {
return
}
case reflect.Float32, reflect.Float64:
v, err := ToFloat(value)
if err != nil {
code = 1000
return
}
if r.compareFloat(rv.Float(), v) {
return
}
case reflect.Struct:
t, ok := r.threshold.(time.Time)
if !ok {
code = 1000
return
}
v, ok := value.(time.Time)
if !ok {
code = 1000
return
}
if v.IsZero() || r.compareTime(t, v) {
return
}
default:
code = 1000
return
}
code = 1000
return
}
func (r *ThresholdRule) compareInt(threshold, value int64) bool {
switch r.operator {
case greaterThan:
return value > threshold
case greaterEqualThan:
return value >= threshold
case lessThan:
return value < threshold
default:
return value <= threshold
}
}
func (r *ThresholdRule) compareUint(threshold, value uint64) bool {
switch r.operator {
case greaterThan:
return value > threshold
case greaterEqualThan:
return value >= threshold
case lessThan:
return value < threshold
default:
return value <= threshold
}
}
func (r *ThresholdRule) compareFloat(threshold, value float64) bool {
switch r.operator {
case greaterThan:
return value > threshold
case greaterEqualThan:
return value >= threshold
case lessThan:
return value < threshold
default:
return value <= threshold
}
}
func (r *ThresholdRule) compareTime(threshold, value time.Time) bool {
switch r.operator {
case greaterThan:
return value.After(threshold)
case greaterEqualThan:
return value.After(threshold) || value.Equal(threshold)
case lessThan:
return value.Before(threshold)
default:
return value.Before(threshold) || value.Equal(threshold)
}
}