Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
yottahmd committed Aug 17, 2024
1 parent 891cc66 commit 74eee8f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
7 changes: 6 additions & 1 deletion dew.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ var (
)

// Dispatch executes the action.
func Dispatch[T Action](ctx context.Context, action *T) (*T, error) {
return action, DispatchMulti(ctx, NewAction(action))
}

// DispatchMulti executes all actions synchronously.
// It assumes that all handlers have been registered to the same mux.
func Dispatch(ctx context.Context, actions ...CommandHandler[Action]) error {
func DispatchMulti(ctx context.Context, actions ...CommandHandler[Action]) error {
if len(actions) == 0 {
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions examples/authorization/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func runMemberScenario(bus dew.Bus) error {
fmt.Printf("Organization Profile: %s\n", orgProfile.Result)

fmt.Println("\n2. Dispatch an action to update the organization profile (should fail for member).")
err = dew.Dispatch(memberContext, dew.NewAction(&action.UpdateOrgAction{Name: "Foo"}))
_, err = dew.Dispatch(memberContext, &action.UpdateOrgAction{Name: "Foo"})
if err == nil {
return fmt.Errorf("expected unauthorized error, got nil")
}
Expand All @@ -80,7 +80,7 @@ func runAdminScenario(bus dew.Bus) error {
adminContext := authContext(busContext, &CurrentUser{ID: adminID})

fmt.Println("\n3. Dispatch an action to update the organization profile (should succeed for admin).")
err := dew.Dispatch(adminContext, dew.NewAction(&action.UpdateOrgAction{Name: "Foo"}))
err := dew.DispatchMulti(adminContext, dew.NewAction(&action.UpdateOrgAction{Name: "Foo"}))
if err != nil {
return fmt.Errorf("unexpected error in UpdateOrgAction: %w", err)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/hello-world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ func run() error {
}

// Create and dispatch HelloAction.
action := &HelloAction{Name: name}
if err := dew.Dispatch(ctx, dew.NewAction(action)); err != nil {
if _, err := dew.Dispatch(ctx, &HelloAction{Name: name}); err != nil {
return fmt.Errorf("failed to dispatch HelloAction: %w", err)
}

Expand Down
22 changes: 11 additions & 11 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestMux_BasicCommand(t *testing.T) {
func TestMux_DispatchError(t *testing.T) {
t.Run("BusNotFound", func(t *testing.T) {
ctx := context.Background()
err := dew.Dispatch(ctx, dew.NewAction(&createUser{Name: "john"}))
err := dew.DispatchMulti(ctx, dew.NewAction(&createUser{Name: "john"}))
if err == nil {
t.Fatal("expected an error, but got nil")
}
Expand All @@ -45,7 +45,7 @@ func TestMux_DispatchError(t *testing.T) {
t.Run("ResolveError", func(t *testing.T) {
mux := dew.New()
ctx := dew.NewContext(context.Background(), mux)
err := dew.Dispatch(ctx, dew.NewAction(&createUser{Name: "john"}))
err := dew.DispatchMulti(ctx, dew.NewAction(&createUser{Name: "john"}))
if err == nil {
t.Fatal("expected an error, but got nil")
}
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestMux_HandlerNotFound(t *testing.T) {
ctx := dew.NewContext(context.Background(), mux)

action := dew.NewAction(&createUser{Name: "john"})
err := dew.Dispatch(ctx, action)
err := dew.DispatchMulti(ctx, action)
if err == nil {
t.Fatal("expected an error, but got nil")
}
Expand Down Expand Up @@ -364,7 +364,7 @@ func TestMux_Middlewares(t *testing.T) {

// dispatch no action

if err := dew.Dispatch(ctx); err != nil {
if err := dew.DispatchMulti(ctx); err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestMux_DispatchMiddlewares(t *testing.T) {
}

// multiple commands
if err := dew.Dispatch(ctx,
if err := dew.DispatchMulti(ctx,
dew.NewAction(createUsers[0]),
dew.NewAction(createUsers[1]),
); err != nil {
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestMux_QueryMiddlewares(t *testing.T) {

// multiple commands
createUser := &createUser{Name: "test"}
if err := dew.Dispatch(ctx, dew.NewAction(createUser)); err != nil {
if err := dew.DispatchMulti(ctx, dew.NewAction(createUser)); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -631,7 +631,7 @@ func TestMux_ErrorHandling(t *testing.T) {
ctx := dew.NewContext(context.Background(), mux)

createUser := &createUser{Name: ""}
err := dew.Dispatch(ctx, dew.NewAction(createUser))
err := dew.DispatchMulti(ctx, dew.NewAction(createUser))
if err == nil {
t.Fatal("expected an error, but got nil")
}
Expand All @@ -646,7 +646,7 @@ func TestMux_Validation(t *testing.T) {

ctx := dew.NewContext(context.Background(), mux)

err := dew.Dispatch(ctx, dew.NewAction(&createPost{Title: ""}))
err := dew.DispatchMulti(ctx, dew.NewAction(&createPost{Title: ""}))
if err == nil {
t.Fatal("expected a validation error, but got nil")
}
Expand Down Expand Up @@ -722,7 +722,7 @@ func BenchmarkMux(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
_ = dew.Dispatch(ctx1, dew.NewAction(&createPost{Title: "john"}))
_ = dew.DispatchMulti(ctx1, dew.NewAction(&createPost{Title: "john"}))
}
})

Expand Down Expand Up @@ -759,7 +759,7 @@ func BenchmarkMux(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
_ = dew.Dispatch(ctx2, dew.NewAction(&createPost{Title: "john"}))
_ = dew.DispatchMulti(ctx2, dew.NewAction(&createPost{Title: "john"}))
}
})
}
Expand All @@ -775,7 +775,7 @@ func testRunQuery[T dew.QueryAction](t *testing.T, ctx context.Context, query *T

func testRunDispatch(t *testing.T, ctx context.Context, commands ...dew.CommandHandler[dew.Action]) {
t.Helper()
err := dew.Dispatch(ctx, commands...)
err := dew.DispatchMulti(ctx, commands...)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 74eee8f

Please sign in to comment.