-
Notifications
You must be signed in to change notification settings - Fork 0
/
di.go
183 lines (148 loc) · 5.62 KB
/
di.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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 (
"errors"
"reflect"
"github.com/gozix/di/internal/compiler"
)
type (
// Builder must be used to create a Container. The Builder should be created with NewBuilder.
// Then you can provide any definition with various allowed methods
// and finally build the Container with the Build method.
Builder interface {
// Add provides value as is.
//
// The value argument must contain any value what you want, fre example:
// - new(http.Server)
// - *http.Server{}
// - etc.
// The options argument may be one of:
// - di.Tags{}
// - di.As()
Add(value Value, options ...AddOption) error
// Apply applies options to Builder.
// The options argument may be one of:
// - di.BuilderOptions()
// - di.Add()
// - di.Autowire()
// - di.Provide()
Apply(options ...BuilderOption) error
// Autowire providers autowired type.
//
// The target argument must contain the wanted type, for example:
// - (*http.Server)(nil)
// - (*io.Writer)(nil)
// - new(io.Writer)
// - etc.
// The options argument may be one of:
// - di.As()
// - di.Constraint()
// - di.Tags{}
// - di.Unshared()
Autowire(target Type, options ...ProvideOption) error
// Provide provides any constructor.
//
// The constructor argument must be a function with one of the following signatures:
// - func New(constraints ...any) (value any)
// - func New(constraints ...any) (value any, err error)
// - func New(constraints ...any) (value any, closer func(){})
// - func New(constraints ...any) (value any, closer func(){}, err error)
// The options argument may be one of:
// - di.As()
// - di.Constraint()
// - di.Tags{}
// - di.Unshared()
Provide(constructor Constructor, options ...ProvideOption) error
// Build is container build method.
Build() (Container, error)
// Definitions are build snapshot of definitions.
Definitions() []Definition
}
// Constructor is any constructor.
Constructor any
// Container represents a dependency injection container.
// To create a container, you should use a builder.
Container interface {
// Call calls the function with resolved arguments.
//
// The fn argument must contain any function. If the function contains error in the last return type,
// then Call will return that value as own return type value.
// The options argument may be one of:
// - di.Constraint()
Call(fn Function, options ...ConstraintOption) (err error)
// Close runs closers in reverse order that has been created.
//
// Any close function can return any error that stop the calling loop for all rest closers. Any close function
// can return any error that stop the calling loop for all rest closers. That error will return in function
// return type.
Close() error
// Has checks that type exists in container, if not it return false.
//
// The value argument must contain the wanted type, for example:
// - (*http.Server)(nil)
// - (*io.Writer)(nil)
// - new(io.Writer)
// - etc.
// The modifiers argument may be one of:
// - di.WithTags()
Has(value Type, modifiers ...Modifier) (exist bool)
// Resolve resolves type and fills target pointer.
//
// The target argument must contain reference to wanted variable.
// The modifiers argument may be one of:
// - di.WithTags()
Resolve(target Value, modifiers ...Modifier) (err error)
}
// Definition represent container definition.
Definition interface {
// ID is definition unique identificator getter.
ID() int
// Dependencies is definition type dependencies getter.
Dependencies() []Dependency
// Tags is definition tags getter.
Tags() Tags
// Type is definition type getter.
Type() reflect.Type
// Unshared is definition unshared getter.
Unshared() bool
}
// Dependency represent definition dependency.
Dependency struct {
// Type is type of dependency.
Type reflect.Type
// Optional is optional flag.
Optional bool
// Definitions are list of matched definitions.
Definitions []Definition
}
// Function is any function.
Function any
// Type is any type.
Type any
// Value is any value.
Value any
)
var (
// ErrNotPointerToInterface is error triggered when provided alias not pointer to interface.
ErrNotPointerToInterface = errors.New("not pointer to interface")
// ErrIsNil is error triggered when provided nil alias.
ErrIsNil = errors.New("is nil")
// ErrNotImplementInterface is error triggered when provided type not implement alias interface.
ErrNotImplementInterface = errors.New("not implement interface")
// ErrDoesNotExist triggered when type not present in container.
ErrDoesNotExist = errors.New("does not exist")
// ErrMustBeSliceOrPointer triggered when resolved in invalid target.
ErrMustBeSliceOrPointer = errors.New("must be a slice or pointer")
// ErrMultipleDefinitions triggered when type resolved in single instance, but container contain multiple types.
ErrMultipleDefinitions = errors.New("multiple definitions")
// ErrorMustBeFunction triggered when value not a function.
ErrorMustBeFunction = errors.New("must be a function")
// ErrInvalidConstructor is error triggered when constructor have invalid signature.
ErrInvalidConstructor = compiler.ErrInvalidConstructor
// ErrInvalidType is error triggered when provided invalid type.
ErrInvalidType = compiler.ErrInvalidType
// ErrInvalidValue is error triggered when provided invalid value.
ErrInvalidValue = compiler.ErrInvalidValue
)