mirror of
https://github.com/kataras/iris.git
synced 2025-12-31 16:57:04 +00:00
make the macro#Parse to return a value of a Template instead of its ptr and debug logs for handlers length ignores the internal generated macro evaluator handler if it is there, so end-dev cannot be confused about the debug logs at that point
Former-commit-id: c23a3d10b43f145de575f1ea11e3dbf9bbd33a6b
This commit is contained in:
@@ -7,11 +7,11 @@ import (
|
||||
"github.com/kataras/iris/macro"
|
||||
)
|
||||
|
||||
// MakeHandler creates and returns a handler from a macro template, the handler evaluates each of the parameters if necessary at all.
|
||||
// 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 a nil handler and false as its second output value,
|
||||
// the caller should check those two values before any further action.
|
||||
func MakeHandler(tmpl *macro.Template) (handler context.Handler, needsMacroHandler bool) {
|
||||
// then it returns false.
|
||||
func CanMakeHandler(tmpl macro.Template) (needsMacroHandler bool) {
|
||||
if len(tmpl.Params) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -27,11 +27,18 @@ func MakeHandler(tmpl *macro.Template) (handler context.Handler, needsMacroHandl
|
||||
}
|
||||
}
|
||||
|
||||
if !needsMacroHandler {
|
||||
return
|
||||
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
|
||||
}
|
||||
|
||||
handler = func(ctx context.Context) {
|
||||
return func(ctx context.Context) {
|
||||
for _, p := range tmpl.Params {
|
||||
if !p.CanEval() {
|
||||
continue // allow.
|
||||
@@ -46,6 +53,4 @@ func MakeHandler(tmpl *macro.Template) (handler context.Handler, needsMacroHandl
|
||||
// if all passed, just continue.
|
||||
ctx.Next()
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/kataras/iris/macro"
|
||||
)
|
||||
|
||||
func TestMakeHandlerNeeds(t *testing.T) {
|
||||
func TestCanMakeHandler(t *testing.T) {
|
||||
tests := []struct {
|
||||
src string
|
||||
needsHandler bool
|
||||
@@ -29,13 +29,13 @@ func TestMakeHandlerNeeds(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("[%d] '%s' failed to be parsed: %v", i, tt.src, err)
|
||||
}
|
||||
if _, got := MakeHandler(tmpl); got != tt.needsHandler {
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,18 +95,17 @@ func (p *TemplateParam) Eval(paramValue string, paramChanger memstore.ValueSette
|
||||
// and returns a new Template.
|
||||
// It builds all the parameter functions for that template
|
||||
// and their evaluators, it's the api call that makes use the interpeter's parser -> lexer.
|
||||
func Parse(src string, macros Macros) (*Template, error) {
|
||||
func Parse(src string, macros Macros) (Template, error) {
|
||||
types := make([]ast.ParamType, len(macros))
|
||||
for i, m := range macros {
|
||||
types[i] = m
|
||||
}
|
||||
|
||||
tmpl := Template{Src: src}
|
||||
params, err := parser.Parse(src, types)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return tmpl, err
|
||||
}
|
||||
t := new(Template)
|
||||
t.Src = src
|
||||
|
||||
for idx, p := range params {
|
||||
m := macros.Lookup(p.Type)
|
||||
@@ -140,8 +139,8 @@ func Parse(src string, macros Macros) (*Template, error) {
|
||||
tmplParam.Funcs = append(tmplParam.Funcs, evalFn)
|
||||
}
|
||||
|
||||
t.Params = append(t.Params, tmplParam.preComputed())
|
||||
tmpl.Params = append(tmpl.Params, tmplParam.preComputed())
|
||||
}
|
||||
|
||||
return t, nil
|
||||
return tmpl, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user