forked from Infinytum/injector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinjector_read.go
More file actions
140 lines (126 loc) · 3.81 KB
/
Copy pathinjector_read.go
File metadata and controls
140 lines (126 loc) · 3.81 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
package injector
import (
"fmt"
"reflect"
)
// Inject tries to resolve a dependency by its type and optionally its name
// If the dependency is unknown, ErrorDepFactoryNotFound is returned
func Inject[T any](name ...string) (T, error) {
argType := reflect.TypeOf((*T)(nil)).Elem()
factory := depMap.GetOrDefault(reflectTypeKey(argType), nameOrDefault(name), nil)
if factory == nil {
return reflect.Zero(argType).Interface().(T), ErrorDepFactoryNotFound
}
dep, err := factory()
if err != nil {
return reflect.Zero(argType).Interface().(T), err
}
castDep, ok := dep.(T)
if !ok {
return reflect.Zero(argType).Interface().(T), ErrorDependencyTypeMismatch
}
return castDep, nil
}
// InjectT tries to resolve a dependency by its type and optionally its name
// If the dependency is unknown, ErrorDepFactoryNotFound is returned
func InjectT(depType reflect.Type, name ...string) (interface{}, error) {
factory := depMap.GetOrDefault(reflectTypeKey(depType), nameOrDefault(name), nil)
if factory == nil {
return nil, ErrorDepFactoryNotFound
}
return factory()
}
// InjectT tries to resolve a dependency by its type and optionally its name
// If the dependency is unknown, ErrorDepFactoryNotFound is returned
func InjectInto(out interface{}, name ...string) error {
argType := reflect.TypeOf(out)
if argType.Kind() != reflect.Pointer {
return ErrorDepNotAPointer
}
factory := depMap.GetOrDefault(reflectTypeKey(argType.Elem()), nameOrDefault(name), nil)
if factory == nil {
return ErrorDepFactoryNotFound
}
dep, err := factory()
if err != nil {
return err
}
reflect.ValueOf(out).Elem().Set(reflect.ValueOf(dep))
return nil
}
// MustInject tries to resolve a dependency by its type and optionally its name or panics
func MustInject[T any](name ...string) T {
dep, err := Inject[T](name...)
if err != nil {
panic(err)
}
return dep
}
// MustInjectT tries to resolve a dependency by its type and optionally its name or panics
func MustInjectT(depType reflect.Type, name ...string) interface{} {
dep, err := InjectT(depType, name...)
if err != nil {
panic(err)
}
return dep
}
// MustInjectInto tries to resolve a dependency by its type and optionally its name or panics
func MustInjectInto(out interface{}, name ...string) {
if err := InjectInto(out, name...); err != nil {
panic(err)
}
}
// Call will attempt to resolve all arguments of the function and then call it
func Call(fn interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("dependency injection failed because factory paniced, recovered value: %v", r)
}
}()
args, err := injectFuncArgs(fn)
if err != nil {
return
}
resVal := reflect.ValueOf(fn).Call(args)
if len(resVal) > 0 {
if err, ok := resVal[0].Interface().(error); ok && err != nil {
return err
}
}
return
}
// CallT will attempt to resolve all arguments of the function and then call it and return the return value
func CallT[T any](fn interface{}) (val T, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("dependency injection failed because factory paniced, recovered value: %v", r)
}
}()
args, err := injectFuncArgs(fn)
if err != nil {
return
}
resVal := reflect.ValueOf(fn).Call(args)
if len(resVal) > 0 {
if v, ok := resVal[0].Interface().(T); ok {
val = v
}
}
return
}
// MustCall will attempt to resolve all arguments of the function and then call it or panic
func MustCall(fn interface{}) {
if err := Call(fn); err != nil {
panic(err)
}
}
// Fill will attempt to resolve all tagged fields of a struct with their matching dependency
func Fill(strct interface{}) error {
return injectStructFields(strct)
}
// MustFill will attempt to resolve all tagged fields of a struct with their matching dependency or panic
func MustFill(strct interface{}) {
if err := Fill(strct); err != nil {
panic(err)
}
}