1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 09:57:01 +00:00

DBUG API: mark overlapped routes, hide the builtin functionality (read description)

also reference the correct file:line on <autogenerated> (by searching for the file,line of method inside the embedded fields themselves, as go automatically generates the methods foreach struct for their embedded fields)

fix UseGlobal may override the overlap feature
This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-08-28 16:02:14 +03:00
parent 0b31b50404
commit 6f9a453160
6 changed files with 80 additions and 21 deletions

View File

@@ -415,13 +415,19 @@ func (c *ControllerActivator) handleMany(method, path, funcName string, override
}
func (c *ControllerActivator) saveRoutes(funcName string, routes []*router.Route, override bool) {
m, ok := c.Type.MethodByName(funcName)
if !ok {
return
}
sourceFileName, sourceLineNumber := getSourceFileLine(c.Type, m)
relName := c.RelName()
for _, r := range routes {
r.Description = relName
r.MainHandlerName = fmt.Sprintf("%s.%s", relName, funcName)
if m, ok := c.Type.MethodByName(funcName); ok {
r.SourceFileName, r.SourceLineNumber = context.HandlerFileLineRel(m.Func)
}
r.SourceFileName, r.SourceLineNumber = sourceFileName, sourceLineNumber
}
// add this as a reserved method name in order to

View File

@@ -2,6 +2,8 @@ package mvc
import (
"reflect"
"github.com/kataras/iris/v12/context"
)
var baseControllerTyp = reflect.TypeOf((*BaseController)(nil)).Elem()
@@ -20,3 +22,37 @@ func indirectType(typ reflect.Type) reflect.Type {
}
return typ
}
func getSourceFileLine(ctrlType reflect.Type, m reflect.Method) (string, int) { // used for debug logs.
sourceFileName, sourceLineNumber := context.HandlerFileLineRel(m.Func)
if sourceFileName == "<autogenerated>" {
elem := indirectType(ctrlType)
for i, n := 0, elem.NumField(); i < n; i++ {
if f := elem.Field(i); f.Anonymous {
typ := indirectType(f.Type)
if typ.Kind() != reflect.Struct {
continue // field is not a struct.
}
// why we do that?
// because if the element is not Ptr
// then it's probably used as:
// type ctrl {
// BaseCtrl
// }
// but BaseCtrl has not the method, *BaseCtrl does:
// (c *BaseCtrl) HandleHTTPError(...)
// so we are creating a new temporarly value ptr of that type
// and searching inside it for the method instead.
typ = reflect.New(typ).Type()
if embeddedMethod, ok := typ.MethodByName(m.Name); ok {
sourceFileName, sourceLineNumber = context.HandlerFileLineRel(embeddedMethod.Func)
}
}
}
}
return sourceFileName, sourceLineNumber
}