test
This commit is contained in:
1 parent
d04679daf5
commit
8db09e3c6a
2 files changed
+82
No files matched your search
@@ -12,9 +12,13 @@ package catalog
|
||||
// docs/plan-projectionstore-generalization.md for why the split exists.
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.k6n.net/mats/platform/event"
|
||||
"git.k6n.net/mats/platform/projection"
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
// Generic machinery, re-exported under their historical catalog names. These
|
||||
@@ -97,3 +101,28 @@ func decodeProjectionUpdates(payload []byte) ([]projection.Update[Projection], e
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// MakeAmqpHandler returns a RabbitMQ delivery handler that wraps a ProjectionStore[T].
|
||||
// It decodes the event.Event envelope, recovers from panics, and applies the frame to the store.
|
||||
func MakeAmqpHandler[T any](store *ProjectionStore[T]) func(amqp.Delivery) error {
|
||||
return func(d amqp.Delivery) (err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
err = fmt.Errorf("projection handler panic recovered: %v", r)
|
||||
}
|
||||
}()
|
||||
var ev event.Event
|
||||
if err := json.Unmarshal(d.Body, &ev); err != nil {
|
||||
return fmt.Errorf("decode event envelope: %w", err)
|
||||
}
|
||||
if err := store.HandleFrame(ev.Meta, ev.Payload); err != nil {
|
||||
return fmt.Errorf("apply projection frame: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// HandleEvent feeds an already-decoded event.Event directly into the projection store.
|
||||
func HandleEvent[T any](store *ProjectionStore[T], ev event.Event) error {
|
||||
return store.HandleFrame(ev.Meta, ev.Payload)
|
||||
}
|
||||
Reference in new issue
Block a user