-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.go
43 lines (37 loc) · 972 Bytes
/
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
package utils
// NOTE: currently not used
import (
"strconv"
"github.com/gobuffalo/buffalo"
)
// GetParam returns string or integer based on the value.
func GetParam(c buffalo.Context, key string) interface{} {
s := c.Param(key)
i, err := strconv.Atoi(s)
if err == nil {
return i
}
return s
}
// GetIntParam extracts and returns an integer value from request parameter.
// if the parameter is not exist or smaller than min or larger than max,
// it returns min value.
func GetIntParam(c buffalo.Context, key string, min, max int) (i int) {
i, err := strconv.Atoi(c.Param(key))
if err != nil || i < min {
return min
}
if max > min && i > max {
return max
}
return
}
// GetStringParam extracts and returns a string value from request parameter.
// if the parameter is not exist, it returns failback value.
func GetStringParam(c buffalo.Context, key, failback string) (val string) {
val = c.Param(key)
if val == "" {
return failback
}
return
}