-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcontext.go
30 lines (24 loc) · 834 Bytes
/
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
package hugot
import "context"
type hugotCtxKey int
//var adapterKey = hugotCtxKey(1)
const (
adapterKey hugotCtxKey = iota
)
// NewAdapterContext creates a context for passing an adapter. This is
// mainly used by web handlers.
func NewAdapterContext(ctx context.Context, a Adapter) context.Context {
return context.WithValue(ctx, adapterKey, a)
}
// AdapterFromContext returns the Adapter stored in a context.
func AdapterFromContext(ctx context.Context) (Adapter, bool) {
a, ok := ctx.Value(adapterKey).(Adapter)
return a, ok
}
// SenderFromContext can be used to retrieve a valid sender from
// a context. This is mostly useful in WebHook handlers for sneding
// messages back to the inbound Adapter.
func SenderFromContext(ctx context.Context) (Sender, bool) {
a, ok := ctx.Value(adapterKey).(Adapter)
return a, ok
}