Skip to content

Commit 4dd870f

Browse files
committed
Simplify Query API parameter
1 parent 8a75fda commit 4dd870f

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func main() {
211211
bus := dew.New()
212212
bus.Register(new(MyHandler))
213213

214-
result, err := dew.Query(ctx, dew.NewQuery(&MyQuery{Question: "What is Dew?"}))
214+
result, err := dew.Query(ctx, bus, &MyQuery{Question: "What is Dew?"})
215215
if err != nil {
216216
fmt.Println("Error executing query:", err)
217217
} else {

dew.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ func Dispatch(ctx context.Context, actions ...CommandHandler[Action]) error {
4040
}
4141

4242
// Query executes the query and returns the result.
43-
func Query[T QueryAction](ctx context.Context, query CommandHandler[T]) (*T, error) {
44-
mux := query.Mux().root()
43+
func Query[T QueryAction](ctx context.Context, bus Bus, query *T) (*T, error) {
44+
queryObj := NewQuery(bus, query)
45+
mux := queryObj.Mux().root()
4546

4647
rctx := mux.pool.Get().(*BusContext)
4748
rctx.Reset()
@@ -50,12 +51,12 @@ func Query[T QueryAction](ctx context.Context, query CommandHandler[T]) (*T, err
5051
defer mux.pool.Put(rctx)
5152

5253
if err := mux.mHandlers[mQuery](rctx, func(ctx Context) error {
53-
return query.Mux().dispatch(QUERY, ctx, query)
54+
return queryObj.Mux().dispatch(QUERY, ctx, queryObj)
5455
}); err != nil {
5556
return nil, err
5657
}
5758

58-
return query.Command().(*T), nil
59+
return queryObj.Command().(*T), nil
5960
}
6061

6162
// QueryAsync executes all queries asynchronously and collects errors.

examples/authorization/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func main() {
4646

4747
// Execute a query to get the organization profile.
4848
ctx = ctxWithCurrUser(context.Background(), &CurrentUser{ID: MemberID})
49-
orgProfile, err := dew.Query(ctx, dew.NewQuery(bus, &query.GetOrgDetailsQuery{}))
49+
orgProfile, err := dew.Query(ctx, bus, &query.GetOrgDetailsQuery{})
5050
println(
5151
fmt.Sprintf("Organization Profile: %s, Error: %v", orgProfile, err),
5252
) // Output: Organization Profile: , Error: <nil>

mux_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/go-dew/dew"
87
"sync/atomic"
98
"testing"
109
"time"
10+
11+
"github.com/go-dew/dew"
1112
)
1213

1314
func TestMux_BasicCommand(t *testing.T) {
@@ -51,7 +52,7 @@ func TestMux_Query(t *testing.T) {
5152
}
5253

5354
// Test query error
54-
_, err := dew.Query(context.Background(), dew.NewQuery(mux, &findUser{ID: 2}))
55+
_, err := dew.Query(context.Background(), mux, &findUser{ID: 2})
5556
if err == nil {
5657
t.Fatal("expected an error, but got nil")
5758
}
@@ -158,7 +159,6 @@ func TestMux_QueryAsync_Error(t *testing.T) {
158159
return errPostNotFound
159160
},
160161
))
161-
162162
commands := dew.Commands{
163163
dew.NewQuery(mux, &findUser{ID: 1}),
164164
dew.NewQuery(mux, &findPost{ID: 1}),
@@ -192,11 +192,11 @@ func TestMux_Reentrant(t *testing.T) {
192192

193193
mux.Register(dew.HandlerFunc[findUserPost](
194194
func(ctx context.Context, query *findUserPost) error {
195-
findUserQuery, err := dew.Query(ctx, dew.NewQuery(dew.FromContext(ctx), &findUser{ID: query.ID}))
195+
findUserQuery, err := dew.Query(ctx, dew.FromContext(ctx), &findUser{ID: query.ID})
196196
if err != nil {
197197
return err
198198
}
199-
postQuery, err := dew.Query(ctx, dew.NewQuery(dew.FromContext(ctx), &findPost{ID: query.ID}))
199+
postQuery, err := dew.Query(ctx, dew.FromContext(ctx), &findPost{ID: query.ID})
200200
if err != nil {
201201
return err
202202
}
@@ -206,7 +206,7 @@ func TestMux_Reentrant(t *testing.T) {
206206
},
207207
))
208208

209-
query, err := dew.Query(context.Background(), dew.NewQuery(mux, &findUserPost{ID: 1}))
209+
query, err := dew.Query(context.Background(), mux, &findUserPost{ID: 1})
210210
if err != nil {
211211
t.Fatalf("unexpected error: %v", err)
212212
}
@@ -259,7 +259,7 @@ func TestMux_Middlewares(t *testing.T) {
259259
}
260260

261261
query := &findUser{ID: 1}
262-
result, err := dew.Query(context.Background(), dew.NewQuery(mux, query))
262+
result, err := dew.Query(context.Background(), mux, query)
263263
if err != nil {
264264
t.Fatal(err)
265265
}
@@ -303,7 +303,7 @@ func TestMux_DispatchMiddlewares(t *testing.T) {
303303
}
304304

305305
// query
306-
findUser, err := dew.Query(context.Background(), dew.NewQuery(mux, &findUser{ID: 1}))
306+
findUser, err := dew.Query(context.Background(), mux, &findUser{ID: 1})
307307
if err != nil {
308308
t.Fatal(err)
309309
}
@@ -376,7 +376,7 @@ func TestMux_QueryMiddlewares(t *testing.T) {
376376
}
377377

378378
// query
379-
findUser, err := dew.Query(context.Background(), dew.NewQuery(mux, &findUser{ID: 1}))
379+
findUser, err := dew.Query(context.Background(), mux, &findUser{ID: 1})
380380
if err != nil {
381381
t.Fatal(err)
382382
}
@@ -627,7 +627,7 @@ func BenchmarkMux(b *testing.B) {
627627
b.ResetTimer()
628628

629629
for i := 0; i < b.N; i++ {
630-
_, _ = dew.Query(context.Background(), dew.NewQuery(mux1, &findUser{ID: 1}))
630+
_, _ = dew.Query(context.Background(), mux1, &findUser{ID: 1})
631631
}
632632
})
633633

@@ -647,7 +647,7 @@ func BenchmarkMux(b *testing.B) {
647647
b.ResetTimer()
648648

649649
for i := 0; i < b.N; i++ {
650-
_, _ = dew.Query(context.Background(), dew.NewQuery(mux2, &findUser{ID: 1}))
650+
_, _ = dew.Query(context.Background(), mux2, &findUser{ID: 1})
651651
}
652652
})
653653

@@ -664,7 +664,7 @@ func BenchmarkMux(b *testing.B) {
664664

665665
func testRunQuery[T dew.QueryAction](t *testing.T, mux dew.Bus, query *T) *T {
666666
t.Helper()
667-
result, err := dew.Query[T](context.Background(), dew.NewQuery(mux, query))
667+
result, err := dew.Query[T](context.Background(), mux, query)
668668
if err != nil {
669669
t.Fatal(err)
670670
}

0 commit comments

Comments
 (0)