/
params.go
49 lines (39 loc) · 1.01 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
package httpsrv
import (
"net/http"
"strconv"
"github.com/julienschmidt/httprouter"
uuid "github.com/satori/go.uuid"
)
// ReqValue is a value pulled from the request
type ReqValue string
func (r ReqValue) String() string {
return string(r)
}
// Bool returns r as a bool
func (r ReqValue) Bool() bool {
v, _ := strconv.ParseBool(string(r))
return v
}
// Int returns r as an int
func (r ReqValue) Int() int {
v, _ := strconv.Atoi(string(r))
return v
}
// Float64 returns r as a float64
func (r ReqValue) Float64() float64 {
v, _ := strconv.ParseFloat(string(r), 64)
return v
}
// UUID returns r as a UUID
func (r ReqValue) UUID() uuid.UUID {
return uuid.FromStringOrNil(string(r))
}
// ParamValue returns a ReqValue from the url path
func ParamValue(r *http.Request, key string) ReqValue {
return ReqValue(httprouter.ParamsFromContext(r.Context()).ByName(key))
}
// QueryValue returns a ReqValue from the query string
func QueryValue(r *http.Request, key string) ReqValue {
return ReqValue(r.URL.Query().Get(key))
}