add support for b.HandleMany(GET, /custom3/{ps:string}/{pssecond:string} /custom3/{ps:string}, CtrlMethodName) relative to: https://github.com/kataras/iris/issues/1292

Former-commit-id: de08c5eeab7a1c2729fbff7260de00cc2516f78c
This commit is contained in:
Gerasimos (Makis) Maropoulos 2019-07-11 16:33:20 +03:00
parent f8d19b3ed2
commit 657e0133d0
3 changed files with 67 additions and 0 deletions

View File

@ -102,14 +102,24 @@ var (
// } // }
// //
// Read https://github.com/kataras/iris/tree/master/_examples/routing/macros for more details. // Read https://github.com/kataras/iris/tree/master/_examples/routing/macros for more details.
// Checks for total available request parameters length
// and parameter index based on the hero/mvc function added
// in order to support the MVC.HandleMany("GET", "/path/{ps}/{pssecond} /path/{ps}")
// when on the second requested path, the 'pssecond' should be empty.
ParamResolvers = map[reflect.Type]func(paramIndex int) interface{}{ ParamResolvers = map[reflect.Type]func(paramIndex int) interface{}{
reflect.TypeOf(""): func(paramIndex int) interface{} { reflect.TypeOf(""): func(paramIndex int) interface{} {
return func(ctx Context) string { return func(ctx Context) string {
if ctx.Params().Len() <= paramIndex {
return ""
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(string) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(string)
} }
}, },
reflect.TypeOf(int(1)): func(paramIndex int) interface{} { reflect.TypeOf(int(1)): func(paramIndex int) interface{} {
return func(ctx Context) int { return func(ctx Context) int {
if ctx.Params().Len() <= paramIndex {
return 0
}
// v, _ := ctx.Params().GetEntryAt(paramIndex).IntDefault(0) // v, _ := ctx.Params().GetEntryAt(paramIndex).IntDefault(0)
// return v // return v
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int)
@ -117,51 +127,81 @@ var (
}, },
reflect.TypeOf(int8(1)): func(paramIndex int) interface{} { reflect.TypeOf(int8(1)): func(paramIndex int) interface{} {
return func(ctx Context) int8 { return func(ctx Context) int8 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int8) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int8)
} }
}, },
reflect.TypeOf(int16(1)): func(paramIndex int) interface{} { reflect.TypeOf(int16(1)): func(paramIndex int) interface{} {
return func(ctx Context) int16 { return func(ctx Context) int16 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int16) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int16)
} }
}, },
reflect.TypeOf(int32(1)): func(paramIndex int) interface{} { reflect.TypeOf(int32(1)): func(paramIndex int) interface{} {
return func(ctx Context) int32 { return func(ctx Context) int32 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int32) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int32)
} }
}, },
reflect.TypeOf(int64(1)): func(paramIndex int) interface{} { reflect.TypeOf(int64(1)): func(paramIndex int) interface{} {
return func(ctx Context) int64 { return func(ctx Context) int64 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int64) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(int64)
} }
}, },
reflect.TypeOf(uint(1)): func(paramIndex int) interface{} { reflect.TypeOf(uint(1)): func(paramIndex int) interface{} {
return func(ctx Context) uint { return func(ctx Context) uint {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint)
} }
}, },
reflect.TypeOf(uint8(1)): func(paramIndex int) interface{} { reflect.TypeOf(uint8(1)): func(paramIndex int) interface{} {
return func(ctx Context) uint8 { return func(ctx Context) uint8 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint8) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint8)
} }
}, },
reflect.TypeOf(uint16(1)): func(paramIndex int) interface{} { reflect.TypeOf(uint16(1)): func(paramIndex int) interface{} {
return func(ctx Context) uint16 { return func(ctx Context) uint16 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint16) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint16)
} }
}, },
reflect.TypeOf(uint32(1)): func(paramIndex int) interface{} { reflect.TypeOf(uint32(1)): func(paramIndex int) interface{} {
return func(ctx Context) uint32 { return func(ctx Context) uint32 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint32) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint32)
} }
}, },
reflect.TypeOf(uint64(1)): func(paramIndex int) interface{} { reflect.TypeOf(uint64(1)): func(paramIndex int) interface{} {
return func(ctx Context) uint64 { return func(ctx Context) uint64 {
if ctx.Params().Len() <= paramIndex {
return 0
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint64) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(uint64)
} }
}, },
reflect.TypeOf(true): func(paramIndex int) interface{} { reflect.TypeOf(true): func(paramIndex int) interface{} {
return func(ctx Context) bool { return func(ctx Context) bool {
if ctx.Params().Len() <= paramIndex {
return false
}
return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(bool) return ctx.Params().GetEntryAt(paramIndex).ValueRaw.(bool)
} }
}, },

View File

@ -40,6 +40,13 @@ func (c *testControllerHandle) BeforeActivation(b BeforeActivation) {
b.Handle("GET", "/hiservice/{ps:string}", "HiServiceBy") b.Handle("GET", "/hiservice/{ps:string}", "HiServiceBy")
b.Handle("GET", "/hiparam/{ps:string}", "HiParamBy") b.Handle("GET", "/hiparam/{ps:string}", "HiParamBy")
b.Handle("GET", "/hiparamempyinput/{ps:string}", "HiParamEmptyInputBy") b.Handle("GET", "/hiparamempyinput/{ps:string}", "HiParamEmptyInputBy")
b.HandleMany("GET", "/custom/{ps:string} /custom2/{ps:string}", "CustomWithParameter")
// if dynamic path exist
// then longest path should be registered first
// and the controller's method if wants to add path parameters
// dependency injection then they should accept the longest path parameters.
// See `testControllerHandle.CustomWithParameters`.
b.HandleMany("GET", "/custom3/{ps:string}/{pssecond:string} /custom3/{ps:string}", "CustomWithParameters")
} }
// test `GetRoute` for custom routes. // test `GetRoute` for custom routes.
@ -97,6 +104,16 @@ func (c *testControllerHandle) HiParamEmptyInputBy() string {
return "empty in but served with ctx.Params.Get('ps')=" + c.Ctx.Params().Get("ps") return "empty in but served with ctx.Params.Get('ps')=" + c.Ctx.Params().Get("ps")
} }
func (c *testControllerHandle) CustomWithParameter(param1 string) string {
return param1
}
func (c *testControllerHandle) CustomWithParameters(param1, param2 string) string {
// it returns empty string for requested path: /custom3/value1,
// see BeforeActivation.
return param1 + param2
}
type testSmallController struct{} type testSmallController struct{}
// test ctx + id in the same time. // test ctx + id in the same time.
@ -137,6 +154,15 @@ func TestControllerHandle(t *testing.T) {
Body().Equal("value") Body().Equal("value")
e.GET("/hiparamempyinput/value").Expect().Status(httptest.StatusOK). e.GET("/hiparamempyinput/value").Expect().Status(httptest.StatusOK).
Body().Equal("empty in but served with ctx.Params.Get('ps')=value") Body().Equal("empty in but served with ctx.Params.Get('ps')=value")
e.GET("/custom/value1").Expect().Status(httptest.StatusOK).
Body().Equal("value1")
e.GET("/custom2/value2").Expect().Status(httptest.StatusOK).
Body().Equal("value2")
e.GET("/custom3/value1/value2").Expect().Status(httptest.StatusOK).
Body().Equal("value1value2")
e.GET("/custom3/value1").Expect().Status(httptest.StatusOK).
Body().Equal("value1")
e.GET("/hi/param/empty/input/with/ctx/value").Expect().Status(httptest.StatusOK). e.GET("/hi/param/empty/input/with/ctx/value").Expect().Status(httptest.StatusOK).
Body().Equal("empty in but served with ctx.Params.Get('param2')= value == id == value") Body().Equal("empty in but served with ctx.Params.Get('param2')= value == id == value")
} }

View File

@ -41,6 +41,7 @@ func getPathParamsForInput(params []macro.TemplateParam, funcIn ...reflect.Type)
if _, ok := consumed[j]; ok { if _, ok := consumed[j]; ok {
continue continue
} }
funcDep, ok := context.ParamResolverByTypeAndIndex(in, param.Index) funcDep, ok := context.ParamResolverByTypeAndIndex(in, param.Index)
if !ok { if !ok {
continue continue