forked from guregu/kami
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
44 lines (36 loc) · 1.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
package kami
import (
"github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
)
type key int
const (
paramsKey key = iota
panicKey
)
// Param returns a request URL parameter, or a blank string if it doesn't exist.
// For example, with the path /v2/papers/:page
// use kami.Param(ctx, "page") to access the :page variable.
func Param(ctx context.Context, name string) string {
params, ok := ctx.Value(paramsKey).(httprouter.Params)
if !ok {
return ""
}
return params.ByName(name)
}
// Exception gets the "v" in panic(v). The panic details.
// Only PanicHandler will receive a context you can use this with.
func Exception(ctx context.Context) interface{} {
return ctx.Value(panicKey)
}
func newContextWithParams(ctx context.Context, params httprouter.Params) context.Context {
return context.WithValue(ctx, paramsKey, params)
}
func mergeParams(ctx context.Context, params httprouter.Params) context.Context {
current, _ := ctx.Value(paramsKey).(httprouter.Params)
current = append(current, params...)
return context.WithValue(ctx, paramsKey, current)
}
func newContextWithException(ctx context.Context, exception interface{}) context.Context {
return context.WithValue(ctx, panicKey, exception)
}