-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
40 lines (29 loc) · 822 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
31
32
33
34
35
36
37
38
39
40
package conveyor
import (
"context"
"fmt"
"github.com/jpoz/conveyor/wire"
)
type ContextKey struct{ Name string }
var JobContextKey = &ContextKey{"job"}
func JobContext(ctx context.Context, obj *wire.Job) context.Context {
return context.WithValue(ctx, JobContextKey, obj)
}
func CurrentJob(ctx context.Context) *wire.Job {
obj, ok := ctx.Value(JobContextKey).(*wire.Job)
if !ok {
panic(fmt.Errorf("job not found in context"))
}
return obj
}
var ClientContextKey = &ContextKey{"client"}
func AddClientToContext(ctx context.Context, obj *Client) context.Context {
return context.WithValue(ctx, ClientContextKey, obj)
}
func CurrentClient(ctx context.Context) *Client {
obj, ok := ctx.Value(ClientContextKey).(*Client)
if !ok {
panic(fmt.Errorf("hub client not found in context"))
}
return obj
}