-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathparameter.go
287 lines (237 loc) · 6.37 KB
/
parameter.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package tweed
import (
"fmt"
"strconv"
"strings"
)
const (
TextParameter = "text"
BytesParameter = "bytes"
NumericParameter = "number"
DurationParameter = "duration"
)
type MalformedParameterSpec struct {
msg string
}
func (e MalformedParameterSpec) Error() string {
return fmt.Sprintf("malformed parameter specification: %s", e.msg)
}
type WrongParameterValueType struct {
expect string
got string
}
func (e WrongParameterValueType) Error() string {
return fmt.Sprintf("incorrect parameter value type; expected %s, but got %s", e.expect, e.got)
}
type ParameterValueOutOfRange struct {
value string
msg string
}
func (e ParameterValueOutOfRange) Error() string {
return fmt.Sprintf("parameter value '%s' is out of range: %s", e.value, e.msg)
}
type Parameter struct {
Type string `json:"type"`
Minimum string `json:"minimum"`
Maximum string `json:"maximum"`
}
func (p Parameter) Wellformed() error {
switch p.Type {
case TextParameter:
return nil
case BytesParameter:
min := p.Minimum == "" || p.parsesAsBytes(p.Minimum)
max := p.Maximum == "" || p.parsesAsBytes(p.Maximum)
if !min && !max {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum / maximum specified: '%s' / '%s' are not sizes", p.Minimum, p.Maximum),
}
}
if !min {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum specified: '%s' is not a size", p.Minimum),
}
}
if !min {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid maximum specified: '%s' is not a size", p.Maximum),
}
}
if p.Minimum != "" && p.Maximum != "" {
floor, _ := p.parseAsBytes(p.Minimum)
ceil, _ := p.parseAsBytes(p.Maximum)
if ceil < floor {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum / maximum specified: maximum value of '%s' is below minimum value of '%s'", p.Maximum, p.Minimum),
}
}
}
return nil
case NumericParameter:
min := p.Minimum == "" || p.parsesAsNumber(p.Minimum)
max := p.Maximum == "" || p.parsesAsNumber(p.Maximum)
if !min && !max {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum / maximum specified: '%s' / '%s' are not numbers", p.Minimum, p.Maximum),
}
}
if !min {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum specified: '%s' is not a number", p.Minimum),
}
}
if !min {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid maximum specified: '%s' is not a number", p.Maximum),
}
}
if p.Minimum != "" && p.Maximum != "" {
floor, _ := p.parseAsNumber(p.Minimum)
ceil, _ := p.parseAsNumber(p.Maximum)
if ceil < floor {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum / maximum specified: maximum value of '%s' is below minimum value of '%s'", p.Maximum, p.Minimum),
}
}
}
return nil
case DurationParameter:
min := p.Minimum == "" || p.parsesAsDuration(p.Minimum)
max := p.Maximum == "" || p.parsesAsDuration(p.Maximum)
if !min && !max {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum / maximum specified: '%s' / '%s' are not durations", p.Minimum, p.Maximum),
}
}
if !min {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum specified: '%s' is not a duration", p.Minimum),
}
}
if !min {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid maximum specified: '%s' is not a duration", p.Maximum),
}
}
if p.Minimum != "" && p.Maximum != "" {
floor, _ := p.parseAsDuration(p.Minimum)
ceil, _ := p.parseAsDuration(p.Maximum)
if ceil < floor {
return MalformedParameterSpec{
msg: fmt.Sprintf("invalid minimum / maximum specified: maximum value of '%s' is below minimum value of '%s'", p.Maximum, p.Minimum),
}
}
}
return nil
default:
return MalformedParameterSpec{
msg: fmt.Sprintf("unrecognized parameter type '%s'", p.Type),
}
}
}
func (p Parameter) Validate(value string) error {
if err := p.Wellformed(); err != nil {
return err
}
switch p.Type {
case TextParameter:
return nil
case BytesParameter:
min, _ := p.parseAsBytes(p.Minimum)
max, _ := p.parseAsBytes(p.Maximum)
v, err := p.parseAsBytes(value)
if err != nil {
return err
}
if v < min {
return ParameterValueOutOfRange{
value: value,
msg: fmt.Sprintf("value '%s' is below minimum of '%s'", value, p.Minimum),
}
}
if v > max {
return ParameterValueOutOfRange{
value: value,
msg: fmt.Sprintf("value '%s' is above maximum of '%s'", value, p.Maximum),
}
}
return nil
case NumericParameter:
return nil
case DurationParameter:
return nil
default:
return nil
}
}
func (p Parameter) parseAsBytes(in string) (int64, error) {
for i, r := range in {
if r >= '0' && r <= '9' || r == '.' {
continue
}
qty, err := strconv.ParseInt(in[:i], 10, 64)
if err != nil {
return 0, err
}
switch strings.ToLower(in[i:]) {
case "b":
return qty, nil
case "k", "kb", "ki", "kib":
return qty * 1024, nil
case "m", "mb", "mi", "mib":
return qty * 1024 * 1024, nil
case "g", "gb", "gi", "gib":
return qty * 1024 * 1024 * 1024, nil
case "t", "tb", "ti", "tib":
return qty * 1024 * 1024 * 1024 * 1024, nil
default:
return 0, fmt.Errorf("'%s' is not a size", in)
}
}
// unit-less size specification implies 'b'
return strconv.ParseInt(in, 10, 64)
}
func (p Parameter) parsesAsBytes(in string) bool {
_, err := p.parseAsBytes(in)
return err == nil
}
func (p Parameter) parseAsNumber(in string) (int64, error) {
return strconv.ParseInt(in, 10, 64)
}
func (p Parameter) parsesAsNumber(in string) bool {
_, err := p.parseAsNumber(in)
return err == nil
}
func (p Parameter) parseAsDuration(in string) (int64, error) {
for i, r := range in {
if r >= '0' && r <= '9' || r == '.' {
continue
}
qty, err := strconv.ParseInt(in[:i], 10, 64)
if err != nil {
return 0, err
}
switch strings.ToLower(in[i:]) {
case "s":
return qty, nil
case "m":
return qty * 60, nil
case "h":
return qty * 60 * 60, nil
case "d":
return qty * 60 * 60 * 24, nil
case "w":
return qty * 60 * 60 * 24 * 7, nil
case "y":
return qty * 60 * 60 * 24 * 365, nil
default:
return 0, fmt.Errorf("'%s' is not a duration", in)
}
}
// unit-less size specification implies 's'
return strconv.ParseInt(in, 10, 64)
}
func (p Parameter) parsesAsDuration(in string) bool {
_, err := p.parseAsDuration(in)
return err == nil
}