2017-08-27 17:46:04 +02:00
|
|
|
package methodfunc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2017-09-15 14:05:35 +02:00
|
|
|
|
|
|
|
"github.com/kataras/iris/context"
|
2017-10-09 14:26:46 +02:00
|
|
|
"github.com/kataras/iris/core/errors"
|
2017-08-27 17:46:04 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// MethodFunc the handler function.
|
|
|
|
type MethodFunc struct {
|
|
|
|
FuncInfo
|
2017-09-15 14:05:35 +02:00
|
|
|
// MethodCall fires the actual handler.
|
|
|
|
// The "ctx" is the current context, helps us to get any path parameter's values.
|
|
|
|
//
|
|
|
|
// The "f" is the controller's function which is responsible
|
|
|
|
// for that request for this http method.
|
|
|
|
// That function can accept one parameter.
|
|
|
|
//
|
|
|
|
// The default callers (and the only one for now)
|
|
|
|
// are pre-calculated by the framework.
|
|
|
|
MethodCall func(ctx context.Context, f reflect.Value)
|
|
|
|
RelPath string
|
2017-08-27 17:46:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Resolve returns all the method funcs
|
|
|
|
// necessary information and actions to
|
|
|
|
// perform the request.
|
2017-10-09 14:26:46 +02:00
|
|
|
func Resolve(typ reflect.Type) ([]MethodFunc, error) {
|
|
|
|
r := errors.NewReporter()
|
|
|
|
var methodFuncs []MethodFunc
|
2017-08-27 17:46:04 +02:00
|
|
|
infos := fetchInfos(typ)
|
|
|
|
for _, info := range infos {
|
2017-11-27 20:39:57 +01:00
|
|
|
methodFunc, err := ResolveMethodFunc(info)
|
2017-10-09 14:26:46 +02:00
|
|
|
if r.AddErr(err) {
|
2017-08-27 17:46:04 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
methodFuncs = append(methodFuncs, methodFunc)
|
|
|
|
}
|
|
|
|
|
2017-10-09 14:26:46 +02:00
|
|
|
return methodFuncs, r.Return()
|
2017-08-27 17:46:04 +02:00
|
|
|
}
|
2017-11-27 20:39:57 +01:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|