-
Notifications
You must be signed in to change notification settings - Fork 7
/
context.go
40 lines (33 loc) · 849 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 routine
import (
"context"
)
type _argments struct{}
//WithArgments inject into context
func WithArgments(ctx context.Context, args ...interface{}) context.Context {
if ctx != nil {
return context.WithValue(ctx, _argments{}, args)
}
return nil
}
//ArgumentsFrom extract from context
func ArgumentsFrom(ctx context.Context) ([]interface{}, bool) {
if ctx != nil {
argments := ctx.Value(_argments{})
if argments != nil {
return argments.([]interface{}), true
}
}
return nil, false
}
type _parent_routine struct{}
func WithParent(ctx context.Context, parent *Routine) context.Context {
return context.WithValue(ctx, _parent_routine{}, parent)
}
func ParentFrom(ctx context.Context) (*Routine, bool) {
parent := ctx.Value(_parent_routine{})
if parent != nil {
return parent.(*Routine), true
}
return nil, false
}