-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
32 lines (27 loc) · 985 Bytes
/
context.go
File metadata and controls
32 lines (27 loc) · 985 Bytes
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
package mohttp
import (
"github.com/jonasi/mohttp/contextutil"
"golang.org/x/net/context"
)
// ContextValueAccessors generates a pair of funcs to get and set values on the context. The first
// func returns a Handler that will set the value in a middleware chain. The second func can be used
// to retrieve that value from a handler body.
func ContextValueAccessors(str string) (func(interface{}) PriorityHandler, func(context.Context) interface{}) {
st := NewContextValueStore(str)
return func(val interface{}) PriorityHandler {
return PriorityHandlerFunc(-100, func(c context.Context) {
c = st.Set(c, val)
Next(c)
})
}, st.Get
}
// NewContextValueStore is a small wrapper around contextutil.ValueStore that turns the provided string
// into a unique key
func NewContextValueStore(key string) contextutil.ValueStore {
return &contextValueStore{
contextutil.NewValueStore(contextutil.NewKey(key)),
}
}
type contextValueStore struct {
contextutil.ValueStore
}