-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
59 lines (47 loc) · 1.28 KB
/
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
54
55
56
57
58
59
package dew
import "context"
var _ Context = (*BusContext)(nil)
// BusContext represents the context for a command execution.
type BusContext struct {
ctx context.Context
// mwsIdx is the index of the middleware chain for method execution.
mwsIdx int
// handler is the wrapped handler function.
handler internalHandler
}
type internalHandler interface {
Handle(ctx Context) error
Command() Command
}
// Command returns the command object to be processed.
func (c *BusContext) Command() Command {
if c.handler == nil {
return nil
}
return c.handler.Command()
}
// WithContext returns a new Context with the given context.
func (c *BusContext) WithContext(ctx context.Context) Context {
c.ctx = ctx
return c
}
func (c *BusContext) Copy(a *BusContext) *BusContext {
c.ctx = a.ctx
c.mwsIdx = a.mwsIdx
c.handler = a.handler
return c
}
func (c *BusContext) Reset() {
c.ctx = nil
c.mwsIdx = 0
c.handler = nil
}
// Context returns the underlying context.Context.
// If no context is set, it returns context.Background().
func (c *BusContext) Context() context.Context {
return c.ctx
}
// WithValue returns a new Context with the given key-value pair added to the context.
func (c *BusContext) WithValue(key, val any) Context {
return c.WithContext(context.WithValue(c.ctx, key, val))
}