From bc15b1aabbca913cb07bdbe36a4f7a19d6259d60 Mon Sep 17 00:00:00 2001 From: zeako <4206293+zeako@users.noreply.github.com> Date: Wed, 8 Jul 2020 15:47:44 +0300 Subject: [PATCH] use reverse looping instead of calling recursion --- middleware.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/middleware.go b/middleware.go index 1e77e1d..5a84334 100644 --- a/middleware.go +++ b/middleware.go @@ -14,14 +14,13 @@ type LambdaFunc func(context.Context, interface{}) (interface{}, error) // take in one LambdaFunc and wrap it within another LambdaFunc type Middleware func(next LambdaFunc) LambdaFunc -// buildChain builds the middlware chain recursively, functions are first class +// buildChain builds the middlware chain, functions are first class func buildChain(f LambdaFunc, m ...Middleware) LambdaFunc { - // if our chain is done, use the original LambdaFunc - if len(m) == 0 { - return f + // build middleware chain in reverse order, if no middlewares provided returns f + for i := len(m) - 1; i >= 0; i-- { + f = m[i](f) } - // otherwise nest the LambdaFuncs - return m[0](buildChain(f, m[1:]...)) + return f } // newMiddlewareWrapper takes the middleware chain, and converts it into