Skip to content

Commit

Permalink
Code formatting for PR #91
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Jan 28, 2017
1 parent 0423e85 commit 78521e4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 50 deletions.
7 changes: 3 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,11 @@ func (r *Request) Body() *RequestBody {
return &RequestBody{r.Request.Body}
}

// ContextInvoker Context Invoker Handler
// ContextInvoker is an inject.FastInvoker wrapper of func(ctx *Context).
type ContextInvoker func(ctx *Context)

// Invoke ContextInvoker
func (l ContextInvoker) Invoke(p []interface{}) ([]reflect.Value, error) {
l(p[0].(*Context))
func (invoke ContextInvoker) Invoke(params []interface{}) ([]reflect.Value, error) {
invoke(params[0].(*Context))
return nil, nil
}

Expand Down
7 changes: 3 additions & 4 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ func init() {
ColorLog = runtime.GOOS != "windows"
}

// LoggerInvoker Context Logger Invoker Handler
// LoggerInvoker is an inject.FastInvoker wrapper of func(ctx *Context, log *log.Logger).
type LoggerInvoker func(ctx *Context, log *log.Logger)

// Invoke LoggerInvoker
func (l LoggerInvoker) Invoke(p []interface{}) ([]reflect.Value, error) {
l(p[0].(*Context), p[1].(*log.Logger))
func (invoke LoggerInvoker) Invoke(params []interface{}) ([]reflect.Value, error) {
invoke(params[0].(*Context), params[1].(*log.Logger))
return nil, nil
}

Expand Down
55 changes: 26 additions & 29 deletions macaron.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import (
"github.com/go-macaron/inject"
)

const _VERSION = "1.1.12.0122"

const _VERSION = "1.2.0.0128"

func Version() string {
return _VERSION
Expand All @@ -44,27 +43,26 @@ func Version() string {
// and panics if an argument could not be fullfilled via dependency injection.
type Handler interface{}

// handlerFuncInvoker func(http.ResponseWriter, *http.Request) Invoker Handler
// handlerFuncInvoker is an inject.FastInvoker wrapper of func(http.ResponseWriter, *http.Request).
type handlerFuncInvoker func(http.ResponseWriter, *http.Request)

// Invoke handlerFuncInvoker
func (l handlerFuncInvoker) Invoke(p []interface{}) ([]reflect.Value, error) {
l(p[0].(http.ResponseWriter), p[1].(*http.Request))
func (invoke handlerFuncInvoker) Invoke(params []interface{}) ([]reflect.Value, error) {
invoke(params[0].(http.ResponseWriter), params[1].(*http.Request))
return nil, nil
}

// internalServerErrorInvoker internalServerError Invoker Handler
// internalServerErrorInvoker is an inject.FastInvoker wrapper of func(rw http.ResponseWriter, err error).
type internalServerErrorInvoker func(rw http.ResponseWriter, err error)

// Invoke internalServerErrorInvoker
func (l internalServerErrorInvoker) Invoke(p []interface{}) ([]reflect.Value, error) {
l(p[0].(http.ResponseWriter), p[1].(error))
func (invoke internalServerErrorInvoker) Invoke(params []interface{}) ([]reflect.Value, error) {
invoke(params[0].(http.ResponseWriter), params[1].(error))
return nil, nil
}

// validateWrapHandler makes sure a handler is a callable function,
// and panics if it is not.
func validateWrapHandler(h Handler) Handler {
// validateAndWrapHandler makes sure a handler is a callable function, it panics if not.
// When the handler is also potential to be any built-in inject.FastInvoker,
// it wraps the handler automatically to have some performance gain.
func validateAndWrapHandler(h Handler) Handler {
if reflect.TypeOf(h).Kind() != reflect.Func {
panic("Macaron handler must be a callable function")
}
Expand All @@ -84,25 +82,24 @@ func validateWrapHandler(h Handler) Handler {
return h
}

// validateWrapHandlers makes sure handlers are callable functions,
// and panics if any of them is not.
func validateWrapHandlers(handlers []Handler, wrapActions ...func(Handler) Handler) []Handler {
rHandlers := make([]Handler, 0, len(handlers))

var wrapAction func(Handler) Handler
if len(wrapActions) > 0 {
wrapAction = wrapActions[0]
// validateAndWrapHandlers preforms validation and wrapping for each input handler.
// It accepts an optional wrapper function to perform custom wrapping on handlers.
func validateAndWrapHandlers(handlers []Handler, wrappers ...func(Handler) Handler) []Handler {
var wrapper func(Handler) Handler
if len(wrappers) > 0 {
wrapper = wrappers[0]
}

for _, h := range handlers {
h = validateWrapHandler(h)
if wrapAction != nil && !inject.IsFastInvoker(h) {
h = wrapAction(h)
wrappedHandlers := make([]Handler, len(handlers))
for i, h := range handlers {
h = validateAndWrapHandler(h)
if wrapper != nil && !inject.IsFastInvoker(h) {
h = wrapper(h)
}
rHandlers = append(rHandlers, h)
wrappedHandlers[i] = h
}

return rHandlers
return wrappedHandlers
}

// Macaron represents the top level web application.
Expand Down Expand Up @@ -169,7 +166,7 @@ func (m *Macaron) Handlers(handlers ...Handler) {
// Action sets the handler that will be called after all the middleware has been invoked.
// This is set to macaron.Router in a macaron.Classic().
func (m *Macaron) Action(handler Handler) {
handler = validateWrapHandler(handler)
handler = validateAndWrapHandler(handler)
m.action = handler
}

Expand All @@ -185,7 +182,7 @@ func (m *Macaron) Before(handler BeforeHandler) {
// and panics if the handler is not a callable func.
// Middleware Handlers are invoked in the order that they are added.
func (m *Macaron) Use(handler Handler) {
handler = validateWrapHandler(handler)
handler = validateAndWrapHandler(handler)
m.handlers = append(m.handlers, handler)
}

Expand Down
21 changes: 10 additions & 11 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ type Router struct {
notFound http.HandlerFunc
internalServerError func(*Context, error)

//wrap function Handler to FastInvoker Handler
handlerWrapAction func(Handler) Handler
// handlerWrapper is used to wrap arbitrary function from Handler to inject.FastInvoker.
handlerWrapper func(Handler) Handler
}

func NewRouter() *Router {
Expand Down Expand Up @@ -176,7 +176,7 @@ func (r *Router) Handle(method string, pattern string, handlers []Handler) *Rout
h = append(h, handlers...)
handlers = h
}
handlers = validateWrapHandlers(handlers, r.handlerWrapAction)
handlers = validateAndWrapHandlers(handlers, r.handlerWrapper)

return r.handle(method, pattern, func(resp http.ResponseWriter, req *http.Request, params Params) {
c := r.m.createContext(resp, req)
Expand Down Expand Up @@ -254,11 +254,11 @@ func (r *Router) Combo(pattern string, h ...Handler) *ComboRouter {
return &ComboRouter{r, pattern, h, map[string]bool{}, nil}
}

// NotFound Configurable http.HandlerFunc which is called when no matching route is
// NotFound configurates http.HandlerFunc which is called when no matching route is
// found. If it is not set, http.NotFound is used.
// Be sure to set 404 response code in your handler.
func (r *Router) NotFound(handlers ...Handler) {
handlers = validateWrapHandlers(handlers)
handlers = validateAndWrapHandlers(handlers)
r.notFound = func(rw http.ResponseWriter, req *http.Request) {
c := r.m.createContext(rw, req)
c.handlers = make([]Handler, 0, len(r.m.handlers)+len(handlers))
Expand All @@ -268,11 +268,11 @@ func (r *Router) NotFound(handlers ...Handler) {
}
}

// InternalServerError Configurable handler which is called when route handler returns
// InternalServerError configurates handler which is called when route handler returns
// error. If it is not set, default handler is used.
// Be sure to set 500 response code in your handler.
func (r *Router) InternalServerError(handlers ...Handler) {
handlers = validateWrapHandlers(handlers)
handlers = validateAndWrapHandlers(handlers)
r.internalServerError = func(c *Context, err error) {
c.index = 0
c.handlers = handlers
Expand All @@ -281,10 +281,9 @@ func (r *Router) InternalServerError(handlers ...Handler) {
}
}

// HandlerWrapAction Set HandlerWrap Action
// f func try wrap function Handler to FastInvoker Handler
func (r *Router) HandlerWrapAction(f func(Handler) Handler) {
r.handlerWrapAction = f
// SetHandlerWrapper sets handlerWrapper for the router.
func (r *Router) SetHandlerWrapper(f func(Handler) Handler) {
r.handlerWrapper = f
}

func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
Expand Down
4 changes: 2 additions & 2 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func test_Router_Handle(t *testing.T, isFast bool) {

if isFast {
// FastInvoker Handler Wrap Action
m.Router.HandlerWrapAction(func(h Handler) Handler {
m.Router.SetHandlerWrapper(func(h Handler) Handler {
switch v := h.(type) {
case func() string:
return handlerFunc0Invoker(v)
Expand Down Expand Up @@ -138,7 +138,7 @@ func test_Router_Handle(t *testing.T, isFast bool) {

if isFast {
//remove Handler Wrap Action
m.Router.HandlerWrapAction(nil)
m.Router.SetHandlerWrapper(nil)
}
})

Expand Down

0 comments on commit 78521e4

Please sign in to comment.