/
ActionMethodExecutor.go
63 lines (52 loc) 路 1.51 KB
/
ActionMethodExecutor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package Controller
import (
"github.com/maxzhang1985/yoyogo/Context"
"github.com/maxzhang1985/yoyogo/Utils"
"reflect"
)
type ActionMethodExecutor struct {
}
func NewActionMethodExecutor() ActionMethodExecutor {
return ActionMethodExecutor{}
}
func (actionExecutor ActionMethodExecutor) Execute(ctx *ActionExecutorContext) interface{} {
if ctx.Controller != nil {
if ctx.In.MethodInovker == nil {
ctx.In.MethodInovker = Utils.NewMethodCaller(ctx.Controller, ctx.ActionName)
ctx.In.ActionParamTypes = ctx.In.MethodInovker.GetParamTypes()
}
values := getParamValues(ctx.In.ActionParamTypes, ctx.Context)
returns := ctx.In.MethodInovker.Invoke(values...)
if len(returns) > 0 {
responseData := returns[0]
return responseData
}
}
return nil
}
func getParamValues(paramTypes []reflect.Type, ctx *Context.HttpContext) []interface{} {
if len(paramTypes) == 0 {
return nil
}
values := make([]interface{}, len(paramTypes))
for index, paramType := range paramTypes {
if paramType.Kind() == reflect.Ptr {
paramType = paramType.Elem()
}
if paramType.Kind() == reflect.Struct {
switch paramType.Name() {
case "HttpContext":
values[index] = ctx
default:
if paramType.NumField() > 0 && paramType.Field(0).Name == "RequestBody" {
reqBindingData := reflect.New(paramType).Interface()
_ = ctx.Bind(reqBindingData)
values[index] = reqBindingData
}
}
}
}
return values
}
func RequestParamTypeConvertFunc(index int, paramType reflect.Type, ctx *Context.HttpContext) {
}