-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdscope.go
More file actions
287 lines (252 loc) · 8.34 KB
/
dscope.go
File metadata and controls
287 lines (252 loc) · 8.34 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
package dscope
import (
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"reflect"
"sync"
)
type _Value struct {
typeInfo *_TypeInfo
initializer *_Initializer
}
type _TypeInfo struct {
DefType reflect.Type
TypeID _TypeID
Position int
Dependencies []_TypeID
}
// _TypeID is a unique identifier for a reflect.Type.
type _TypeID int
// _Hash is used for scope signatures and cache keys.
type _Hash [sha256.Size]byte
// Scope represents an immutable dependency injection container.
// Operations like Fork create new Scope values.
type Scope struct {
// values points to the top layer of the immutable value stack (_StackedMap).
values *_StackedMap
// signature is a hash representing the structural identity of this scope,
// based on all definition types involved in its creation.
signature _Hash
// forkFuncKey is a cache key representing the specific Fork operation that created this scope.
forkFuncKey _Hash
}
// Universe is the empty root scope.
var Universe = Scope{}
// New creates a new root Scope with the given definitions.
// Equivalent to Universe.Fork(defs...).
func New(
defs ...any,
) Scope {
return Universe.Fork(defs...)
}
// forkers caches _Forker instances to speed up repeated Fork calls.
// The key is a _Hash derived from the parent scope signature and new def types.
// _Hash -> *_Forker
var forkers sync.Map
// Fork creates a new child scope by layering the given definitions (`defs`)
// on top of the current scope. It handles overriding existing definitions
// and ensures values are lazily initialized.
//
// Definitions can be provider functions or pointers to values. When a pointer is
// provided, the value it points to is copied; subsequent changes to the original
// variable will not affect the value in the scope. To provide a shared singleton,
// use a provider function that returns a pointer.
func (scope Scope) Fork(
defs ...any,
) Scope {
// handle modules
var moduleObjects []any
for _, def := range defs {
if module, ok := def.(isModule); ok {
moduleObjects = append(moduleObjects, module)
}
}
if len(moduleObjects) > 0 {
// If we have modules, we need to construct a new defs slice
// to avoid modifying the caller's slice.
newDefs := make([]any, 0, len(defs))
for _, def := range defs {
if _, ok := def.(isModule); !ok {
newDefs = append(newDefs, def)
}
}
defs = append(newDefs, Methods(moduleObjects...)...)
}
// sorting defs may reduce memory consumption if there're calls with same defs but different order
// but sorting will increase heap allocations, causing performance drop
// Calculate cache key for this Fork operation.
// Key is based on parent signature and the types of new definitions.
// Hashing types is sufficient as only one definition instance per type is effectively used.
h := sha256.New() // use cryptographic hash to avoid collision
h.Write(scope.signature[:])
buf := make([]byte, 0, len(defs)*8)
for _, def := range defs {
id := getTypeID(reflect.TypeOf(def))
buf = binary.NativeEndian.AppendUint64(buf, uint64(id))
}
// h.Write (from sha256.New()) is not expected to return an error,
// but check is included for robustness against potential future changes
// or different hash.Hash implementations.
if _, err := h.Write(buf); err != nil {
panic(fmt.Errorf("unexpected error during hash calculation in Scope.Fork: %w", err))
}
var key _Hash
h.Sum(key[:0])
// Check cache
v, ok := forkers.Load(key)
if ok {
return v.(*_Forker).Fork(scope, defs)
}
// Cache miss, create and cache forker
forker := newForker(scope, defs, key)
v, _ = forkers.LoadOrStore(key, forker)
return v.(*_Forker).Fork(scope, defs)
}
// Assign retrieves values from the scope matching the types of the provided pointers
// and assigns the values to the pointers.
// It panics if any argument is not a pointer or if a required type is not found.
// It's safe to call Assign concurrently.
func (scope Scope) Assign(objects ...any) {
for _, o := range objects {
v := reflect.ValueOf(o)
if v.Kind() != reflect.Pointer {
panic(errors.Join(
fmt.Errorf("%T is not a pointer", o),
ErrBadArgument,
))
}
if v.IsNil() {
panic(errors.Join(
fmt.Errorf("cannot assign to a nil pointer target of type %v", v.Type()),
ErrBadArgument,
))
}
t := v.Type().Elem()
value, ok := scope.Get(t)
if !ok {
throwErrDependencyNotFound(t)
}
v.Elem().Set(value)
}
}
// Assign is a type-safe generic wrapper for Scope.Assign for a single pointer.
func Assign[T any](scope Scope, ptr *T) {
*ptr = Get[T](scope)
}
// get retrieves a value of a specific type ID and type from the scope.
// Internal function called by Get and Assign.
func (scope Scope) get(id _TypeID) (
ret reflect.Value,
ok bool,
) {
// special types
switch id {
case injectStructTypeID:
return reflect.ValueOf(scope.InjectStruct), true
}
value, ok := scope.values.Load(id)
if !ok {
return ret, false
}
return value.initializer.get(scope, value.typeInfo.Position), true
}
// Get retrieves a single value of the specified type `t` from the scope.
// It panics if the type is not found or if an error occurs during resolution.
// Use Assign or the generic Get[T] for safer retrieval.
func (scope Scope) Get(t reflect.Type) (
ret reflect.Value,
ok bool,
) {
return scope.get(getTypeID(t))
}
// Get is a type-safe generic function to retrieve a single value of type T.
// It panics if the type is not found or if an error occurs during resolution.
func Get[T any](scope Scope) (o T) {
typ := reflect.TypeFor[T]()
value, ok := scope.Get(typ)
if !ok {
throwErrDependencyNotFound(typ)
}
return value.Interface().(T)
}
// Call executes the given function `fn`, resolving its arguments from the scope.
// It returns a CallResult containing the return values of the function.
// Panics if argument resolution fails or if `fn` is not a function.
func (scope Scope) Call(fn any) CallResult {
return scope.CallValue(reflect.ValueOf(fn))
}
// Cache for the argument-fetching logic for a given function type.
// reflect.Type -> func(Scope, []reflect.Value) (int, error)
var getArgsFunc sync.Map
// getArgs resolves the arguments for a function of type `fnType` from the scope
// and places them into the `args` slice. It returns the number of arguments resolved.
func (scope Scope) getArgs(fnType reflect.Type, args []reflect.Value) int {
if v, ok := getArgsFunc.Load(fnType); ok {
return v.(func(Scope, []reflect.Value) int)(scope, args)
}
return scope.getArgsSlow(fnType, args)
}
// getArgsSlow generates and caches the argument-fetching logic for a function type.
func (scope Scope) getArgsSlow(fnType reflect.Type, args []reflect.Value) int {
numIn := fnType.NumIn()
ids := make([]_TypeID, numIn)
for i := range numIn {
t := fnType.In(i)
ids[i] = getTypeID(t)
}
getArgs := func(scope Scope, args []reflect.Value) int {
for i := range ids {
var ok bool
args[i], ok = scope.get(ids[i])
if !ok {
throwErrDependencyNotFound(typeIDToType(ids[i]))
}
}
return numIn
}
v, _ := getArgsFunc.LoadOrStore(fnType, getArgs)
return v.(func(Scope, []reflect.Value) int)(scope, args)
}
// Cache for mapping return types to their position index for a given function type.
// reflect.Type -> map[reflect.Type]int
var fnRetTypes sync.Map
const reflectValuesPoolMaxLen = 64
var reflectValuesPool = sync.Pool{
New: func() any {
return new([reflectValuesPoolMaxLen]reflect.Value)
},
}
// CallValue executes the given function `fnValue` after resolving its arguments from the scope.
// It returns a CallResult containing the function's return values.
func (scope Scope) CallValue(fnValue reflect.Value) (res CallResult) {
fnType := fnValue.Type()
var args []reflect.Value
// Use pool for small number of arguments
if nArgs := fnType.NumIn(); nArgs <= reflectValuesPoolMaxLen {
ptr := reflectValuesPool.Get().(*[reflectValuesPoolMaxLen]reflect.Value)
args = (*ptr)[:]
defer func() {
clear(args)
reflectValuesPool.Put(ptr)
}()
} else {
args = make([]reflect.Value, nArgs)
}
n := scope.getArgs(fnType, args)
res.Values = fnValue.Call(args[:n])
// Cache return type positions
if v, ok := fnRetTypes.Load(fnType); ok {
res.positionsByType = v.(map[reflect.Type][]int)
} else {
m := make(map[reflect.Type][]int)
for i := range fnType.NumOut() {
t := fnType.Out(i)
m[t] = append(m[t], i)
}
actual, _ := fnRetTypes.LoadOrStore(fnType, m)
res.positionsByType = actual.(map[reflect.Type][]int)
}
return
}