-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
212 lines (193 loc) · 5.78 KB
/
controller.go
File metadata and controls
212 lines (193 loc) · 5.78 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package peony
import (
"code.google.com/p/go.net/websocket"
"errors"
"fmt"
"net/http"
"reflect"
)
var (
WebSocketConnType = reflect.TypeOf((*websocket.Conn)(nil))
RequestType = reflect.TypeOf((*Request)(nil))
ErrorType = reflect.TypeOf((*error)(nil)).Elem()
ResponseType = reflect.TypeOf((*Response)(nil))
SessionType = reflect.TypeOf((*Session)(nil))
ControllerType = reflect.TypeOf((*Controller)(nil))
AppType = reflect.TypeOf((*App)(nil))
FlashType = reflect.TypeOf((*Flash)(nil))
NotFunc = errors.New("action should be a func")
NotMethod = errors.New("action should be a method")
ValueMustbePtr = errors.New("value must be should be ptr")
)
type Controller struct {
Resp *Response
Req *Request
Session *Session
Server *Server
Flash *Flash
Params *Params
actionName string
action *Action
render Renderer
templateLoader *TemplateLoader
}
var ControllerPtrType = reflect.TypeOf((*Controller)(nil))
type Action struct {
Name string
call reflect.Value
function interface{} //if Action is function, not nil
method interface{} //if Action is method, not nil
targetType reflect.Type //if is method action, targetType is recv type. e.g. (*X).DO *X is the targetType
targetPtr reflect.Value // the ptr value for targetType, always is a ptr value
Args []*ArgType
}
func (c *Controller) SetCookie(cookie *http.Cookie) {
http.SetCookie(c.Resp, cookie)
}
//Get Param Value
func (c *Controller) GetParam(name string, val interface{}) error {
valType := reflect.TypeOf(val)
if valType.Kind() != reflect.Ptr {
return ValueMustbePtr
}
value := reflect.ValueOf(val)
paramValue := c.Server.convertors.Convert(c.Params, name, valType)
value.Elem().Set(paramValue.Elem())
return nil
}
func (a *Action) Invoke(args []reflect.Value) []reflect.Value {
var callArgs []reflect.Value
if a.method != nil {
target := a.targetPtr
if a.targetType.Kind() != reflect.Ptr {
target = target.Elem()
}
callArgs = append(append(callArgs, target), args...)
} else {
callArgs = append(callArgs, args...)
}
return a.call.Call(callArgs)
}
func (a *Action) Dup() *Action {
newAction := new(Action)
*newAction = *a
if a.method != nil {
var ptr reflect.Value
var targetType = a.targetType
//when targetType is ptr like (*Struct).Call, the first arguments should be ptr's elem
if targetType.Kind() == reflect.Ptr {
ptr = reflect.New(targetType.Elem())
} else {
ptr = reflect.New(targetType)
}
newAction.targetPtr = ptr
}
return newAction
}
type ArgType struct {
Name string // arg name
Type reflect.Type
}
type ActionContainer struct {
Actions map[string]*Action //e.g. key is Controller.Call or Function
FuncActions map[string]*Action //e.g. key is the type
}
func NewActionContainer() *ActionContainer {
actions := &ActionContainer{
Actions: make(map[string]*Action),
FuncActions: make(map[string]*Action),
}
return actions
}
func (a *ActionContainer) FindAction(name string) *Action {
return a.Actions[name]
}
func (a *ActionContainer) RegisterFuncAction(function interface{}, action *Action) error {
funcVal := reflect.ValueOf(function)
funcType := funcVal.Type()
if funcType.Kind() != reflect.Func {
ERROR.Println("registor func action error:", NotFunc)
return NotFunc
}
action.function = function
action.call = funcVal
a.Actions[action.Name] = action
a.FuncActions[fmt.Sprintf("%p", function)] = action
return nil
}
func (a *ActionContainer) FindActionByFunc(i interface{}) *Action {
funcType := reflect.TypeOf(i)
if funcType.Kind() != reflect.Func {
ERROR.Println("registor func action error:", NotFunc)
panic(NotFunc)
}
return a.FuncActions[fmt.Sprintf("%p", i)]
}
func (a *ActionContainer) RegisterMethodAction(method interface{}, action *Action) error {
methodVal := reflect.ValueOf(method)
methodType := methodVal.Type()
numIn := methodType.NumIn()
if numIn < 1 {
ERROR.Println("register method action error:", NotMethod)
return NotMethod
}
targetType := methodType.In(0)
action.method = method
action.call = methodVal
action.targetType = targetType
a.Actions[action.Name] = action
return nil
}
func (c *Controller) NotFound(msg string, args ...interface{}) {
c.render = NotFound(msg, args...)
}
func GetActionInvokeFilter(server *Server) Filter {
return func(controller *Controller, _ []Filter) {
convertors := server.convertors
args := controller.action.Args
callArgs := make([]reflect.Value, 0, len(args))
for _, arg := range args {
var argValue reflect.Value
switch arg.Type {
case ControllerType:
argValue = reflect.ValueOf(controller)
case WebSocketConnType:
argValue = reflect.ValueOf(controller.Req.WSConn)
case RequestType:
argValue = reflect.ValueOf(controller.Req)
case ResponseType:
argValue = reflect.ValueOf(controller.Resp)
case SessionType:
argValue = reflect.ValueOf(controller.Session)
case AppType:
argValue = reflect.ValueOf(controller.Server.App)
case FlashType:
argValue = reflect.ValueOf(controller.Flash)
default:
argValue = convertors.Convert(controller.Params, arg.Name, arg.Type)
}
callArgs = append(callArgs, argValue)
}
rsSlice := controller.action.Invoke(callArgs)
if len(rsSlice) > 0 {
last := rsSlice[len(rsSlice)-1]
if last.Type().Implements(ErrorType) {
lastInterface := last.Interface()
if lastInterface != nil {
controller.render = RenderError(lastInterface.(error))
return
}
}
rsVal := rsSlice[0]
if rsVal.Type().Kind() == reflect.String {
controller.render = RenderText(rsVal.String())
return
} else if rsVal.Type().Implements(RendererType) {
rs := rsVal.Interface()
if rs != nil {
controller.render = rs.(Renderer)
}
}
}
}
}