mirror of
https://github.com/kataras/iris.git
synced 2026-01-06 03:27:27 +00:00
minor fix required for the upcoming release
Former-commit-id: f04b770f93d7612941340af6e45d7d0516035e14
This commit is contained in:
@@ -4,6 +4,7 @@ package handler
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12/context"
|
||||
"github.com/kataras/iris/v12/core/memstore"
|
||||
"github.com/kataras/iris/v12/macro"
|
||||
)
|
||||
|
||||
@@ -76,10 +77,33 @@ func MakeFilter(tmpl macro.Template) context.Filter {
|
||||
return false
|
||||
}
|
||||
|
||||
if !p.Eval(entry.String(), &ctx.Params().Store) {
|
||||
value := p.Eval(entry.String())
|
||||
if value == nil {
|
||||
ctx.StatusCode(p.ErrCode)
|
||||
return false
|
||||
}
|
||||
|
||||
// Fixes binding different path parameters names,
|
||||
//
|
||||
// app.Get("/{fullname:string}", strHandler)
|
||||
// app.Get("/{id:int}", idHandler)
|
||||
//
|
||||
// before that user didn't see anything
|
||||
// but under the hoods the set-ed value was a type of string instead of type of int,
|
||||
// because store contained both "fullname" (which set-ed by the router itself on its string representation)
|
||||
// and "id" by the param evaluator (see core/router/handler.go and bindMultiParamTypesHandler->MakeFilter)
|
||||
// and the MVC get by index (e.g. 0) therefore
|
||||
// it got the "fullname" of type string instead of "id" int if /{int} requested.
|
||||
// which is critical for faster type assertion in the upcoming, new iris dependency injection (20 Feb 2020).
|
||||
ctx.Params().Store[p.Index] = memstore.Entry{
|
||||
Key: p.Name,
|
||||
ValueRaw: value,
|
||||
}
|
||||
|
||||
// for i, v := range ctx.Params().Store {
|
||||
// fmt.Printf("[%d:%s] macro/handler/handler.go: param passed: %s(%v of type: %T)\n", i, v.Key,
|
||||
// p.Src, v.ValueRaw, v.ValueRaw)
|
||||
// }
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
@@ -3,7 +3,6 @@ package macro
|
||||
import (
|
||||
"reflect"
|
||||
|
||||
"github.com/kataras/iris/v12/core/memstore"
|
||||
"github.com/kataras/iris/v12/macro/interpreter/ast"
|
||||
"github.com/kataras/iris/v12/macro/interpreter/parser"
|
||||
)
|
||||
@@ -65,29 +64,28 @@ func (p *TemplateParam) CanEval() bool {
|
||||
return p.canEval
|
||||
}
|
||||
|
||||
// Eval is the most critical part of the TEmplateParam.
|
||||
// It is responsible to return "passed:true" or "not passed:false"
|
||||
// if the "paramValue" is the correct type of the registered parameter type
|
||||
// Eval is the most critical part of the TemplateParam.
|
||||
// It is responsible to return the type-based value if passed otherwise nil.
|
||||
// If the "paramValue" is the correct type of the registered parameter type
|
||||
// and all functions, if any, are passed.
|
||||
// "paramChanger" is the same form of context's Params().Set
|
||||
// we could accept a memstore.Store or even context.RequestParams
|
||||
// but this form has been chosed in order to test easier and fully decoupled from a request when necessary.
|
||||
//
|
||||
// It is called from the converted macro handler (middleware)
|
||||
// from the higher-level component of "kataras/iris/macro/handler#MakeHandler".
|
||||
func (p *TemplateParam) Eval(paramValue string, paramSetter memstore.ValueSetter) bool {
|
||||
func (p *TemplateParam) Eval(paramValue string) interface{} {
|
||||
if p.TypeEvaluator == nil {
|
||||
for _, fn := range p.stringInFuncs {
|
||||
if !fn(paramValue) {
|
||||
return false
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return true
|
||||
return paramValue
|
||||
}
|
||||
|
||||
// fmt.Printf("macro/template.go#L88: Eval for param value: %s and p.Src: %s\n", paramValue, p.Src)
|
||||
|
||||
newValue, passed := p.TypeEvaluator(paramValue)
|
||||
if !passed {
|
||||
return false
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(p.Funcs) > 0 {
|
||||
@@ -96,13 +94,14 @@ func (p *TemplateParam) Eval(paramValue string, paramSetter memstore.ValueSetter
|
||||
// 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
|
||||
return false
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
paramSetter.Set(p.Name, newValue)
|
||||
return true
|
||||
// fmt.Printf("macro/template.go: passed with value: %v and type: %T\n", newValue, newValue)
|
||||
|
||||
return newValue
|
||||
}
|
||||
|
||||
// Parse takes a full route path and a macro map (macro map contains the macro types with their registered param functions)
|
||||
|
||||
Reference in New Issue
Block a user