-
Notifications
You must be signed in to change notification settings - Fork 0
feat(command): align Result with .NET Eventuous, add Change type #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "reflect" | ||
|
|
||
| eventuous "github.com/eventuous/eventuous-go/core" | ||
| "github.com/eventuous/eventuous-go/core/codec" | ||
| "github.com/eventuous/eventuous-go/core/store" | ||
| "github.com/google/uuid" | ||
| ) | ||
|
|
@@ -21,6 +22,7 @@ type CommandHandler[S any] interface { | |
| type Service[S any] struct { | ||
| reader store.EventReader | ||
| writer store.EventWriter | ||
| typeMap *codec.TypeMap | ||
| fold func(S, any) S | ||
| zero S | ||
| handlers map[reflect.Type]untypedHandler[S] | ||
|
|
@@ -30,12 +32,14 @@ type Service[S any] struct { | |
| func New[S any]( | ||
| reader store.EventReader, | ||
| writer store.EventWriter, | ||
| typeMap *codec.TypeMap, | ||
| fold func(S, any) S, | ||
| zero S, | ||
| ) *Service[S] { | ||
| return &Service[S]{ | ||
| reader: reader, | ||
| writer: writer, | ||
| typeMap: typeMap, | ||
| fold: fold, | ||
| zero: zero, | ||
| handlers: make(map[reflect.Type]untypedHandler[S]), | ||
|
|
@@ -79,7 +83,7 @@ func (svc *Service[S]) Handle(ctx context.Context, command any) (*Result[S], err | |
| if len(newEvents) == 0 { | ||
| return &Result[S]{ | ||
| State: state, | ||
| NewEvents: nil, | ||
| Changes: nil, | ||
| StreamVersion: int64(version), | ||
| }, nil | ||
| } | ||
|
|
@@ -98,15 +102,18 @@ func (svc *Service[S]) Handle(ctx context.Context, command any) (*Result[S], err | |
| return nil, err | ||
| } | ||
|
|
||
| // Step 7: Fold new events into state for the result. | ||
| for _, e := range newEvents { | ||
| // Step 7: Build changes and fold new events into state. | ||
| changes := make([]Change, len(newEvents)) | ||
| for i, e := range newEvents { | ||
| typeName, _ := svc.typeMap.TypeName(e) | ||
|
||
| changes[i] = Change{Event: e, EventType: typeName} | ||
| state = svc.fold(state, e) | ||
| } | ||
|
|
||
| // Step 8: Return Result[S]. | ||
| return &Result[S]{ | ||
| State: state, | ||
| NewEvents: newEvents, | ||
| Changes: changes, | ||
| GlobalPosition: appendResult.GlobalPosition, | ||
| StreamVersion: appendResult.NextExpectedVersion, | ||
| }, nil | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AggregateService.Handlehas the same uncheckedsvc.typeMap.TypeName(e)call when materializingResult.Changes. A niltypeMapcauses a runtime panic, and an unregistered event type is silently converted toeventType:"", so API clients can receive malformed change metadata even though the append succeeded. Returning a clear error here would avoid both crash and silent data-shape corruption.Useful? React with 👍 / 👎.