From 83305a4b53687fc46a594e02ed01d4a64b276c6f Mon Sep 17 00:00:00 2001 From: ssengalanto Date: Thu, 20 Jul 2023 10:45:42 +0800 Subject: [PATCH] remove interface and linq dependency --- midt.go | 40 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 30 deletions(-) diff --git a/midt.go b/midt.go index 41c916c..7aa32b1 100644 --- a/midt.go +++ b/midt.go @@ -8,18 +8,8 @@ import ( "context" "fmt" "reflect" - - "github.com/ahmetb/go-linq/v3" ) -type Mediator interface { - RegisterRequestHandler(handler RequestHandler) error - RegisterNotificationHandler(handler NotificationHandler) error - RegisterPipelineBehaviour(behaviour PipelineBehaviour) error - Send(ctx context.Context, request any) (any, error) - Publish(ctx context.Context, request any) error -} - type RequestHandler interface { Name() string Handle(ctx context.Context, request any) (any, error) @@ -86,7 +76,7 @@ func (m *Midt) RegisterPipelineBehaviour(behaviour PipelineBehaviour) error { return fmt.Errorf("%w: %s", ErrPipelineBehaviourAlreadyExists, bt) } - m.pipelineBehaviourRegistry = prepend(m.pipelineBehaviourRegistry, behaviour) + m.pipelineBehaviourRegistry = append(m.pipelineBehaviourRegistry, behaviour) return nil } @@ -104,21 +94,17 @@ func (m *Midt) Send(ctx context.Context, request any) (any, error) { return handler.Handle(ctx, request) } - aggregateResult := linq.From(m.pipelineBehaviourRegistry).AggregateWithSeedT( - lastHandler, - func(next RequestHandlerFunc, pipe PipelineBehaviour) RequestHandlerFunc { - pipeValue := pipe - nexValue := next - - var handlerFunc RequestHandlerFunc = func() (any, error) { - return pipeValue.Handle(ctx, request, nexValue) - } + var aggregateResult = lastHandler + for _, pipe := range m.pipelineBehaviourRegistry { + pipeValue := pipe + nextValue := aggregateResult - return handlerFunc - }) + aggregateResult = func() (any, error) { + return pipeValue.Handle(ctx, request, nextValue) + } + } - v := aggregateResult.(RequestHandlerFunc) - response, err := v() + response, err := aggregateResult() if err != nil { return nil, err } @@ -160,9 +146,3 @@ func (m *Midt) existsPipeType(p reflect.Type) bool { } return false } - -// prepend an element in the slice. -func prepend[T any](x []T, y T) []T { - x = append([]T{y}, x...) - return x -}