-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
66 lines (49 loc) · 1.25 KB
/
builder.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
package dimple
import (
"context"
)
var _ ContainerBuilder = (*DefaultBuilder)(nil)
// Builder returns a new ContainerBuilder instance
func Builder(defs ...Definition) *DefaultBuilder {
b := &DefaultBuilder{
container: &DefaultContainer{
order: make([]string, 0),
definitions: make(map[string]Definition),
},
}
b.Add(Service("container", WithInstance(b.container)))
for _, def := range defs {
b.Add(def)
}
return b
}
type DefaultBuilder struct {
container *DefaultContainer
}
func (b *DefaultBuilder) MustBuild(ctx context.Context) *DefaultContainer {
c, err := b.Build(ctx)
if err != nil {
panic(err)
}
return c
}
func (b *DefaultBuilder) Build(ctx context.Context) (*DefaultContainer, error) {
c := b.container
c.ctx = ctx
b.Add(Service("context", WithInstance(ctx)))
// mandatory boot of decorated services to rewrite the decorated definitions
if err := c.boot(c.getAllDecoratorIDs()...); err != nil {
return nil, err
}
return c, nil
}
func (b *DefaultBuilder) Add(def Definition) ContainerBuilder {
b.container.add(def.Id(), def)
return b
}
func (b *DefaultBuilder) Get(id string) Definition {
return b.container.getDefinition(id)
}
func (b *DefaultBuilder) Has(id string) bool {
return b.container.Has(id)
}