-
Notifications
You must be signed in to change notification settings - Fork 27
/
context.go
72 lines (58 loc) · 1.97 KB
/
context.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
package bokchoy
import (
"context"
)
type contextKey struct {
name string
}
// AfterRequestFunc is a function which will execute after the request
type AfterRequestFunc func()
func (k *contextKey) String() string {
return "bokchoy context value " + k.name
}
var (
// ErrorCtxKey is the context.Context key to store
// the recovered error from the middleware
ErrorCtxKey = &contextKey{"Error"}
// TaskCtxKey is the context.Context key to store
// the task from the middleware
TaskCtxKey = &contextKey{"Task"}
// AfterRequestCtxKey is the context.Context to store
// functions to execute after the request
AfterRequestCtxKey = &contextKey{"AfterRequest"}
)
// WithContextTask sets the in-context task for a request.
func WithContextTask(ctx context.Context, task *Task) context.Context {
ctx = context.WithValue(ctx, TaskCtxKey, task)
return ctx
}
// GetContextTask returns the in-context task for a request.
func GetContextTask(ctx context.Context) *Task {
err, _ := ctx.Value(TaskCtxKey).(*Task)
return err
}
// WithContextError sets the in-context error for a request.
func WithContextError(ctx context.Context, err error) context.Context {
ctx = context.WithValue(ctx, ErrorCtxKey, err)
return ctx
}
// GetContextError returns the in-context recovered error for a request.
func GetContextError(ctx context.Context) error {
err, _ := ctx.Value(ErrorCtxKey).(error)
return err
}
// GetContextAfterRequestFuncs returns the registered functions
// which will execute after the request
func GetContextAfterRequestFuncs(ctx context.Context) []AfterRequestFunc {
funcs, _ := ctx.Value(AfterRequestCtxKey).([]AfterRequestFunc)
if funcs == nil {
return []AfterRequestFunc{}
}
return funcs
}
// WithContextAfterRequestFunc registers a new function to be executed
// after the request
func WithContextAfterRequestFunc(ctx context.Context, f AfterRequestFunc) context.Context {
funcs := append(GetContextAfterRequestFuncs(ctx), f)
return context.WithValue(ctx, AfterRequestCtxKey, funcs)
}