diff --git a/_examples/routing/macros/main.go b/_examples/routing/macros/main.go index 8548da88..1afaf005 100644 --- a/_examples/routing/macros/main.go +++ b/_examples/routing/macros/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" "reflect" - "strconv" "github.com/kataras/iris" "github.com/kataras/iris/context" @@ -37,39 +36,40 @@ func main() { ^ Done 27 sep 2018. */ - app.Macros().Register("uint32", "small", false, false, func(paramValue string) (interface{}, bool) { - v, err := strconv.ParseUint(paramValue, 10, 32) - return uint32(v), err == nil - }). - RegisterFunc("min", func(min uint32) func(uint32) bool { - return func(paramValue uint32) bool { - return paramValue >= min - } - }) + // app.Macros().Register("uint32", "small", false, false, func(paramValue string) (interface{}, bool) { + // v, err := strconv.ParseUint(paramValue, 10, 32) + // return uint32(v), err == nil + // }). + // RegisterFunc("min", func(min uint32) func(uint32) bool { + // return func(paramValue uint32) bool { + // return paramValue >= min + // } + // }) - // optionally, only when mvc or hero features are used for this custom macro/parameter type. - context.ParamResolvers[reflect.Uint32] = func(paramIndex int) interface{} { - /* both works but second is faster, we omit the duplication of the type conversion over and over as of 27 Sep of 2018 (this patch)*/ - // return func(ctx context.Context) uint32 { - // param := ctx.Params().GetEntryAt(paramIndex) - // paramValueAsUint32, _ := strconv.ParseUint(param.String(), 10, 32) - // return uint32(paramValueAsUint32) - // } - return func(ctx context.Context) uint32 { - return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint32) - } /* TODO: find a way to automative it based on the macro's first return value type, if thats the case then we must not return nil even if not found, - we must return a value i.e 0 for int for its interface{} */ - } - // + // // optionally, only when mvc or hero features are used for this custom macro/parameter type. + // context.ParamResolvers[reflect.Uint32] = func(paramIndex int) interface{} { + // /* both works but second is faster, we omit the duplication of the type conversion over and over as of 27 Sep of 2018 (this patch)*/ + // // return func(ctx context.Context) uint32 { + // // param := ctx.Params().GetEntryAt(paramIndex) + // // paramValueAsUint32, _ := strconv.ParseUint(param.String(), 10, 32) + // // return uint32(paramValueAsUint32) + // // } + // return func(ctx context.Context) uint32 { + // return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint32) + // } /* TODO: find a way to automative it based on the macro's first return value type, if thats the case then we must not return nil even if not found, + // we must return a value i.e 0 for int for its interface{} */ + // } + // // - app.Get("/test_uint32/{myparam:uint32 min(10)}", hero.Handler(func(paramValue uint32) string { - return fmt.Sprintf("Value of the parameter is: %d\n", paramValue) + app.Get("/test_uint32/{myparam1:string}/{myparam2:uint32 min(10)}", hero.Handler(func(myparam1 string, myparam2 uint32) string { + return fmt.Sprintf("Value of the parameters are: %s:%d\n", myparam1, myparam2) })) - app.Get("test_uint64/{myparam:uint64 min(5)}", func(ctx context.Context) { + app.Get("test_uint64/{myparam1:string}/{myparam2:uint64}", func(ctx context.Context) { // works: ctx.Writef("Value of the parameter is: %s\n", ctx.Params().Get("myparam")) // but better and faster because the macro converts the string to uint64 automatically: - ctx.Writef("Value of the parameter is: %d\n", ctx.Params().GetUint64Default("myparam", 0)) + println("type of myparam2 (should be uint64) is: " + reflect.ValueOf(ctx.Params().GetEntry("myparam2").ValueRaw).Kind().String()) + ctx.Writef("Value of the parameters are: %s:%d\n", ctx.Params().Get("myparam1"), ctx.Params().GetUint64Default("myparam2", 0)) }) app.Run(iris.Addr(":8080")) diff --git a/core/router/macro.go b/core/router/macro.go index 0a84f67f..dfbf39be 100644 --- a/core/router/macro.go +++ b/core/router/macro.go @@ -85,8 +85,7 @@ func convertTmplToHandler(tmpl *macro.Template) context.Handler { for _, p := range tmpl.Params { if p.TypeEvaluator == nil { // allow. - ctx.Next() - return + continue } // first, check for type evaluator. diff --git a/mvc/controller_test.go b/mvc/controller_test.go index f367d0ea..7103c9e9 100644 --- a/mvc/controller_test.go +++ b/mvc/controller_test.go @@ -377,8 +377,10 @@ func (c *testControllerRelPathFromFunc) GetAdminLogin() {} func (c *testControllerRelPathFromFunc) PutSomethingIntoThis() {} -func (c *testControllerRelPathFromFunc) GetSomethingBy(bool) {} -func (c *testControllerRelPathFromFunc) GetSomethingByBy(string, int) {} +func (c *testControllerRelPathFromFunc) GetSomethingBy(bool) {} + +func (c *testControllerRelPathFromFunc) GetSomethingByBy(string, int) {} + func (c *testControllerRelPathFromFunc) GetSomethingNewBy(string, int) {} // two input arguments, one By which is the latest word. func (c *testControllerRelPathFromFunc) GetSomethingByElseThisBy(bool, int) {} // two input arguments