-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgen_base.go
212 lines (170 loc) · 4.43 KB
/
gen_base.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
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 fastapi
import (
"fmt"
"path/filepath"
"reflect"
"github.com/funny/link"
)
var sessionType = reflect.TypeOf((*link.Session)(nil))
type APIs map[byte][2]interface{}
type Provider interface {
APIs() APIs
}
func (app *App) Register(id byte, service Provider) {
typeOfService := reflect.TypeOf(service)
if app.services[id] != nil {
panic(fmt.Sprintf("duplicate service id '%d' for '%s' and '%s'", id, typeOfService, app.services[id]))
}
for _, serviceType := range app.serviceTypes {
if serviceType.t == typeOfService {
panic(fmt.Sprintf("duplicate register in '%s'", typeOfService))
}
}
app.services[id] = service
serviceType := &ServiceType{
id: id,
t: typeOfService,
}
for id, api := range service.APIs() {
if api[0] != nil {
serviceType.registerReq(id, api[0])
}
if api[1] != nil {
serviceType.registerRsp(id, api[1])
}
}
app.serviceTypes = append(app.serviceTypes, serviceType)
}
func (app *App) ServiceTypes() []*ServiceType {
return app.serviceTypes
}
type ServiceType struct {
id byte
t reflect.Type
requests []*MessageType
responses []*MessageType
handlers []*HandlerMethod
}
func (service *ServiceType) registerReq(id byte, req interface{}) {
reqType := reflect.TypeOf(req)
if reqType.Kind() == reflect.Ptr {
reqType = reqType.Elem()
}
for _, req := range service.requests {
if req.t == reqType {
panic(fmt.Sprintf("duplicate register request type '%s'", reqType))
}
}
service.requests = append(service.requests, &MessageType{
service: service,
id: id,
t: reqType,
})
// Search Request Handler:
//
// HandleRequest(session *MySession, req *MyRequest) *MyResponse
// HandleRequest(req *MyRequest) *MyResponse
// HandleRequest(session *MySession, req *MyRequest)
// HandleRequest(req *MyRequest)
//
for i := 0; i < service.t.NumMethod(); i++ {
method := service.t.Method(i)
if n := method.Type.NumOut(); n != 0 && n != 1 {
continue
}
if n := method.Type.NumIn() - 1; n != 1 && n != 2 {
continue
}
if lastArg := method.Type.In(method.Type.NumIn() - 1); lastArg.Kind() != reflect.Ptr || lastArg.Elem() != reqType {
continue
}
var rspType reflect.Type
if method.Type.NumOut() == 1 {
rspType = method.Type.Out(0)
}
service.handlers = append(service.handlers, &HandlerMethod{
ID: id,
Name: method.Name,
ReqType: reqType,
RspType: rspType,
NeedSession: method.Type.In(1) == sessionType,
})
break
}
}
func (service *ServiceType) registerRsp(id byte, rsp interface{}) {
rspType := reflect.TypeOf(rsp)
if rspType.Kind() == reflect.Ptr {
rspType = rspType.Elem()
}
for _, rsp := range service.responses {
if rsp.t == rspType {
panic(fmt.Sprintf("duplicate register response type '%s'", rspType))
}
}
service.responses = append(service.responses, &MessageType{
service: service,
id: id,
t: rspType,
})
}
func (service *ServiceType) ID() byte {
return service.id
}
func (service *ServiceType) Type() reflect.Type {
return service.t.Elem()
}
func (service *ServiceType) Package() string {
return filepath.Base(service.t.PkgPath())
}
func (service *ServiceType) Name() string {
return service.t.Elem().Name()
}
func (service *ServiceType) Requests() []*MessageType {
return service.requests
}
func (service *ServiceType) Responses() []*MessageType {
return service.responses
}
func (service *ServiceType) Handlers() []*HandlerMethod {
return service.handlers
}
type MessageType struct {
service *ServiceType
id byte
t reflect.Type
}
func (msg *MessageType) Service() *ServiceType {
return msg.service
}
func (msg *MessageType) ID() byte {
return msg.id
}
func (msg *MessageType) Type() reflect.Type {
return msg.t
}
func (msg *MessageType) Package() string {
return filepath.Base(msg.t.PkgPath())
}
func (msg *MessageType) Name() string {
return msg.t.Name()
}
type HandlerMethod struct {
ID byte
Name string
ReqType reflect.Type
RspType reflect.Type
NeedSession bool
}
func (h *HandlerMethod) InvokeCode() string {
if !h.NeedSession {
if h.RspType == nil {
return fmt.Sprintf("s.%s(req.(*%s))", h.Name, h.ReqType.Name())
}
return fmt.Sprintf("session.Send(s.%s(req.(*%s)))", h.Name, h.ReqType.Name())
}
if h.RspType == nil {
return fmt.Sprintf("s.%s(session, req.(*%s))", h.Name, h.ReqType.Name())
}
return fmt.Sprintf("session.Send(s.%s(session, req.(*%s)))", h.Name, h.ReqType.Name())
}