mirror of
https://github.com/kataras/iris.git
synced 2025-12-27 14:57:05 +00:00
add CustomPathWordFunc
This commit is contained in:
@@ -318,7 +318,7 @@ func (c *ControllerActivator) parseMethods() {
|
||||
}
|
||||
|
||||
func (c *ControllerActivator) parseMethod(m reflect.Method) {
|
||||
httpMethod, httpPath, err := parseMethod(c.app.Router.Macros(), m, c.isReservedMethod)
|
||||
httpMethod, httpPath, err := parseMethod(c.app.Router.Macros(), m, c.isReservedMethod,c.app.customPathWordFunc)
|
||||
if err != nil {
|
||||
if err != errSkip {
|
||||
c.logErrorf("MVC: fail to parse the route path and HTTP method for '%s.%s': %v", c.fullName, m.Name, err)
|
||||
|
||||
@@ -95,9 +95,10 @@ type methodParser struct {
|
||||
lexer *methodLexer
|
||||
fn reflect.Method
|
||||
macros *macro.Macros
|
||||
customPathWordFunc CustomPathWordFunc
|
||||
}
|
||||
|
||||
func parseMethod(macros *macro.Macros, fn reflect.Method, skipper func(string) bool) (method, path string, err error) {
|
||||
func parseMethod(macros *macro.Macros, fn reflect.Method, skipper func(string) bool,wordFunc CustomPathWordFunc) (method, path string, err error) {
|
||||
if skipper(fn.Name) {
|
||||
return "", "", errSkip
|
||||
}
|
||||
@@ -106,6 +107,7 @@ func parseMethod(macros *macro.Macros, fn reflect.Method, skipper func(string) b
|
||||
fn: fn,
|
||||
lexer: newMethodLexer(fn.Name),
|
||||
macros: macros,
|
||||
customPathWordFunc: wordFunc,
|
||||
}
|
||||
return p.parse()
|
||||
}
|
||||
@@ -119,6 +121,8 @@ var errSkip = errors.New("skip")
|
||||
|
||||
var allMethods = append(router.AllMethods[0:], []string{"ALL", "ANY"}...)
|
||||
|
||||
type CustomPathWordFunc func(path, w string,wordIndex int) string
|
||||
|
||||
func addPathWord(path, w string) string {
|
||||
if path[len(path)-1] != '/' {
|
||||
path += "/"
|
||||
@@ -147,6 +151,7 @@ func (p *methodParser) parse() (method, path string, err error) {
|
||||
return "", "", errSkip
|
||||
}
|
||||
|
||||
wordIndex:=0
|
||||
for {
|
||||
w := p.lexer.next()
|
||||
if w == "" {
|
||||
@@ -171,8 +176,15 @@ func (p *methodParser) parse() (method, path string, err error) {
|
||||
|
||||
continue
|
||||
}
|
||||
// static path.
|
||||
path = addPathWord(path, w)
|
||||
|
||||
// custom static path.
|
||||
if p.customPathWordFunc!=nil {
|
||||
path = p.customPathWordFunc(path, w,wordIndex)
|
||||
}else{
|
||||
// default static path.
|
||||
path = addPathWord(path, w)
|
||||
}
|
||||
wordIndex++
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -37,6 +37,9 @@ type Application struct {
|
||||
// Disables verbose logging for controllers under this and its children mvc apps.
|
||||
// Defaults to false.
|
||||
controllersNoLog bool
|
||||
|
||||
// Set custom path
|
||||
customPathWordFunc CustomPathWordFunc
|
||||
}
|
||||
|
||||
func newApp(subRouter router.Party, container *hero.Container) *Application {
|
||||
@@ -119,6 +122,11 @@ func (app *Application) SetName(appName string) *Application {
|
||||
return app
|
||||
}
|
||||
|
||||
func (app *Application) SetCustomPathWordFunc(wordFunc CustomPathWordFunc) *Application {
|
||||
app.customPathWordFunc = wordFunc
|
||||
return app
|
||||
}
|
||||
|
||||
// SetControllersNoLog disables verbose logging for next registered controllers
|
||||
// under this App and its children of `Application.Party` or `Application.Clone`.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user