generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
167 lines (129 loc) · 3.25 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package imbue
import (
"context"
"fmt"
"reflect"
"sort"
"sync"
"github.com/xlab/treeprint"
"golang.org/x/sync/errgroup"
)
// ContainerAware is an interface for types that can operate on a container.
type ContainerAware interface {
withContainer(func(*Container))
}
// Container is a dependency injection container.
type Container struct {
m sync.Mutex
declarations map[reflect.Type]declaration
defers deferSet
}
// ContainerOption is an option that changes the behavior of a container or how
// it is constructed.
type ContainerOption interface {
applyContainerOption(*Container)
}
// New returns a new, empty container.
func New(options ...ContainerOption) *Container {
con := &Container{
declarations: map[reflect.Type]declaration{},
}
for _, opt := range options {
opt.applyContainerOption(con)
}
return con
}
// WaitGroup returns a new WaitGroup that is bound to this container.
func (c *Container) WaitGroup(ctx context.Context) *WaitGroup {
g, ctx := errgroup.WithContext(ctx)
return &WaitGroup{
con: c,
ctx: ctx,
group: g,
}
}
// Close closes the container, calling any deferred functions registered
// during construction of dependencies.
func (c *Container) Close() error {
c.m.Lock()
defer c.m.Unlock()
if errors := c.defers.Call(); len(errors) != 0 {
return closeError(errors)
}
return nil
}
func (c *Container) withContainer(fn func(*Container)) {
fn(c)
}
// typeOf returns the reflect.Type for T.
func typeOf[T any]() reflect.Type {
return reflect.TypeOf([0]T{}).Elem()
}
// get returns the declaration for type T.
func get[T any](con *Container) *declarationOf[T] {
t := typeOf[T]()
con.m.Lock()
if d, ok := con.declarations[t]; ok {
con.m.Unlock()
return d.(*declarationOf[T])
}
d := &declarationOf[T]{
defers: &con.defers,
}
con.declarations[t] = d
con.m.Unlock()
d.Init(con)
return d
}
// String returns a string representation of the dependency tree.
func (c *Container) String() string {
c.m.Lock()
declarations := sortDeclarations(c.declarations)
c.m.Unlock()
tree := treeprint.New()
tree.SetValue("<container>")
for _, d := range declarations {
if !d.IsDependency() {
buildTree(tree, d)
}
}
return tree.String()
}
// buildTree builds the tree of dependencies for the given declaration.
func buildTree(t treeprint.Tree, d declaration) {
dependencies := d.Dependencies()
if len(dependencies) == 0 {
t.AddNode(d.Type().String())
return
}
sub := t.AddBranch(d.Type().String())
for _, dep := range dependencies {
buildTree(sub, dep)
}
}
// sortDeclarations returns the given declarations sorted by type.
func sortDeclarations(declarations map[reflect.Type]declaration) []declaration {
sorted := make([]declaration, 0, len(declarations))
for _, d := range declarations {
sorted = append(sorted, d)
}
sort.Slice(
sorted,
func(i, j int) bool {
return sorted[i].Type().String() < sorted[j].Type().String()
},
)
return sorted
}
// closeError is returned when there are one or more errors closing a container.
type closeError []error
func (e closeError) Error() string {
message := fmt.Sprintf(
"%d error(s) occurred while closing the container:",
len(e),
)
for i, err := range e {
message += fmt.Sprintf("\n\t%d) %s", i+1, err)
}
return message
}