-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathrequest_params.go
407 lines (361 loc) · 11.7 KB
/
request_params.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package context
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/kataras/iris/v12/core/memstore"
)
// RequestParams is a key string - value string storage which
// context's request dynamic path params are being kept.
// Empty if the route is static.
type RequestParams struct {
memstore.Store
}
// Set inserts a parameter value.
// See `Get` too.
func (r *RequestParams) Set(key, value string) {
if ln := len(r.Store); cap(r.Store) > ln {
r.Store = r.Store[:ln+1]
p := &r.Store[ln]
p.Key = key
p.ValueRaw = value
return
}
r.Store = append(r.Store, memstore.Entry{
Key: key,
ValueRaw: value,
})
}
// Get returns a path parameter's value based on its route's dynamic path key.
func (r *RequestParams) Get(key string) string {
for i := range r.Store {
if kv := r.Store[i]; kv.Key == key {
if v, ok := kv.ValueRaw.(string); ok {
return v // it should always be string here on :string parameter.
}
if v, ok := kv.ValueRaw.(fmt.Stringer); ok {
return v.String()
}
return fmt.Sprintf("%v", kv.ValueRaw)
}
}
return ""
}
// GetEntryAt will return the parameter's internal store's `Entry` based on the index.
// If not found it will return an emptry `Entry`.
func (r *RequestParams) GetEntryAt(index int) memstore.Entry {
entry, _ := r.Store.GetEntryAt(index)
return entry
}
// GetEntry will return the parameter's internal store's `Entry` based on its name/key.
// If not found it will return an emptry `Entry`.
func (r *RequestParams) GetEntry(key string) memstore.Entry {
entry, _ := r.Store.GetEntry(key)
return entry
}
// Visit accepts a visitor which will be filled
// by the key-value params.
func (r *RequestParams) Visit(visitor func(key string, value string)) {
r.Store.Visit(func(k string, v interface{}) {
visitor(k, fmt.Sprintf("%v", v)) // always string here.
})
}
// GetTrim returns a path parameter's value without trailing spaces based on its route's dynamic path key.
func (r *RequestParams) GetTrim(key string) string {
return strings.TrimSpace(r.Get(key))
}
// GetEscape returns a path parameter's double-url-query-escaped value based on its route's dynamic path key.
func (r *RequestParams) GetEscape(key string) string {
return DecodeQuery(DecodeQuery(r.Get(key)))
}
// GetDecoded returns a path parameter's double-url-query-escaped value based on its route's dynamic path key.
// same as `GetEscape`.
func (r *RequestParams) GetDecoded(key string) string {
return r.GetEscape(key)
}
// TrimParamFilePart is a middleware which replaces all route dynamic path parameters
// with values that do not contain any part after the last dot (.) character.
//
// Example Code:
//
// package main
//
// import (
// "github.com/kataras/iris/v12"
// )
//
// func main() {
// app := iris.New()
// app.Get("/{uid:string regexp(^[0-9]{1,20}.html$)}", iris.TrimParamFilePart, handler)
// // TrimParamFilePart can be registered as a middleware to a Party (group of routes) as well.
// app.Listen(":8080")
// }
//
// func handler(ctx iris.Context) {
// //
// // The above line is useless now that we've registered the TrimParamFilePart middleware:
// // uid := ctx.Params().GetTrimFileUint64("uid")
// //
//
// uid := ctx.Params().GetUint64Default("uid", 0)
// ctx.Writef("Param value: %d\n", uid)
// }
func TrimParamFilePart(ctx *Context) { // See #2024.
params := ctx.Params()
for i, param := range params.Store {
if value, ok := param.ValueRaw.(string); ok {
if idx := strings.LastIndexByte(value, '.'); idx > 1 /* at least .h */ {
value = value[0:idx]
param.ValueRaw = value
}
}
params.Store[i] = param
}
ctx.Next()
}
// GetTrimFile returns a parameter value but without the last ".ANYTHING_HERE" part.
func (r *RequestParams) GetTrimFile(key string) string {
value := r.Get(key)
if idx := strings.LastIndexByte(value, '.'); idx > 1 /* at least .h */ {
return value[0:idx]
}
return value
}
// GetTrimFileInt same as GetTrimFile but it returns the value as int.
func (r *RequestParams) GetTrimFileInt(key string) int {
value := r.Get(key)
if idx := strings.LastIndexByte(value, '.'); idx > 1 /* at least .h */ {
value = value[0:idx]
}
v, _ := strconv.Atoi(value)
return v
}
// GetTrimFileUint64 same as GetTrimFile but it returns the value as uint64.
func (r *RequestParams) GetTrimFileUint64(key string) uint64 {
value := r.Get(key)
if idx := strings.LastIndexByte(value, '.'); idx > 1 /* at least .h */ {
value = value[0:idx]
}
v, err := strconv.ParseUint(value, 10, strconv.IntSize)
if err != nil {
return 0
}
return v
}
// GetTrimFileUint same as GetTrimFile but it returns the value as uint.
func (r *RequestParams) GetTrimFileUint(key string) uint {
return uint(r.GetTrimFileUint64(key))
}
func (r *RequestParams) getRightTrimmed(key string, cutset string) string {
return strings.TrimRight(strings.ToLower(r.Get(key)), cutset)
}
// GetTrimHTML returns a parameter value but without the last ".html" part.
func (r *RequestParams) GetTrimHTML(key string) string {
return r.getRightTrimmed(key, ".html")
}
// GetTrimJSON returns a parameter value but without the last ".json" part.
func (r *RequestParams) GetTrimJSON(key string) string {
return r.getRightTrimmed(key, ".json")
}
// GetTrimXML returns a parameter value but without the last ".xml" part.
func (r *RequestParams) GetTrimXML(key string) string {
return r.getRightTrimmed(key, ".xml")
}
// GetIntUnslashed same as Get but it removes the first slash if found.
// Usage: Get an id from a wildcard path.
//
// Returns -1 and false if not path parameter with that "key" found.
func (r *RequestParams) GetIntUnslashed(key string) (int, bool) {
v := r.Get(key)
if v != "" {
if len(v) > 1 {
if v[0] == '/' {
v = v[1:]
}
}
vInt, err := strconv.Atoi(v)
if err != nil {
return -1, false
}
return vInt, true
}
return -1, false
}
// ParamResolvers is the global param resolution for a parameter type for a specific go std or custom type.
//
// Key is the specific type, which should be unique.
// The value is a function which accepts the parameter index
// and it should return the value as the parameter type evaluator expects it.
//
// i.e [reflect.TypeOf("string")] = func(paramIndex int) interface{} {
// return func(ctx *Context) <T> {
// return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(<T>)
// }
// }
//
// Read https://github.com/kataras/iris/tree/main/_examples/routing/macros for more details.
// Checks for total available request parameters length
// and parameter index based on the hero/mvc function added
// in order to support the MVC.HandleMany("GET", "/path/{ps}/{pssecond} /path/{ps}")
// when on the second requested path, the 'pssecond' should be empty.
var ParamResolvers = map[reflect.Type]func(paramIndex int) interface{}{
reflect.TypeOf(""): func(paramIndex int) interface{} {
return func(ctx *Context) string {
if ctx.Params().Len() <= paramIndex {
return ""
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(string)
}
},
reflect.TypeOf(int(1)): func(paramIndex int) interface{} {
return func(ctx *Context) int {
if ctx.Params().Len() <= paramIndex {
return 0
}
// v, _ := ctx.Params().GetEntryAt(paramIndex).IntDefault(0)
// return v
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int)
}
},
reflect.TypeOf(int8(1)): func(paramIndex int) interface{} {
return func(ctx *Context) int8 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int8)
}
},
reflect.TypeOf(int16(1)): func(paramIndex int) interface{} {
return func(ctx *Context) int16 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int16)
}
},
reflect.TypeOf(int32(1)): func(paramIndex int) interface{} {
return func(ctx *Context) int32 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int32)
}
},
reflect.TypeOf(int64(1)): func(paramIndex int) interface{} {
return func(ctx *Context) int64 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int64)
}
},
reflect.TypeOf(uint(1)): func(paramIndex int) interface{} {
return func(ctx *Context) uint {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint)
}
},
reflect.TypeOf(uint8(1)): func(paramIndex int) interface{} {
return func(ctx *Context) uint8 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint8)
}
},
reflect.TypeOf(uint16(1)): func(paramIndex int) interface{} {
return func(ctx *Context) uint16 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint16)
}
},
reflect.TypeOf(uint32(1)): func(paramIndex int) interface{} {
return func(ctx *Context) uint32 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint32)
}
},
reflect.TypeOf(uint64(1)): func(paramIndex int) interface{} {
return func(ctx *Context) uint64 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint64)
}
},
reflect.TypeOf(true): func(paramIndex int) interface{} {
return func(ctx *Context) bool {
if ctx.Params().Len() <= paramIndex {
return false
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(bool)
}
},
reflect.TypeOf(time.Time{}): func(paramIndex int) interface{} {
return func(ctx *Context) time.Time {
if ctx.Params().Len() <= paramIndex {
return unixEpochTime
}
v, ok := ctx.Params().GetEntryAt(paramIndex).ValueRaw.(time.Time)
if !ok {
return unixEpochTime
}
return v
}
},
reflect.TypeOf(time.Weekday(0)): func(paramIndex int) interface{} {
return func(ctx *Context) time.Weekday {
if ctx.Params().Len() <= paramIndex {
return time.Sunday
}
v, ok := ctx.Params().GetEntryAt(paramIndex).ValueRaw.(time.Weekday)
if !ok {
return time.Sunday
}
return v
}
},
}
// ParamResolverByTypeAndIndex will return a function that can be used to bind path parameter's exact value by its Go std type
// and the parameter's index based on the registered path.
// Usage: nameResolver := ParamResolverByKindAndKey(reflect.TypeOf(""), 0)
// Inside a Handler: nameResolver.Call(ctx)[0]
//
// it will return the reflect.Value Of the exact type of the parameter(based on the path parameters and macros).
//
// It is only useful for dynamic binding of the parameter, it is used on "hero" package and it should be modified
// only when Macros are modified in such way that the default selections for the available go std types are not enough.
//
// Returns empty value and false if "k" does not match any valid parameter resolver.
func ParamResolverByTypeAndIndex(typ reflect.Type, paramIndex int) (reflect.Value, bool) {
/* NO:
// This could work but its result is not exact type, so direct binding is not possible.
resolver := m.ParamResolver
fn := func(ctx *context.Context) interface{} {
entry, _ := ctx.Params().GetEntry(paramName)
return resolver(entry)
}
//
// This works but it is slower on serve-time.
paramNameValue := []reflect.Value{reflect.ValueOf(paramName)}
var fnSignature func(*context.Context) string
return reflect.MakeFunc(reflect.ValueOf(&fnSignature).Elem().Type(), func(in []reflect.Value) []reflect.Value {
return in[0].MethodByName("Params").Call(emptyIn)[0].MethodByName("Get").Call(paramNameValue)
// return []reflect.Value{reflect.ValueOf(in[0].Interface().(*context.Context).Params().Get(paramName))}
})
//
*/
r, ok := ParamResolvers[typ]
if !ok || r == nil {
return reflect.Value{}, false
}
return reflect.ValueOf(r(paramIndex)), true
}