-
Notifications
You must be signed in to change notification settings - Fork 0
/
url.go
97 lines (82 loc) · 2.59 KB
/
url.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
package tea
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/go-chi/chi"
)
// ErrNoURLParam means you tried to get a URL param that does not exist
var ErrNoURLParam = fmt.Errorf("url param does not exist")
// URLInt gets a URL parameter from the request and parses it as an int. If
// the value of the URL parameter is empty then ErrNoURLParam is returned.
// If parsing passes, then the validation key will be used to validate the
// value of the int using Validate
func URLInt(r *http.Request, key, validation string) (int, error) {
p := strings.TrimSpace(chi.URLParam(r, key))
if len(p) == 0 {
return 0, ErrNoURLParam
}
i, err := strconv.Atoi(p)
if err != nil {
return 0, err
}
if len(validation) == 0 {
return i, nil
}
return i, Validate.VarCtx(r.Context(), &i, validation)
}
// URLInt64 gets a URL parameter from the request and parses it as an int64. If
// the value of the URL parameter is empty then ErrNoURLParam is returned.
// If parsing passes, then the validation key will be used to validate the
// value of the int64 using Validate
func URLInt64(r *http.Request, key, validation string) (int64, error) {
p := strings.TrimSpace(chi.URLParam(r, key))
if len(p) == 0 {
return 0, ErrNoURLParam
}
i, err := strconv.ParseInt(p, 0, 64)
if err != nil {
return 0, err
}
if len(validation) == 0 {
return i, nil
}
return i, Validate.VarCtx(r.Context(), &i, validation)
}
// URLFloat gets a URL parameter from the request and parses it as an float. If
// the value of the URL parameter is empty then ErrNoURLParam is returned.
// If parsing passes, then the validation key will be used to validate the
// value of the float using Validate
func URLFloat(r *http.Request, key, validation string) (float64, error) {
p := strings.TrimSpace(chi.URLParam(r, key))
if len(p) == 0 {
return 0, ErrNoURLParam
}
i, err := strconv.ParseFloat(p, 64)
if err != nil {
return 0, err
}
if len(validation) == 0 {
return i, nil
}
return i, Validate.VarCtx(r.Context(), &i, validation)
}
// URLUint gets a URL parameter from the request and parses it as an uint64. If
// the value of the URL parameter is empty then ErrNoURLParam is returned.
// If parsing passes, then the validation key will be used to validate the
// value of the uint64 using Validate
func URLUint(r *http.Request, key, validation string) (uint64, error) {
p := strings.TrimSpace(chi.URLParam(r, key))
if len(p) == 0 {
return 0, ErrNoURLParam
}
i, err := strconv.ParseUint(p, 0, 64)
if err != nil {
return 0, err
}
if len(validation) == 0 {
return i, nil
}
return i, Validate.VarCtx(r.Context(), &i, validation)
}