Skip to content

Commit

Permalink
feat: ✨ add ctx to RequestHandlerFunc for keeping the flow o… (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi Hadeli authored Oct 5, 2023
1 parent 3c67723 commit e9968cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package commands

import (
"context"
"fmt"

"github.com/mehdihadeli/go-mediatr"
creatingProductDtos "github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/creating_product/dtos"
"github.com/mehdihadeli/go-mediatr/examples/cqrs/internal/products/features/creating_product/events"
Expand All @@ -18,6 +20,10 @@ func NewCreateProductCommandHandler(productRepository *repository.InMemoryProduc
}

func (c *CreateProductCommandHandler) Handle(ctx context.Context, command *CreateProductCommand) (*creatingProductDtos.CreateProductCommandResponse, error) {
isLoggerPipelineEnabled := ctx.Value("logger_pipeline").(bool)
if isLoggerPipelineEnabled {
fmt.Println("[CreateProductCommandHandler]: logging pipeline is enabled")
}

product := &models.Product{
ProductID: command.ProductID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package behaviours

import (
"context"
"github.com/mehdihadeli/go-mediatr"
"log"

"github.com/mehdihadeli/go-mediatr"
)

type RequestLoggerBehaviour struct {
Expand All @@ -12,7 +13,10 @@ type RequestLoggerBehaviour struct {
func (r *RequestLoggerBehaviour) Handle(ctx context.Context, request interface{}, next mediatr.RequestHandlerFunc) (interface{}, error) {
log.Printf("logging some stuff before handling the request")

response, err := next()
// https://golang.org/pkg/context/#Context
ctx = context.WithValue(ctx, "logger_pipeline", true)

response, err := next(ctx)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions mediatr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// RequestHandlerFunc is a continuation for the next task to execute in the pipeline
type RequestHandlerFunc func() (interface{}, error)
type RequestHandlerFunc func(ctx context.Context) (interface{}, error)

// PipelineBehavior is a Pipeline behavior for wrapping the inner handler.
type PipelineBehavior interface {
Expand Down Expand Up @@ -116,7 +116,7 @@ func RegisterNotificationHandlers[TEvent any](handlers ...NotificationHandler[TE
return nil
}

// RegisterNotificationHandlers register the notification handlers factories to mediatr registry.
// RegisterNotificationHandlersFactories register the notification handlers factories to mediatr registry.
func RegisterNotificationHandlersFactories[TEvent any](factories ...NotificationHandlerFactory[TEvent]) error {
if len(factories) == 0 {
return errors.New("no handlers provided")
Expand Down Expand Up @@ -172,23 +172,23 @@ func Send[TRequest any, TResponse any](ctx context.Context, request TRequest) (T
if len(pipelineBehaviours) > 0 {
var reversPipes = reversOrder(pipelineBehaviours)

var lastHandler RequestHandlerFunc = func() (interface{}, error) {
var lastHandler RequestHandlerFunc = func(ctx context.Context) (interface{}, error) {
return handlerValue.Handle(ctx, request)
}

aggregateResult := linq.From(reversPipes).AggregateWithSeedT(lastHandler, func(next RequestHandlerFunc, pipe PipelineBehavior) RequestHandlerFunc {
pipeValue := pipe
nexValue := next

var handlerFunc RequestHandlerFunc = func() (interface{}, error) {
var handlerFunc RequestHandlerFunc = func(ctx context.Context) (interface{}, error) {
return pipeValue.Handle(ctx, request, nexValue)
}

return handlerFunc
})

v := aggregateResult.(RequestHandlerFunc)
response, err := v()
response, err := v(ctx)

if err != nil {
return *new(TResponse), errors.Wrap(err, "error handling request")
Expand Down
4 changes: 2 additions & 2 deletions mediatr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func (c *PipelineBehaviourTest) Handle(ctx context.Context, request interface{},
fmt.Println("PipelineBehaviourTest.Handled")
testData = append(testData, "PipelineBehaviourTest")

res, err := next()
res, err := next(ctx)
if err != nil {
return nil, err
}
Expand All @@ -471,7 +471,7 @@ func (c *PipelineBehaviourTest2) Handle(ctx context.Context, request interface{}
fmt.Println("PipelineBehaviourTest2.Handled")
testData = append(testData, "PipelineBehaviourTest2")

res, err := next()
res, err := next(ctx)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit e9968cb

Please sign in to comment.