iris/mvc/activator/methodfunc/func_caller.go
Gerasimos (Makis) Maropoulos 69b5327ecc Update to Version 8.4.2 | Read HISTORY.md
https://github.com/kataras/iris/blob/master/HISTORY.md#fr-15-september-2017--v842

Former-commit-id: 0ee4cc1d93ef7f26e5d402fdfbe07062aff5b08c
2017-09-15 15:05:35 +03:00

63 lines
1.7 KiB
Go

package methodfunc
import (
"reflect"
"github.com/kataras/iris/context"
)
// buildMethodCall builds the method caller.
// We have repeated code here but it's the only way
// to support more than one input arguments without performance cost compared to previous implementation.
// so it's hard-coded written to check the length of input args and their types.
func buildMethodCall(a *ast) func(ctx context.Context, f reflect.Value) {
// if accepts one or more parameters.
if a.dynamic {
// if one function input argument then call the function
// by "casting" (faster).
if l := len(a.paramKeys); l == 1 {
paramType := a.paramTypes[0]
paramKey := a.paramKeys[0]
if paramType == paramTypeInt {
return func(ctx context.Context, f reflect.Value) {
v, _ := ctx.Params().GetInt(paramKey)
f.Interface().(func(int))(v)
}
}
if paramType == paramTypeLong {
return func(ctx context.Context, f reflect.Value) {
v, _ := ctx.Params().GetInt64(paramKey)
f.Interface().(func(int64))(v)
}
}
if paramType == paramTypeBoolean {
return func(ctx context.Context, f reflect.Value) {
v, _ := ctx.Params().GetBool(paramKey)
f.Interface().(func(bool))(v)
}
}
// string, path...
return func(ctx context.Context, f reflect.Value) {
f.Interface().(func(string))(ctx.Params().Get(paramKey))
}
}
// if func input arguments are more than one then
// use the Call method (slower).
return func(ctx context.Context, f reflect.Value) {
f.Call(a.paramValues(ctx))
}
}
// if it's static without any receivers then just call it.
return func(ctx context.Context, f reflect.Value) {
f.Interface().(func())()
}
}