-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathcontext_wrapper.go
More file actions
35 lines (28 loc) · 710 Bytes
/
context_wrapper.go
File metadata and controls
35 lines (28 loc) · 710 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
33
34
35
package rel
import (
"context"
)
type contextKey int8
type contextWrapper struct {
ctx context.Context
adapter Adapter
}
var ctxKey contextKey
// fetchContext and use adapter passed by context if exists.
// it stores contextData values to struct for fast repeated access.
func fetchContext(ctx context.Context, adapter Adapter) contextWrapper {
if adp, ok := ctx.Value(ctxKey).(Adapter); ok {
adapter = adp
}
return contextWrapper{
ctx: ctx,
adapter: adapter,
}
}
// wrapContext wraps adapter inside context.
func wrapContext(ctx context.Context, adapter Adapter) contextWrapper {
return contextWrapper{
ctx: context.WithValue(ctx, ctxKey, adapter),
adapter: adapter,
}
}