-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_params.go
39 lines (29 loc) · 887 Bytes
/
route_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
package badger
import (
"net/http"
"github.com/julienschmidt/httprouter"
)
type routeParamsKey struct{}
// RouteParamsKey is the key to find route params in context
var RouteParamsKey = routeParamsKey{}
// RouteParams are the params found in route named parameters
// they are a TypedParams type, which allows to retrieve typed info
type RouteParams struct {
params TypedParams
}
// CreateRouteParams converts httprouter.Params to TypedParams
func CreateRouteParams(rps httprouter.Params) TypedParams {
dict := make(map[string]string)
for _, rp := range rps {
if rp.Key == "" {
continue
}
dict[rp.Key] = rp.Value
}
return CreateTypedParams(dict)
}
// GetRouteParamsFromRequest retrieves Typed Route params from given request
func GetRouteParamsFromRequest(req *http.Request) TypedParams {
ctx := req.Context()
return ctx.Value(RouteParamsKey).(TypedParams)
}