easy fix of macro handler caused tests to fail by before prev commit

Former-commit-id: 32f67072f604935a8efecc90151715f27ba7c2c1
This commit is contained in:
Gerasimos (Makis) Maropoulos 2018-09-27 21:31:07 +03:00
parent 39b180b14c
commit 972dff8729
3 changed files with 33 additions and 32 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"strconv"
"github.com/kataras/iris" "github.com/kataras/iris"
"github.com/kataras/iris/context" "github.com/kataras/iris/context"
@ -37,39 +36,40 @@ func main() {
^ Done 27 sep 2018. ^ Done 27 sep 2018.
*/ */
app.Macros().Register("uint32", "small", false, false, func(paramValue string) (interface{}, bool) { // app.Macros().Register("uint32", "small", false, false, func(paramValue string) (interface{}, bool) {
v, err := strconv.ParseUint(paramValue, 10, 32) // v, err := strconv.ParseUint(paramValue, 10, 32)
return uint32(v), err == nil // return uint32(v), err == nil
}). // }).
RegisterFunc("min", func(min uint32) func(uint32) bool { // RegisterFunc("min", func(min uint32) func(uint32) bool {
return func(paramValue uint32) bool { // return func(paramValue uint32) bool {
return paramValue >= min // 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{} */
}
//
app.Get("/test_uint32/{myparam:uint32 min(10)}", hero.Handler(func(paramValue uint32) string { // // optionally, only when mvc or hero features are used for this custom macro/parameter type.
return fmt.Sprintf("Value of the parameter is: %d\n", paramValue) // 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/{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")) // 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: // 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")) app.Run(iris.Addr(":8080"))

View File

@ -85,8 +85,7 @@ func convertTmplToHandler(tmpl *macro.Template) context.Handler {
for _, p := range tmpl.Params { for _, p := range tmpl.Params {
if p.TypeEvaluator == nil { if p.TypeEvaluator == nil {
// allow. // allow.
ctx.Next() continue
return
} }
// first, check for type evaluator. // first, check for type evaluator.

View File

@ -378,7 +378,9 @@ func (c *testControllerRelPathFromFunc) GetAdminLogin() {}
func (c *testControllerRelPathFromFunc) PutSomethingIntoThis() {} func (c *testControllerRelPathFromFunc) PutSomethingIntoThis() {}
func (c *testControllerRelPathFromFunc) GetSomethingBy(bool) {} func (c *testControllerRelPathFromFunc) GetSomethingBy(bool) {}
func (c *testControllerRelPathFromFunc) GetSomethingByBy(string, int) {} 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) GetSomethingNewBy(string, int) {} // two input arguments, one By which is the latest word.
func (c *testControllerRelPathFromFunc) GetSomethingByElseThisBy(bool, int) {} // two input arguments func (c *testControllerRelPathFromFunc) GetSomethingByElseThisBy(bool, int) {} // two input arguments