-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
157 lines (121 loc) · 3.1 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
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
// Copyright 2022 Sergey Novichkov. All rights reserved.
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
package di
import (
"fmt"
"reflect"
"sync"
"github.com/gozix/di/internal/compiler"
"github.com/gozix/di/internal/runtime"
)
// builder implements the Builder interface.
type builder struct {
defs definitions
mux sync.Mutex
seq int
}
// NewBuilder is builder constructor.
func NewBuilder(options ...BuilderOption) (_ Builder, err error) {
var b = &builder{
defs: definitions{},
}
return b, b.Apply(options...)
}
func (b *builder) Add(value Value, options ...AddOption) (err error) {
var def = &definition{
constraints: constraints{},
}
def.applyAddOptions(options...)
if def.frame == nil {
def.frame = runtime.Caller(0)
}
if def.compiler, err = compiler.NewValue(value); err != nil {
return fmt.Errorf("%s : %w", def.frame, err)
}
return b.add(def)
}
func (b *builder) Apply(options ...BuilderOption) (err error) {
for _, opt := range options {
if err = opt.applyBuilderOption(b); err != nil {
return err
}
}
return nil
}
func (b *builder) Autowire(value Type, options ...ProvideOption) (err error) {
var def = &definition{
constraints: constraints{},
}
def.applyProvideOptions(options...)
if def.frame == nil {
def.frame = runtime.Caller(0)
}
if def.compiler, err = compiler.NewType(value); err != nil {
return fmt.Errorf("%s : %w", def.frame, err)
}
return b.add(def)
}
func (b *builder) Provide(value Constructor, options ...ProvideOption) (err error) {
var def = &definition{
constraints: constraints{},
}
def.applyProvideOptions(options...)
if def.frame == nil {
def.frame = runtime.Caller(0)
}
if def.compiler, err = compiler.NewConstructor(value); err != nil {
return fmt.Errorf("%s : %w", def.frame, err)
}
return b.add(def)
}
func (b *builder) Build() (Container, error) {
b.mux.Lock()
defer b.mux.Unlock()
var defs = definitions{}
for k, v := range b.defs {
defs[k] = v
}
return &container{
defs: defs,
cache: cache{},
}, nil
}
func (b *builder) Definitions() []Definition {
b.mux.Lock()
defer b.mux.Unlock()
var defs = make([]Definition, 0, len(b.defs))
for i := range b.defs {
for j := range b.defs[i] {
var def = b.defs[i][j]
def.definitions = b.defs
defs = append(defs, Definition(&def))
}
}
return defs
}
func (b *builder) add(def *definition) error {
b.mux.Lock()
defer b.mux.Unlock()
b.seq++
def.id = b.seq
var ct = def.compiler.Type()
b.defs[ct] = append(b.defs[ct], *def)
for _, alias := range def.aliases {
if alias == nil {
return fmt.Errorf("%s : %w", def.frame, ErrIsNil)
}
var at = reflect.TypeOf(alias)
if at.Kind() != reflect.Ptr || at.Elem().Kind() != reflect.Interface {
return fmt.Errorf("%s : %w", def.frame, NewTypeError(at, ErrNotPointerToInterface))
}
if ct == at.Elem() {
continue
}
if !ct.Implements(at.Elem()) {
return fmt.Errorf("%s : %s %w %s", def.frame, ct.String(), ErrNotImplementInterface, at.String())
}
b.defs[at.Elem()] = append(b.defs[at.Elem()], *def)
}
return nil
}