-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontainer.go
88 lines (71 loc) · 2.13 KB
/
container.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package resozyme
import (
"context"
"errors"
)
// ActiveResourceContainerContextKey is a context key.
type ActiveResourceContainerContextKey struct{}
// NewResourceContainer creates a new ResourceContainer.
func NewResourceContainer() *ResourceContainer {
return &ResourceContainer{}
}
// WithController sets a controller to the context.
func WithController(ctx context.Context, rcont Controller) context.Context {
return context.WithValue(ctx, ControllerContextKey{}, rcont)
}
// GetController gets a controller from the context.
func GetController(ctx context.Context) Controller {
v := ctx.Value(ControllerContextKey{})
if v == nil {
return nil
}
return v.(Controller)
}
// WithActiveResourceContainer sets a resource container.
func WithActiveResourceContainer(ctx context.Context, rcont *ResourceContainer) context.Context {
return context.WithValue(ctx, ActiveResourceContainerContextKey{}, rcont)
}
// GetActiveResourceContainer gets a resource container.
func GetActiveResourceContainer(ctx context.Context) *ResourceContainer {
v := ctx.Value(ActiveResourceContainerContextKey{})
if v == nil {
return nil
}
return v.(*ResourceContainer)
}
// ActivateResource sets a resource to ActiveResourceContainer.
func ActivateResource(ctx context.Context, resc Resource) error {
rcont := GetActiveResourceContainer(ctx)
if rcont == nil {
return errors.New("no resource container activated")
}
rcont.Set(resc)
return nil
}
// GetActiveResource gets the activated resource from ActiveResourceContainer.
func GetActiveResource(ctx context.Context) Resource {
rcont := GetActiveResourceContainer(ctx)
if rcont == nil {
return nil
}
if !rcont.Exists() {
return nil
}
return rcont.Get()
}
// ResourceContainer is a container.
type ResourceContainer struct {
resc Resource
}
// Exists checks whether the container has a resource.
func (rcont *ResourceContainer) Exists() bool {
return rcont.resc != nil
}
// Set sets a resource to the container.
func (rcont *ResourceContainer) Set(resc Resource) {
rcont.resc = resc
}
// Get gets a resource from the container.
func (rcont *ResourceContainer) Get() Resource {
return rcont.resc
}