1
0
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:
Gerasimos (Makis) Maropoulos
2018-09-27 03:17:45 +03:00
parent dc3c38b189
commit d6d27b2605
11 changed files with 338 additions and 279 deletions

View File

@@ -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)