Skip to content

Commit

Permalink
refactor(event): add AggregateRef.Split()
Browse files Browse the repository at this point in the history
  • Loading branch information
bounoable committed Apr 18, 2022
1 parent cafb04e commit f550bab
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion command/builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestDeleteAggregate(t *testing.T) {
t.Fatalf("Name() should return %q; got %q", "goes.command.aggregate.delete", cmd.Name())
}

id, name, _ := cmd.Aggregate().Aggregate()
id, name := cmd.Aggregate().Split()

if name != aggregateName {
t.Fatalf("AggregateName() should return %q; got %q", aggregateName, name)
Expand Down
2 changes: 1 addition & 1 deletion command/builtin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Handle(ctx context.Context, bus command.Bus, repo aggregate.Repository, opt

deleteErrors, err := h.Handle(ctx, DeleteAggregateCmd, func(ctx command.Context) error {
cmd := ctx
id, name, _ := cmd.Aggregate().Aggregate()
id, name := cmd.Aggregate().Split()
a := aggregate.New(name, id)

if err := repo.Fetch(ctx, a); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions command/cmdbus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func (b *Bus) Dispatch(ctx context.Context, cmd command.Command, opts ...command
return fmt.Errorf("encode payload: %w", err)
}

id, name, _ := cmd.Aggregate().Aggregate()
id, name := cmd.Aggregate().Split()

evt := event.New(CommandDispatched, CommandDispatchedData{
ID: cmd.ID(),
Expand Down Expand Up @@ -576,7 +576,7 @@ func (b *Bus) commandExecuted(evt event.Of[CommandExecutedData]) {

// if the dispatch requested a report, report the execution result
if cmd.cfg.Reporter != nil {
id, name, _ := cmd.cmd.Aggregate().Aggregate()
id, name := cmd.cmd.Aggregate().Split()

var err error
if data.Error != "" {
Expand Down
4 changes: 2 additions & 2 deletions command/cmdbus/bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,8 @@ func assertEqualCommands(t *testing.T, cmd1, cmd2 command.Command) {
t.Errorf("Command ID mismatch: %s != %s", cmd1.ID(), cmd2.ID())
}

id1, name1, _ := cmd1.Aggregate().Aggregate()
id2, name2, _ := cmd2.Aggregate().Aggregate()
id1, name1 := cmd1.Aggregate().Split()
id2, name2 := cmd2.Aggregate().Split()

if name1 != name2 {
t.Errorf("Command AggregateName mismatch: %q != %q", name1, name2)
Expand Down
6 changes: 3 additions & 3 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (cmd Cmd[P]) Command() Of[P] {

// Any returns the command with its type paramter set to `any`.
func Any[P any](cmd Of[P]) Cmd[any] {
id, name, _ := cmd.Aggregate().Aggregate()
id, name := cmd.Aggregate().Split()
return New[any](cmd.Name(), cmd.Payload(), ID(cmd.ID()), Aggregate(name, id))
}

Expand All @@ -131,13 +131,13 @@ func TryCast[To, From any](cmd Of[From]) (Cmd[To], bool) {
if !ok {
return Cmd[To]{}, false
}
id, name, _ := cmd.Aggregate().Aggregate()
id, name := cmd.Aggregate().Split()
return New(cmd.Name(), load, ID(cmd.ID()), Aggregate(name, id)), true
}

// Cast casts the payload of the given command to the given `To` type. If the
// payload is not of type `To`, Cast panics.
func Cast[To, From any](cmd Of[From]) Cmd[To] {
id, name, _ := cmd.Aggregate().Aggregate()
id, name := cmd.Aggregate().Split()
return New(cmd.Name(), any(cmd.Payload()).(To), ID(cmd.ID()), Aggregate(name, id))
}
3 changes: 2 additions & 1 deletion command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func TestAggregateName(t *testing.T) {
command.Aggregate(a.AggregateName(), a.AggregateID()),
)

id, name, _ := cmd.Aggregate().Aggregate()
id, name := cmd.Aggregate().Split()

if name != a.AggregateName() {
t.Fatalf(
"cmd.AggregateName should return %q; got %q",
Expand Down
5 changes: 5 additions & 0 deletions event/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ func (ref AggregateRef) Aggregate() (uuid.UUID, string, int) {
return ref.ID, ref.Name, -1
}

// Split splits the reference into its id and name.
func (ref AggregateRef) Split() (uuid.UUID, string) {
return ref.ID, ref.Name
}

// String returns the string representation of the aggregate:
// "NAME(ID)"
func (ref AggregateRef) String() string {
Expand Down

0 comments on commit f550bab

Please sign in to comment.