-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolver.go
More file actions
195 lines (155 loc) · 4.18 KB
/
resolver.go
File metadata and controls
195 lines (155 loc) · 4.18 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
package needle
import (
"context"
"strings"
"github.com/danpasecinic/needle/internal/reflect"
)
type Resolver interface {
Resolve(ctx context.Context, key string) (any, error)
Has(key string) bool
}
type resolverAdapter struct {
container *Container
}
func (r *resolverAdapter) Resolve(ctx context.Context, key string) (any, error) {
return r.container.internal.Resolve(ctx, key)
}
func (r *resolverAdapter) Has(key string) bool {
return r.container.internal.Has(key)
}
func Invoke[T any](c *Container) (T, error) {
return InvokeCtx[T](context.Background(), c)
}
func InvokeCtx[T any](ctx context.Context, c *Container) (T, error) {
var zero T
key := reflect.TypeKey[T]()
instance, err := c.internal.Resolve(ctx, key)
if err != nil {
return zero, errResolutionFailed(reflect.TypeName[T](), err)
}
typed, ok := instance.(T)
if !ok {
return zero, errResolutionFailed(reflect.TypeName[T](), nil)
}
return typed, nil
}
func InvokeNamed[T any](c *Container, name string) (T, error) {
return InvokeNamedCtx[T](context.Background(), c, name)
}
func InvokeNamedCtx[T any](ctx context.Context, c *Container, name string) (T, error) {
var zero T
key := reflect.TypeKeyNamed[T](name)
instance, err := c.internal.Resolve(ctx, key)
if err != nil {
return zero, errResolutionFailed(reflect.TypeName[T]()+"#"+name, err)
}
typed, ok := instance.(T)
if !ok {
return zero, errResolutionFailed(reflect.TypeName[T]()+"#"+name, nil)
}
return typed, nil
}
func MustInvoke[T any](c *Container) T {
v, err := Invoke[T](c)
if err != nil {
panic(err)
}
return v
}
func MustInvokeCtx[T any](ctx context.Context, c *Container) T {
v, err := InvokeCtx[T](ctx, c)
if err != nil {
panic(err)
}
return v
}
func MustInvokeNamed[T any](c *Container, name string) T {
v, err := InvokeNamed[T](c, name)
if err != nil {
panic(err)
}
return v
}
func MustInvokeNamedCtx[T any](ctx context.Context, c *Container, name string) T {
v, err := InvokeNamedCtx[T](ctx, c, name)
if err != nil {
panic(err)
}
return v
}
func TryInvoke[T any](c *Container) (T, bool) {
v, err := Invoke[T](c)
return v, err == nil
}
func TryInvokeNamed[T any](c *Container, name string) (T, bool) {
v, err := InvokeNamed[T](c, name)
return v, err == nil
}
func Has[T any](c *Container) bool {
key := reflect.TypeKey[T]()
return c.internal.Has(key)
}
func HasNamed[T any](c *Container, name string) bool {
key := reflect.TypeKeyNamed[T](name)
return c.internal.Has(key)
}
type Optional[T any] struct {
value T
present bool
}
func (o Optional[T]) Get() (T, bool) {
return o.value, o.present
}
func (o Optional[T]) Value() T {
return o.value
}
func (o Optional[T]) Present() bool {
return o.present
}
func (o Optional[T]) OrElse(defaultValue T) T {
if o.present {
return o.value
}
return defaultValue
}
func (o Optional[T]) OrElseFunc(fn func() T) T {
if o.present {
return o.value
}
return fn()
}
func Some[T any](value T) Optional[T] {
return Optional[T]{value: value, present: true}
}
func None[T any]() Optional[T] {
return Optional[T]{}
}
func InvokeOptional[T any](c *Container) (Optional[T], error) {
return InvokeOptionalCtx[T](context.Background(), c)
}
func InvokeOptionalCtx[T any](ctx context.Context, c *Container) (Optional[T], error) {
return resolveOptional[T](ctx, c, reflect.TypeKey[T](), reflect.TypeName[T]())
}
func InvokeOptionalNamed[T any](c *Container, name string) (Optional[T], error) {
return InvokeOptionalNamedCtx[T](context.Background(), c, name)
}
func InvokeOptionalNamedCtx[T any](ctx context.Context, c *Container, name string) (Optional[T], error) {
return resolveOptional[T](ctx, c, reflect.TypeKeyNamed[T](name), reflect.TypeName[T]()+"#"+name)
}
func resolveOptional[T any](ctx context.Context, c *Container, key, displayName string) (Optional[T], error) {
instance, err := c.internal.Resolve(ctx, key)
if err != nil {
if isServiceNotFound(err) {
return None[T](), nil
}
return None[T](), errResolutionFailed(displayName, err)
}
typed, ok := instance.(T)
if !ok {
return None[T](), errResolutionFailed(displayName, nil)
}
return Some(typed), nil
}
func isServiceNotFound(err error) bool {
return err != nil && strings.Contains(err.Error(), "service not found:")
}