mirror of
https://github.com/kataras/iris.git
synced 2025-12-24 05:17:03 +00:00
implement a way to add controller functions as handlers with the existing rules respected but it's a bit dirty I will change the implementation and move the mvc2 to mvc and make the api builder's PartyFunc to be a critical part of the controller and the mvc2.Mvc bind values should be also respected to the controller and more
Former-commit-id: e452a916da80d886535b8ae9625d0ba8e2b58d6e
This commit is contained in:
@@ -53,38 +53,49 @@ func fetchInfos(typ reflect.Type) (methods []FuncInfo) {
|
||||
// and add that.
|
||||
for i, n := 0, typ.NumMethod(); i < n; i++ {
|
||||
m := typ.Method(i)
|
||||
name := m.Name
|
||||
|
||||
for _, method := range availableMethods {
|
||||
possibleMethodFuncName := methodTitle(method)
|
||||
|
||||
if strings.Index(name, possibleMethodFuncName) == 0 {
|
||||
trailing := ""
|
||||
// if has chars after the method itself
|
||||
if lname, lmethod := len(name), len(possibleMethodFuncName); lname > lmethod {
|
||||
ch := rune(name[lmethod])
|
||||
// if the next char is upper, otherise just skip the whole func info.
|
||||
if unicode.IsUpper(ch) {
|
||||
trailing = name[lmethod:]
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
methodInfo := FuncInfo{
|
||||
Name: name,
|
||||
Trailing: trailing,
|
||||
Type: m.Type,
|
||||
HTTPMethod: method,
|
||||
Index: m.Index,
|
||||
}
|
||||
methods = append(methods, methodInfo)
|
||||
}
|
||||
if method, ok := FetchFuncInfo(m); ok {
|
||||
methods = append(methods, method)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// FetchFuncInfo returns a FuncInfo based on the method of the controller.
|
||||
func FetchFuncInfo(m reflect.Method) (FuncInfo, bool) {
|
||||
name := m.Name
|
||||
|
||||
for _, method := range availableMethods {
|
||||
possibleMethodFuncName := methodTitle(method)
|
||||
|
||||
if strings.Index(name, possibleMethodFuncName) == 0 {
|
||||
trailing := ""
|
||||
// if has chars after the method itself
|
||||
if lname, lmethod := len(name), len(possibleMethodFuncName); lname > lmethod {
|
||||
ch := rune(name[lmethod])
|
||||
// if the next char is upper, otherise just skip the whole func info.
|
||||
if unicode.IsUpper(ch) {
|
||||
trailing = name[lmethod:]
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
info := FuncInfo{
|
||||
Name: name,
|
||||
Trailing: trailing,
|
||||
Type: m.Type,
|
||||
HTTPMethod: method,
|
||||
Index: m.Index,
|
||||
}
|
||||
return info, true
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
return FuncInfo{}, false
|
||||
}
|
||||
|
||||
func methodTitle(httpMethod string) string {
|
||||
httpMethodFuncName := strings.Title(strings.ToLower(httpMethod))
|
||||
return httpMethodFuncName
|
||||
|
||||
@@ -182,6 +182,7 @@ func (a *ast) paramValues(ctx context.Context) []reflect.Value {
|
||||
|
||||
l := len(a.paramKeys)
|
||||
values := make([]reflect.Value, l, l)
|
||||
|
||||
for i := 0; i < l; i++ {
|
||||
paramKey := a.paramKeys[i]
|
||||
paramType := a.paramTypes[i]
|
||||
|
||||
@@ -31,20 +31,38 @@ func Resolve(typ reflect.Type) ([]MethodFunc, error) {
|
||||
var methodFuncs []MethodFunc
|
||||
infos := fetchInfos(typ)
|
||||
for _, info := range infos {
|
||||
parser := newFuncParser(info)
|
||||
a, err := parser.parse()
|
||||
methodFunc, err := ResolveMethodFunc(info)
|
||||
if r.AddErr(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
methodFunc := MethodFunc{
|
||||
RelPath: a.relPath,
|
||||
FuncInfo: info,
|
||||
MethodCall: buildMethodCall(a),
|
||||
}
|
||||
|
||||
methodFuncs = append(methodFuncs, methodFunc)
|
||||
}
|
||||
|
||||
return methodFuncs, r.Return()
|
||||
}
|
||||
|
||||
// ResolveMethodFunc resolves a single `MethodFunc` from a single `FuncInfo`.
|
||||
func ResolveMethodFunc(info FuncInfo, paramKeys ...string) (MethodFunc, error) {
|
||||
parser := newFuncParser(info)
|
||||
a, err := parser.parse()
|
||||
if err != nil {
|
||||
return MethodFunc{}, err
|
||||
}
|
||||
|
||||
if len(paramKeys) > 0 {
|
||||
a.paramKeys = paramKeys
|
||||
}
|
||||
|
||||
methodFunc := MethodFunc{
|
||||
RelPath: a.relPath,
|
||||
FuncInfo: info,
|
||||
MethodCall: buildMethodCall(a),
|
||||
}
|
||||
|
||||
/* TODO: split the method path and ast param keys, and all that
|
||||
because now we want to use custom param keys but 'paramfirst' is set-ed.
|
||||
|
||||
*/
|
||||
|
||||
return methodFunc, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user