Skip to content

Commit

Permalink
Refactor request parser reorder arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Jun 9, 2024
1 parent 154f2f7 commit 0049d3f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion dispatch/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ func (f RequestHandlerFunc) Handle(conn wasabi.Connection, req wasabi.Request) e
return f(conn, req)
}

type RequestParser func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request
type RequestParser func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request
2 changes: 1 addition & 1 deletion dispatch/router_dipatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *RouterDispatcher) AddBackend(backend wasabi.RequestHandler, routingKeys
// determining the appropriate backend, and handling the request using middleware.
// If an error occurs during handling, it is logged.
func (d *RouterDispatcher) Dispatch(conn wasabi.Connection, msgType wasabi.MessageType, data []byte) {
req := d.parser(conn, msgType, data, conn.Context())
req := d.parser(conn, conn.Context(), msgType, data)

if req == nil {
return
Expand Down
16 changes: 8 additions & 8 deletions dispatch/router_dipatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func TestNewRouterDispatcher(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)

parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return mocks.NewMockRequest(t)
}

Expand All @@ -28,7 +28,7 @@ func TestNewRouterDispatcher(t *testing.T) {
}
func TestRouterDispatcher_AddBackend(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return mocks.NewMockRequest(t)
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)

req := mocks.NewMockRequest(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return req
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand All @@ -85,7 +85,7 @@ func TestRouterDispatcher_DispatchDefaultBackend(t *testing.T) {
func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
req := mocks.NewMockRequest(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return req
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand All @@ -108,7 +108,7 @@ func TestRouterDispatcher_DispatchByRoutingKey(t *testing.T) {

func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return nil
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand All @@ -124,7 +124,7 @@ func TestRouterDispatcher_DispatchWrongRequest(t *testing.T) {
func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
req := mocks.NewMockRequest(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return req
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand All @@ -145,7 +145,7 @@ func TestRouterDispatcher_DispatchErrorHandlingRequest(t *testing.T) {
}
func TestRouterDispatcher_Use(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return mocks.NewMockRequest(t)
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand All @@ -166,7 +166,7 @@ func TestRouterDispatcher_UseMiddleware(t *testing.T) {
defaultBackend := mocks.NewMockBackend(t)
defaultBackend.EXPECT().Handle(mockConn, mockReq).Return(testError)

parser := func(_ wasabi.Connection, _ wasabi.MessageType, _ []byte, _ context.Context) wasabi.Request {
parser := func(_ wasabi.Connection, _ context.Context, _ wasabi.MessageType, _ []byte) wasabi.Request {
return mocks.NewMockRequest(t)
}
dispatcher := NewRouterDispatcher(defaultBackend, parser)
Expand Down
2 changes: 1 addition & 1 deletion examples/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {
return conn.Send(wasabi.MsgTypeText, req.Data())
})

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request {
dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(ctx, msgType, data)
})
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))
Expand Down
2 changes: 1 addition & 1 deletion examples/http_backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
return httpReq, nil
})

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request {
dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(ctx, msgType, data)
})

Expand Down
2 changes: 1 addition & 1 deletion examples/passthrough/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
},
)

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request {
dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(conn.Context(), msgType, data)
})
channel := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))
Expand Down
2 changes: 1 addition & 1 deletion tests/echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestEcho(t *testing.T) {
return conn.Send(wasabi.MsgTypeText, req.Data())
})

dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, msgType wasabi.MessageType, data []byte, ctx context.Context) wasabi.Request {
dispatcher := dispatch.NewRouterDispatcher(backend, func(conn wasabi.Connection, ctx context.Context, msgType wasabi.MessageType, data []byte) wasabi.Request {
return dispatch.NewRawRequest(ctx, msgType, data)
})
ch := channel.NewChannel("/", dispatcher, channel.NewConnectionRegistry(), channel.WithOriginPatterns("*"))
Expand Down

0 comments on commit 0049d3f

Please sign in to comment.