-
Notifications
You must be signed in to change notification settings - Fork 0
/
timestamp.go
142 lines (124 loc) · 3.5 KB
/
timestamp.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
package filter
import (
"strconv"
"strings"
)
type TimestampFilter struct {
validators []TimestampValidator
allowVals []string
}
type TimestampValidator func(paramName string, paramValue uint32) *Error
// Timestamp return a timestamp filter.
func Timestamp() *TimestampFilter {
f := new(TimestampFilter)
return f
}
// Allow allow value is a string in the specified list
func (f *TimestampFilter) Allow(vals ...string) *TimestampFilter {
f.allowVals = append(f.allowVals, vals...)
return f
}
// AddValidator add a custom validator to filter
func (f *TimestampFilter) AddValidator(validator TimestampValidator) *TimestampFilter {
f.validators = append(f.validators, validator)
return f
}
// StartFrom valid whether start from specified time.
func (f *TimestampFilter) StartFrom(val uint32) *TimestampFilter {
f.AddValidator(func(paramName string, paramValue uint32) *Error {
if paramValue < val {
return NewError(ErrorInvalidParam, paramName, "TooEarly")
}
return nil
})
return f
}
// StartFrom valid whether end to specified time.
func (f *TimestampFilter) EndTo(val uint32) *TimestampFilter {
f.AddValidator(func(paramName string, paramValue uint32) *Error {
if paramValue < val {
return NewError(ErrorInvalidParam, paramName, "TooLate")
}
return nil
})
return f
}
// After valid whether after specified time.
func (f *TimestampFilter) After(val uint32) *TimestampFilter {
f.AddValidator(func(paramName string, paramValue uint32) *Error {
if paramValue <= val {
return NewError(ErrorInvalidParam, paramName, "TooEarly")
}
return nil
})
return f
}
// Before valid whether before specified time.
func (f *TimestampFilter) Before(val uint32) *TimestampFilter {
f.AddValidator(func(paramName string, paramValue uint32) *Error {
if paramValue >= val {
return NewError(ErrorInvalidParam, paramName, "TooLate")
}
return nil
})
return f
}
// Equal valid whether equal to specified time.
func (f *TimestampFilter) Equal(val uint32) *TimestampFilter {
f.AddValidator(func(paramName string, paramValue uint32) *Error {
if paramValue < val {
return NewError(ErrorInvalidParam, paramName, "TooEarly")
}
if paramValue > val {
return NewError(ErrorInvalidParam, paramName, "TooLate")
}
return nil
})
return f
}
// Between valid whether between two times.
func (f *TimestampFilter) Between(start, end uint32) *TimestampFilter {
f.AddValidator(func(paramName string, paramValue uint32) *Error {
if paramValue < start {
return NewError(ErrorInvalidParam, paramName, "TooEarly")
}
if paramValue > end {
return NewError(ErrorInvalidParam, paramName, "TooLate")
}
return nil
})
return f
}
// Run make the filter running.
func (f *TimestampFilter) Run(paramName string, paramValue interface{}) (interface{}, *Error) {
if paramValue == nil {
return nil, nil
}
var intVal uint32
switch val := paramValue.(type) {
case string:
val = strings.Trim(val, " \t\r\n")
for _, allowVal := range f.allowVals {
if allowVal == val {
return val, nil
}
}
v, err := strconv.ParseUint(val, 10, 0)
if err != nil {
goto parse_error
}
intVal = uint32(v)
case uint32:
intVal = val
default:
goto parse_error
}
for _, validator := range f.validators {
if err := validator(paramName, intVal); err != nil {
return nil, err
}
}
return intVal, nil
parse_error:
return nil, NewError(ErrorInvalidParam, paramName, "NotTimestamp")
}