-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject_struct.go
More file actions
198 lines (176 loc) · 4.63 KB
/
inject_struct.go
File metadata and controls
198 lines (176 loc) · 4.63 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
package dscope
import (
"errors"
"fmt"
"reflect"
"sync"
)
type InjectStruct func(target any)
var injectStructTypeID = getTypeID(reflect.TypeFor[InjectStruct]())
func (scope Scope) InjectStruct(target any) {
injectStruct(scope, target, 0)
}
type _InjectStructFunc = func(scope Scope, value reflect.Value, depth int)
// reflect.Type -> _InjectStructFunc
var injectStructFuncs sync.Map
func injectStruct(scope Scope, target any, depth int) {
v := reflect.ValueOf(target)
if !v.IsValid() {
panic(errors.Join(
fmt.Errorf("target must be a pointer to a struct, got nil"),
ErrBadArgument,
))
}
if v.Kind() != reflect.Pointer {
panic(errors.Join(
fmt.Errorf("target must be a pointer to a struct, got %v", v.Type()),
ErrBadArgument,
))
}
targetType := v.Type()
if fn, ok := injectStructFuncs.Load(targetType); ok {
fn.(_InjectStructFunc)(scope, v, depth)
return
}
injectFunc := makeInjectStructFunc(targetType)
fn, _ := injectStructFuncs.LoadOrStore(targetType, injectFunc)
fn.(_InjectStructFunc)(scope, v, depth)
}
func makeInjectStructFunc(t reflect.Type) _InjectStructFunc {
numDeref := 0
l:
for {
if numDeref > 100 {
panic(errors.Join(
fmt.Errorf("too many dereferences or recursive pointer type %v", t),
ErrBadArgument,
))
}
switch t.Kind() {
case reflect.Pointer:
numDeref++
// This may causes stack overflow for recursive pointer types.
// Fixing this will introduce extra cost for common cases.
// So we choose not to handle recursive pointer types, just let it crash.
t = t.Elem()
case reflect.Struct:
break l
default:
panic(errors.Join(
fmt.Errorf("target type %v is not a struct or pointer to struct", t),
ErrBadArgument,
))
}
}
type FieldInfo struct {
Field reflect.StructField
IsInject bool
IsEmbedded bool
Type reflect.Type
}
var infos []FieldInfo
for i := range t.NumField() {
field := t.Field(i)
if field.PkgPath != "" {
// un-exported field
continue
}
directive := field.Tag.Get("dscope")
if field.Type.Kind() == reflect.Func && field.Type.Implements(isInjectType) {
// Only treat as Inject[T] if it is a function.
// Pointers to Inject[T] also implement the interface but cannot be
// processed by reflect.MakeFunc or Out(0).
infos = append(infos, FieldInfo{
Field: field,
IsInject: true,
Type: field.Type.Out(0),
})
} else if directive == "." || directive == "inject" {
infos = append(infos, FieldInfo{
Field: field,
Type: field.Type,
})
} else if field.Anonymous {
fieldType := field.Type
if fieldType.Kind() == reflect.Pointer {
if fieldType.Elem().Kind() != reflect.Struct {
continue
}
} else if fieldType.Kind() != reflect.Struct {
continue
}
infos = append(infos, FieldInfo{
Field: field,
IsEmbedded: true,
Type: field.Type,
})
}
}
return func(scope Scope, value reflect.Value, depth int) {
if depth > 64 {
panic(errors.Join(
fmt.Errorf("recursive struct injection depth limit exceeded"),
ErrBadArgument,
))
}
// Check if the target pointer is nil before dereferencing
if value.Kind() == reflect.Pointer && value.IsNil() {
panic(errors.Join(
fmt.Errorf("cannot inject into a nil pointer target of type %v", value.Type()),
ErrBadArgument,
))
}
for range numDeref {
if value.IsNil() {
if value.CanSet() {
value.Set(reflect.New(value.Type().Elem()))
} else {
panic(errors.Join(
fmt.Errorf("cannot inject into a nil pointer target of type %v", value.Type()),
ErrBadArgument,
))
}
}
value = value.Elem()
}
for _, info := range infos {
info := info
if info.IsInject {
value.FieldByIndex(info.Field.Index).Set(
reflect.MakeFunc(
info.Field.Type,
func(_ []reflect.Value) []reflect.Value {
value, ok := scope.Get(info.Type)
if !ok {
throwErrDependencyNotFound(info.Type)
}
return []reflect.Value{value}
},
),
)
} else if info.IsEmbedded {
fieldValue := value.FieldByIndex(info.Field.Index)
if fieldValue.Type().Kind() == reflect.Pointer {
if fieldValue.IsNil() {
if fieldValue.CanSet() {
fieldValue.Set(reflect.New(fieldValue.Type().Elem()))
} else {
continue
}
}
injectStruct(scope, fieldValue.Interface(), depth+1)
} else { // Embedded by value
if fieldValue.CanAddr() {
injectStruct(scope, fieldValue.Addr().Interface(), depth+1)
}
}
} else {
v, ok := scope.Get(info.Type)
if !ok {
throwErrDependencyNotFound(info.Type)
}
value.FieldByIndex(info.Field.Index).Set(v)
}
}
}
}