From ab8235d39896a7383c1bf74705472f5a00eb9435 Mon Sep 17 00:00:00 2001 From: "Gerasimos (Makis) Maropoulos" Date: Fri, 22 Sep 2017 15:16:24 +0300 Subject: [PATCH] MVC: Add support for `ByBy` as requested https://github.com/kataras/iris/issues/751 Former-commit-id: 78c2453c56000c2268e11e68d7b530251e2beedd --- mvc/activator/methodfunc/func_parser.go | 21 +++++++++++++++------ mvc/controller_test.go | 3 +++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/mvc/activator/methodfunc/func_parser.go b/mvc/activator/methodfunc/func_parser.go index cedc716f..f87e928c 100644 --- a/mvc/activator/methodfunc/func_parser.go +++ b/mvc/activator/methodfunc/func_parser.go @@ -70,14 +70,23 @@ func (p *funcParser) parse() (*ast, error) { typ := p.info.Type funcArgPos++ // starting with 1 because in typ.NumIn() the first is the struct receiver. - if p.lexer.peekPrev() == tokenBy || typ.NumIn() == 1 { // ByBy, then act this second By like a path - a.relPath += "/" + strings.ToLower(w) - continue - } + // No need for these: + // ByBy will act like /{param:type}/{param:type} as users expected + // if func input arguments are there, else act By like normal path /by. + // + // if p.lexer.peekPrev() == tokenBy || typ.NumIn() == 1 { // ByBy, then act this second By like a path + // a.relPath += "/" + strings.ToLower(w) + // continue + // } if typ.NumIn() <= funcArgPos { - return nil, errors.New("keyword 'By' found but length of input receivers are not match for " + - p.info.Name) + // old: + // return nil, errors.New("keyword 'By' found but length of input receivers are not match for " + + // p.info.Name) + + // By found but input arguments are not there, so act like /by path without restricts. + a.relPath += "/" + strings.ToLower(w) + continue } var ( diff --git a/mvc/controller_test.go b/mvc/controller_test.go index a7d3d17e..4d81525b 100644 --- a/mvc/controller_test.go +++ b/mvc/controller_test.go @@ -456,6 +456,7 @@ func (c *testControllerRelPathFromFunc) GetAdminLogin() {} func (c *testControllerRelPathFromFunc) PutSomethingIntoThis() {} func (c *testControllerRelPathFromFunc) GetSomethingBy(bool) {} +func (c *testControllerRelPathFromFunc) GetSomethingByBy(string, int) {} func (c *testControllerRelPathFromFunc) GetSomethingByElseThisBy(bool, int) {} // two input arguments func TestControllerRelPathFromFunc(t *testing.T) { @@ -474,6 +475,8 @@ func TestControllerRelPathFromFunc(t *testing.T) { Body().Equal("GET:/something/false") e.GET("/something/truee").Expect().Status(httptest.StatusNotFound) e.GET("/something/falsee").Expect().Status(httptest.StatusNotFound) + e.GET("/something/kataras/42").Expect().Status(httptest.StatusOK). + Body().Equal("GET:/something/kataras/42") e.GET("/something/true/else/this/42").Expect().Status(httptest.StatusOK). Body().Equal("GET:/something/true/else/this/42")