-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructor.go
More file actions
322 lines (272 loc) · 7.42 KB
/
constructor.go
File metadata and controls
322 lines (272 loc) · 7.42 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package vessel
import (
"errors"
"fmt"
"reflect"
"strings"
)
// In is a marker type that should be embedded in structs to indicate
// they are parameter objects. Fields of the struct will be treated as
// dependencies to inject. This follows the dig pattern for constructor injection.
//
// Example:
//
// type ServiceParams struct {
// vessel.In
//
// DB *Database
// Logger *Logger `optional:"true"`
// Cache *Cache `name:"redis"`
// Handlers []http.Handler `group:"http"`
// }
type In struct{}
// Out is a marker type that should be embedded in structs to indicate
// they are result objects. Each field of the struct will be registered
// as a separate service. This follows the dig pattern for multi-value returns.
//
// Example:
//
// type ServiceResult struct {
// vessel.Out
//
// UserService *UserService
// ProductService *ProductService `name:"products"`
// Handler http.Handler `group:"http"`
// }
type Out struct{}
var (
inType = reflect.TypeOf(In{})
outType = reflect.TypeOf(Out{})
errorType = reflect.TypeOf((*error)(nil)).Elem()
)
// constructorInfo holds analyzed constructor metadata
type constructorInfo struct {
fn reflect.Value
fnType reflect.Type
params []paramInfo
results []resultInfo
hasError bool
}
// paramInfo describes a constructor parameter
type paramInfo struct {
typ reflect.Type
name string // From `name:"..."` tag, empty for type-based lookup
optional bool // From `optional:"true"` tag
group bool // From `group:"..."` tag - expects slice type
groupKey string // The group name for collection
index int // Position in function parameters or struct field index
isIn bool // Whether this is an In struct (expanded into multiple deps)
inFields []paramInfo // Expanded fields if isIn is true
}
// resultInfo describes a constructor result
type resultInfo struct {
typ reflect.Type
name string // From `name:"..."` tag
group string // From `group:"..."` tag
index int // Position in function results or struct field index
fieldName string // The actual struct field name (for Out structs)
isOut bool // Whether this is an Out struct (expanded into multiple results)
outFields []resultInfo // Expanded fields if isOut is true
}
// analyzeConstructor inspects a constructor function and extracts its dependency
// and result information for automatic resolution.
func analyzeConstructor(constructor any) (*constructorInfo, error) {
fnValue := reflect.ValueOf(constructor)
fnType := fnValue.Type()
if fnType.Kind() != reflect.Func {
return nil, errors.New("constructor must be a function")
}
info := &constructorInfo{
fn: fnValue,
fnType: fnType,
}
// Analyze parameters
for i := 0; i < fnType.NumIn(); i++ {
paramType := fnType.In(i)
param, err := analyzeParam(paramType, i)
if err != nil {
return nil, fmt.Errorf("parameter %d: %w", i, err)
}
info.params = append(info.params, param)
}
// Analyze results
for i := 0; i < fnType.NumOut(); i++ {
resultType := fnType.Out(i)
// Check for error return (must be last)
if resultType.Implements(errorType) {
if i != fnType.NumOut()-1 {
return nil, errors.New("error must be the last return value")
}
info.hasError = true
continue
}
result, err := analyzeResult(resultType, i)
if err != nil {
return nil, fmt.Errorf("result %d: %w", i, err)
}
info.results = append(info.results, result)
}
if len(info.results) == 0 {
return nil, errors.New("constructor must return at least one non-error value")
}
return info, nil
}
// analyzeParam analyzes a single parameter type
func analyzeParam(t reflect.Type, index int) (paramInfo, error) {
param := paramInfo{
typ: t,
index: index,
}
// Check if it's an In struct
if isInStruct(t) {
param.isIn = true
fields, err := expandInStruct(t)
if err != nil {
return param, err
}
param.inFields = fields
}
return param, nil
}
// analyzeResult analyzes a single result type
func analyzeResult(t reflect.Type, index int) (resultInfo, error) {
result := resultInfo{
typ: t,
index: index,
}
// Check if it's an Out struct
if isOutStruct(t) {
result.isOut = true
fields, err := expandOutStruct(t)
if err != nil {
return result, err
}
result.outFields = fields
}
return result, nil
}
// isInStruct checks if a type embeds vessel.In
func isInStruct(t reflect.Type) bool {
// Handle pointer types
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return false
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Anonymous && field.Type == inType {
return true
}
// Check embedded structs recursively
if field.Anonymous && isInStruct(field.Type) {
return true
}
}
return false
}
// isOutStruct checks if a type embeds vessel.Out
func isOutStruct(t reflect.Type) bool {
// Handle pointer types
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return false
}
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if field.Anonymous && field.Type == outType {
return true
}
// Check embedded structs recursively
if field.Anonymous && isOutStruct(field.Type) {
return true
}
}
return false
}
// expandInStruct expands an In struct into its field dependencies
func expandInStruct(t reflect.Type) ([]paramInfo, error) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
var params []paramInfo
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// Skip the embedded In marker
if field.Anonymous && (field.Type == inType || isInStruct(field.Type)) {
continue
}
// Skip unexported fields
if !field.IsExported() {
continue
}
param := paramInfo{
typ: field.Type,
index: i,
}
// Parse struct tags
if tag := field.Tag.Get("name"); tag != "" {
param.name = tag
}
if tag := field.Tag.Get("optional"); strings.ToLower(tag) == "true" {
param.optional = true
}
if tag := field.Tag.Get("group"); tag != "" {
param.group = true
param.groupKey = tag
// Verify it's a slice type for group injection
if field.Type.Kind() != reflect.Slice {
return nil, fmt.Errorf("field %s with group tag must be a slice type", field.Name)
}
}
params = append(params, param)
}
return params, nil
}
// expandOutStruct expands an Out struct into its result fields
func expandOutStruct(t reflect.Type) ([]resultInfo, error) {
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
var results []resultInfo
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// Skip the embedded Out marker
if field.Anonymous && (field.Type == outType || isOutStruct(field.Type)) {
continue
}
// Skip unexported fields
if !field.IsExported() {
continue
}
result := resultInfo{
typ: field.Type,
index: i,
fieldName: field.Name, // Capture the field name for extraction
}
// Parse struct tags
if tag := field.Tag.Get("name"); tag != "" {
result.name = tag
}
if tag := field.Tag.Get("group"); tag != "" {
result.group = tag
}
results = append(results, result)
}
return results, nil
}
// flattenResults returns all results including expanded Out struct fields
func (c *constructorInfo) flattenResults() []resultInfo {
var flat []resultInfo
for _, r := range c.results {
if r.isOut {
flat = append(flat, r.outFields...)
} else {
flat = append(flat, r)
}
}
return flat
}