mirror of
https://github.com/kataras/iris.git
synced 2026-01-08 20:41:57 +00:00
Version 11 released. Read https://github.com/kataras/iris/blob/master/HISTORY.md#su-21-october-2018--v1100
Former-commit-id: fe6305deed00e170bf4d39a12c0644fe686e0a24
This commit is contained in:
56
macro/handler/handler.go
Normal file
56
macro/handler/handler.go
Normal file
@@ -0,0 +1,56 @@
|
||||
// Package handler is the highest level module of the macro package which makes use the rest of the macro package,
|
||||
// it is mainly used, internally, by the router package.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/context"
|
||||
"github.com/kataras/iris/macro"
|
||||
)
|
||||
|
||||
// CanMakeHandler reports whether a macro template needs a special macro's evaluator handler to be validated
|
||||
// before procceed to the next handler(s).
|
||||
// If the template does not contain any dynamic attributes and a special handler is NOT required
|
||||
// then it returns false.
|
||||
func CanMakeHandler(tmpl macro.Template) (needsMacroHandler bool) {
|
||||
if len(tmpl.Params) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// check if we have params like: {name:string} or {name} or {anything:path} without else keyword or any functions used inside these params.
|
||||
// 1. if we don't have, then we don't need to add a handler before the main route's handler (as I said, no performance if macro is not really used)
|
||||
// 2. if we don't have any named params then we don't need a handler too.
|
||||
for _, p := range tmpl.Params {
|
||||
if p.CanEval() {
|
||||
// if at least one needs it, then create the handler.
|
||||
needsMacroHandler = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// MakeHandler creates and returns a handler from a macro template, the handler evaluates each of the parameters if necessary at all.
|
||||
// If the template does not contain any dynamic attributes and a special handler is NOT required
|
||||
// then it returns a nil handler.
|
||||
func MakeHandler(tmpl macro.Template) context.Handler {
|
||||
if !CanMakeHandler(tmpl) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return func(ctx context.Context) {
|
||||
for _, p := range tmpl.Params {
|
||||
if !p.CanEval() {
|
||||
continue // allow.
|
||||
}
|
||||
|
||||
if !p.Eval(ctx.Params().Get(p.Name), &ctx.Params().Store) {
|
||||
ctx.StatusCode(p.ErrCode)
|
||||
ctx.StopExecution()
|
||||
return
|
||||
}
|
||||
}
|
||||
// if all passed, just continue.
|
||||
ctx.Next()
|
||||
}
|
||||
}
|
||||
41
macro/handler/handler_test.go
Normal file
41
macro/handler/handler_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kataras/iris/macro"
|
||||
)
|
||||
|
||||
func TestCanMakeHandler(t *testing.T) {
|
||||
tests := []struct {
|
||||
src string
|
||||
needsHandler bool
|
||||
}{
|
||||
{"/static/static", false},
|
||||
{"/{myparam}", false},
|
||||
{"/{myparam min(1)}", true},
|
||||
{"/{myparam else 500}", true},
|
||||
{"/{myparam else 404}", false},
|
||||
{"/{myparam:string}/static", false},
|
||||
{"/{myparam:int}", true},
|
||||
{"/static/{myparam:int}/static", true},
|
||||
{"/{myparam:path}", false},
|
||||
{"/{myparam:path min(1) else 404}", true},
|
||||
}
|
||||
|
||||
availableMacros := *macro.Defaults
|
||||
for i, tt := range tests {
|
||||
tmpl, err := macro.Parse(tt.src, availableMacros)
|
||||
if err != nil {
|
||||
t.Fatalf("[%d] '%s' failed to be parsed: %v", i, tt.src, err)
|
||||
}
|
||||
|
||||
if got := CanMakeHandler(tmpl); got != tt.needsHandler {
|
||||
if tt.needsHandler {
|
||||
t.Fatalf("[%d] '%s' expected to be able to generate an evaluator handler instead of a nil one", i, tt.src)
|
||||
} else {
|
||||
t.Fatalf("[%d] '%s' should not need an evaluator handler", i, tt.src)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user