-
Notifications
You must be signed in to change notification settings - Fork 1
/
contexts.go
59 lines (48 loc) · 995 Bytes
/
contexts.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 types
import (
"context"
"errors"
"github.com/momentum-xyz/ubercontroller/config"
"go.uber.org/zap"
)
// Typed contex.Context handling
const (
loggerContextKey = "logger"
configContextKey = "config"
)
type LoggerContext interface {
context.Context
Logger() *zap.SugaredLogger
}
type ConfigContext interface {
context.Context
Config() *config.Config
}
type NodeContext interface {
LoggerContext
ConfigContext
}
type nodeContext struct {
context.Context
log *zap.SugaredLogger
cfg *config.Config
}
func (n nodeContext) Logger() *zap.SugaredLogger {
return n.log
}
func (n nodeContext) Config() *config.Config {
return n.cfg
}
func NewNodeContext(ctx context.Context, log *zap.SugaredLogger, cfg *config.Config) (NodeContext, error) {
if log == nil {
return nil, errors.New("NodeContext: log required")
}
if cfg == nil {
return nil, errors.New("NodeContext: cfg required")
}
return nodeContext{
Context: ctx,
log: log,
cfg: cfg,
}, nil
}