-
Notifications
You must be signed in to change notification settings - Fork 17
/
mediatr.go
268 lines (205 loc) · 7.9 KB
/
mediatr.go
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
package mediatr
import (
"context"
"github.com/ahmetb/go-linq/v3"
"github.com/goccy/go-reflect"
"github.com/pkg/errors"
)
// RequestHandlerFunc is a continuation for the next task to execute in the pipeline
type RequestHandlerFunc func(ctx context.Context) (interface{}, error)
// PipelineBehavior is a Pipeline behavior for wrapping the inner handler.
type PipelineBehavior interface {
Handle(ctx context.Context, request interface{}, next RequestHandlerFunc) (interface{}, error)
}
type RequestHandler[TRequest any, TResponse any] interface {
Handle(ctx context.Context, request TRequest) (TResponse, error)
}
type RequestHandlerFactory[TRequest any, TResponse any] func() RequestHandler[TRequest, TResponse]
type NotificationHandler[TNotification any] interface {
Handle(ctx context.Context, notification TNotification) error
}
type NotificationHandlerFactory[TNotification any] func() NotificationHandler[TNotification]
var requestHandlersRegistrations = map[reflect.Type]interface{}{}
var notificationHandlersRegistrations = map[reflect.Type][]interface{}{}
var pipelineBehaviours []interface{} = []interface{}{}
type Unit struct{}
func registerRequestHandler[TRequest any, TResponse any](handler any) error {
var request TRequest
requestType := reflect.TypeOf(request)
_, exist := requestHandlersRegistrations[requestType]
if exist {
// each request in request/response strategy should have just one handler
return errors.Errorf("registered handler already exists in the registry for message %s", requestType.String())
}
requestHandlersRegistrations[requestType] = handler
return nil
}
// RegisterRequestHandler register the request handler to mediatr registry.
func RegisterRequestHandler[TRequest any, TResponse any](handler RequestHandler[TRequest, TResponse]) error {
return registerRequestHandler[TRequest, TResponse](handler)
}
// RegisterRequestHandlerFactory register the request handler factory to mediatr registry.
func RegisterRequestHandlerFactory[TRequest any, TResponse any](factory RequestHandlerFactory[TRequest, TResponse]) error {
return registerRequestHandler[TRequest, TResponse](factory)
}
// RegisterRequestPipelineBehaviors register the request behaviors to mediatr registry.
func RegisterRequestPipelineBehaviors(behaviours ...PipelineBehavior) error {
for _, behavior := range behaviours {
behaviorType := reflect.TypeOf(behavior)
existsPipe := existsPipeType(behaviorType)
if existsPipe {
return errors.Errorf("registered behavior already exists in the registry.")
}
pipelineBehaviours = append(pipelineBehaviours, behavior)
}
return nil
}
func registerNotificationHandler[TEvent any](handler any) error {
var event TEvent
eventType := reflect.TypeOf(event)
handlers, exist := notificationHandlersRegistrations[eventType]
if !exist {
notificationHandlersRegistrations[eventType] = []interface{}{handler}
return nil
}
notificationHandlersRegistrations[eventType] = append(handlers, handler)
return nil
}
// RegisterNotificationHandler register the notification handler to mediatr registry.
func RegisterNotificationHandler[TEvent any](handler NotificationHandler[TEvent]) error {
return registerNotificationHandler[TEvent](handler)
}
// RegisterNotificationHandlerFactory register the notification handler factory to mediatr registry.
func RegisterNotificationHandlerFactory[TEvent any](factory NotificationHandlerFactory[TEvent]) error {
return registerNotificationHandler[TEvent](factory)
}
// RegisterNotificationHandlers register the notification handlers to mediatr registry.
func RegisterNotificationHandlers[TEvent any](handlers ...NotificationHandler[TEvent]) error {
if len(handlers) == 0 {
return errors.New("no handlers provided")
}
for _, handler := range handlers {
err := RegisterNotificationHandler(handler)
if err != nil {
return err
}
}
return nil
}
// RegisterNotificationHandlersFactories register the notification handlers factories to mediatr registry.
func RegisterNotificationHandlersFactories[TEvent any](factories ...NotificationHandlerFactory[TEvent]) error {
if len(factories) == 0 {
return errors.New("no handlers provided")
}
for _, handler := range factories {
err := RegisterNotificationHandlerFactory[TEvent](handler)
if err != nil {
return err
}
}
return nil
}
func ClearRequestRegistrations() {
requestHandlersRegistrations = map[reflect.Type]interface{}{}
}
func ClearNotificationRegistrations() {
notificationHandlersRegistrations = map[reflect.Type][]interface{}{}
}
func buildRequestHandler[TRequest any, TResponse any](handler any) (RequestHandler[TRequest, TResponse], bool) {
handlerValue, ok := handler.(RequestHandler[TRequest, TResponse])
if !ok {
factory, ok := handler.(RequestHandlerFactory[TRequest, TResponse])
if !ok {
return nil, false
}
return factory(), true
}
return handlerValue, true
}
// Send the request to its corresponding request handler.
func Send[TRequest any, TResponse any](ctx context.Context, request TRequest) (TResponse, error) {
requestType := reflect.TypeOf(request)
var response TResponse
handler, ok := requestHandlersRegistrations[requestType]
if !ok {
// request-response strategy should have exactly one handler and if we can't find a corresponding handler, we should return an error
return *new(TResponse), errors.Errorf("no handler for request %T", request)
}
handlerValue, ok := buildRequestHandler[TRequest, TResponse](handler)
if !ok {
return *new(TResponse), errors.Errorf("handler for request %T is not a Handler", request)
}
if len(pipelineBehaviours) > 0 {
var reversPipes = reversOrder(pipelineBehaviours)
var lastHandler RequestHandlerFunc = func(ctx context.Context) (interface{}, error) {
return handlerValue.Handle(ctx, request)
}
aggregateResult := linq.From(reversPipes).AggregateWithSeedT(lastHandler, func(next RequestHandlerFunc, pipe PipelineBehavior) RequestHandlerFunc {
pipeValue := pipe
nexValue := next
var handlerFunc RequestHandlerFunc = func(ctx context.Context) (interface{}, error) {
return pipeValue.Handle(ctx, request, nexValue)
}
return handlerFunc
})
v := aggregateResult.(RequestHandlerFunc)
response, err := v(ctx)
if err != nil {
return *new(TResponse), errors.Wrap(err, "error handling request")
}
return response.(TResponse), nil
} else {
res, err := handlerValue.Handle(ctx, request)
if err != nil {
return *new(TResponse), errors.Wrap(err, "error handling request")
}
response = res
}
return response, nil
}
func buildNotificationHandler[TNotification any](handler any) (NotificationHandler[TNotification], bool) {
handlerValue, ok := handler.(NotificationHandler[TNotification])
if !ok {
factory, ok := handler.(NotificationHandlerFactory[TNotification])
if !ok {
return nil, false
}
return factory(), true
}
return handlerValue, true
}
// Publish the notification event to its corresponding notification handler.
func Publish[TNotification any](ctx context.Context, notification TNotification) error {
eventType := reflect.TypeOf(notification)
handlers, ok := notificationHandlersRegistrations[eventType]
if !ok {
// notification strategy should have zero or more handlers, so it should run without any error if we can't find a corresponding handler
return nil
}
for _, handler := range handlers {
handlerValue, ok := buildNotificationHandler[TNotification](handler)
if !ok {
return errors.Errorf("handler for notification %T is not a Handler", notification)
}
err := handlerValue.Handle(ctx, notification)
if err != nil {
return errors.Wrap(err, "error handling notification")
}
}
return nil
}
func reversOrder(values []interface{}) []interface{} {
var reverseValues []interface{}
for i := len(values) - 1; i >= 0; i-- {
reverseValues = append(reverseValues, values[i])
}
return reverseValues
}
func existsPipeType(p reflect.Type) bool {
for _, pipe := range pipelineBehaviours {
if reflect.TypeOf(pipe) == p {
return true
}
}
return false
}