mirror of
https://github.com/kataras/iris.git
synced 2025-12-21 11:57:02 +00:00
Conversion once at macros and their functions, internal changes required
Former-commit-id: 7b778cccfb7c0e30ca5e8106017ada065993aba5
This commit is contained in:
@@ -3,6 +3,7 @@ package router
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris/context"
|
||||
@@ -83,24 +84,37 @@ func convertTmplToHandler(tmpl *macro.Template) context.Handler {
|
||||
return func(ctx context.Context) {
|
||||
for _, p := range tmpl.Params {
|
||||
paramValue := ctx.Params().Get(p.Name)
|
||||
// first, check for type evaluator
|
||||
if !p.TypeEvaluator(paramValue) {
|
||||
if p.TypeEvaluator == nil {
|
||||
// allow.
|
||||
ctx.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// first, check for type evaluator.
|
||||
newValue, passed := p.TypeEvaluator(paramValue)
|
||||
if !passed {
|
||||
ctx.StatusCode(p.ErrCode)
|
||||
ctx.StopExecution()
|
||||
return
|
||||
}
|
||||
|
||||
// then check for all of its functions
|
||||
for _, evalFunc := range p.Funcs {
|
||||
if !evalFunc(paramValue) {
|
||||
ctx.StatusCode(p.ErrCode)
|
||||
ctx.StopExecution()
|
||||
return
|
||||
if len(p.Funcs) > 0 {
|
||||
paramIn := []reflect.Value{reflect.ValueOf(newValue)}
|
||||
// then check for all of its functions
|
||||
for _, evalFunc := range p.Funcs {
|
||||
// or make it as func(interface{}) bool and pass directly the "newValue"
|
||||
// but that would not be as easy for end-developer, so keep that "slower":
|
||||
if !evalFunc.Call(paramIn)[0].Interface().(bool) { // i.e func(paramValue int) bool
|
||||
ctx.StatusCode(p.ErrCode)
|
||||
ctx.StopExecution()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Params().Store.Set(p.Name, newValue)
|
||||
}
|
||||
// if all passed, just continue
|
||||
// if all passed, just continue.
|
||||
ctx.Next()
|
||||
}
|
||||
}(*tmpl)
|
||||
|
||||
Reference in New Issue
Block a user