-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparams.go
87 lines (74 loc) · 2.2 KB
/
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
package otto
import (
"net/url"
"strconv"
"github.com/pkg/errors"
)
// Params holds url params and provides
// simple ways to parse the value to different types
type Params map[string]string
// String returns the value as string
func (p Params) String(key string) string {
return p[key]
}
// Int returns the value as int
func (p Params) Int(key string) (int, error) {
vi, err := strconv.Atoi(p[key])
return vi, errors.Wrapf(err, "failed to parse '%v' to int", p[key])
}
// Bool returns the value as bool
func (p Params) Bool(key string) (bool, error) {
vb, err := strconv.ParseBool(p[key])
return vb, errors.Wrapf(err, "failed to parse '%v' to bool", p[key])
}
// ValueParams holds url.Values and provides
// simple ways to parse the value to different types
type ValueParams struct {
vals url.Values
}
// String gets one value associated with key and returns it as string
func (p ValueParams) String(key string) string {
return p.vals.Get(key)
}
// Strings returns all value associated with key as string slice
func (p ValueParams) Strings(key string) []string {
return p.vals[key]
}
// Int gets one value associated with key and returns it as int
func (p ValueParams) Int(key string) (int, error) {
v := p.vals.Get(key)
vi, err := strconv.Atoi(v)
return vi, errors.Wrapf(err, "failed to parse '%v' to int", v)
}
// Ints returns all value associated with key as int slice
func (p ValueParams) Ints(key string) ([]int, error) {
var ii []int
ss := p.vals[key]
for _, s := range ss {
i, err := strconv.Atoi(s)
if err != nil {
return ii, errors.Wrapf(err, "failed to parse '%v' to int", s)
}
ii = append(ii, i)
}
return ii, nil
}
// Bool gets one value associated with key and returns it as bool
func (p ValueParams) Bool(key string) (bool, error) {
v := p.vals.Get(key)
vb, err := strconv.ParseBool(v)
return vb, errors.Wrapf(err, "failed to parse '%v' to bool", v)
}
// Bools returns all value associated with key as bool slice
func (p ValueParams) Bools(key string) ([]bool, error) {
var bb []bool
ss := p.vals[key]
for _, s := range ss {
b, err := strconv.ParseBool(s)
if err != nil {
return bb, errors.Wrapf(err, "failed to parse '%v' to bool", s)
}
bb = append(bb, b)
}
return bb, nil
}