-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
85 lines (69 loc) · 2.02 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package di
import (
"errors"
"github.com/michalkurzeja/godi/v2/di"
)
// New creates a new Builder.
// This is the recommended entrypoint to the godi library.
func New() *Builder {
return &Builder{cb: di.NewContainerBuilder()}
}
// Builder is a helper for building a container.
// It offers a fluent interface that incorporates other helpers to make
// the process of setting up the container easy and convenient for the user.
// This is the recommended way of building a container.
type Builder struct {
cb *di.ContainerBuilder
services []*ServiceDefinitionBuilder
functions []*FunctionDefinitionBuilder
bindings []*InterfaceBindingBuilder
passes []*di.CompilerPass
}
func (b *Builder) Services(services ...*ServiceDefinitionBuilder) *Builder {
b.services = append(b.services, services...)
return b
}
func (b *Builder) Functions(functions ...*FunctionDefinitionBuilder) *Builder {
b.functions = append(b.functions, functions...)
return b
}
func (b *Builder) Bindings(bindings ...*InterfaceBindingBuilder) *Builder {
b.bindings = append(b.bindings, bindings...)
return b
}
func (b *Builder) CompilerPasses(passes ...*di.CompilerPass) *Builder {
b.passes = append(b.passes, passes...)
return b
}
func (b *Builder) Build() (Container, error) {
var joinedErr error
for _, builder := range b.services {
if err := builder.parseFactory(); err != nil {
joinedErr = errors.Join(joinedErr, err)
continue
}
}
for _, builder := range b.services {
if err := builder.build(b.cb.RootScope()); err != nil {
joinedErr = errors.Join(joinedErr, err)
continue
}
}
for _, builder := range b.functions {
if err := builder.build(b.cb.RootScope()); err != nil {
joinedErr = errors.Join(joinedErr, err)
continue
}
}
for _, builder := range b.bindings {
if err := builder.Build(b.cb.RootScope()); err != nil {
joinedErr = errors.Join(joinedErr, err)
continue
}
}
for _, pass := range b.passes {
b.cb.Compiler().AddPass(pass)
}
container, err := b.cb.Build()
return container, errors.Join(joinedErr, err)
}