-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
53 lines (42 loc) · 988 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
41
42
43
44
45
46
47
48
49
50
51
52
53
package bee
import (
"context"
"encoding/json"
"github.com/bee-org/bee/message"
"time"
)
type Context struct {
ctx context.Context
msg message.Message
req interface{}
}
func NewCtx(ctx context.Context, m message.Message) *Context {
return &Context{ctx: ctx, msg: m}
}
func (c *Context) Name() string {
return c.msg.GetName()
}
func (c *Context) Parse(v interface{}) error {
// Recording request parameters
defer func() { c.req = v }()
return json.Unmarshal(c.msg.GetBody(), v)
}
// Req must be called after the Parse, Return the req recorded at parsing time.
func (c *Context) Req() interface{} {
return c.req
}
func (c *Context) Message() message.Message {
return c.msg
}
func (c *Context) Deadline() (deadline time.Time, ok bool) {
return c.ctx.Deadline()
}
func (c *Context) Done() <-chan struct{} {
return c.ctx.Done()
}
func (c *Context) Err() error {
return c.ctx.Err()
}
func (c *Context) Value(key interface{}) interface{} {
return c.ctx.Value(key)
}