-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
27 lines (23 loc) · 862 Bytes
/
handler.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
package bunnify
import (
"context"
"encoding/json"
)
// EventHandler is the type definition for a function that is used to handle events of a specific type.
type EventHandler[T any] func(ctx context.Context, event ConsumableEvent[T]) error
// wrappedHandler is internally used to wrap the generic EventHandler
// this is to facilitate adding all the different type of T on the same map
type wrappedHandler func(ctx context.Context, event unmarshalEvent) error
func newWrappedHandler[T any](handler EventHandler[T]) wrappedHandler {
return func(ctx context.Context, event unmarshalEvent) error {
consumableEvent := ConsumableEvent[T]{
Metadata: event.Metadata,
DeliveryInfo: event.DeliveryInfo,
}
err := json.Unmarshal(event.Payload, &consumableEvent.Payload)
if err != nil {
return err
}
return handler(ctx, consumableEvent)
}
}