MVC: Add support for ByBy as requested https://github.com/kataras/iris/issues/751

Former-commit-id: 78c2453c56000c2268e11e68d7b530251e2beedd
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-09-22 15:16:24 +03:00
parent ff8f96d56d
commit ab8235d398
2 changed files with 18 additions and 6 deletions

View File

@ -70,14 +70,23 @@ func (p *funcParser) parse() (*ast, error) {
typ := p.info.Type typ := p.info.Type
funcArgPos++ // starting with 1 because in typ.NumIn() the first is the struct receiver. 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 // No need for these:
a.relPath += "/" + strings.ToLower(w) // ByBy will act like /{param:type}/{param:type} as users expected
continue // 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 { if typ.NumIn() <= funcArgPos {
return nil, errors.New("keyword 'By' found but length of input receivers are not match for " + // old:
p.info.Name) // 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 ( var (

View File

@ -456,6 +456,7 @@ 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) GetSomethingByElseThisBy(bool, int) {} // two input arguments func (c *testControllerRelPathFromFunc) GetSomethingByElseThisBy(bool, int) {} // two input arguments
func TestControllerRelPathFromFunc(t *testing.T) { func TestControllerRelPathFromFunc(t *testing.T) {
@ -474,6 +475,8 @@ func TestControllerRelPathFromFunc(t *testing.T) {
Body().Equal("GET:/something/false") Body().Equal("GET:/something/false")
e.GET("/something/truee").Expect().Status(httptest.StatusNotFound) e.GET("/something/truee").Expect().Status(httptest.StatusNotFound)
e.GET("/something/falsee").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). e.GET("/something/true/else/this/42").Expect().Status(httptest.StatusOK).
Body().Equal("GET:/something/true/else/this/42") Body().Equal("GET:/something/true/else/this/42")