/
binding_types.go
312 lines (272 loc) · 8.63 KB
/
binding_types.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package gotojs
import (
"fmt"
"log"
"net/http"
"reflect"
)
// remoteBinder is a function type that will be invoked for a remote binding.
type remoteBinder func(c *HTTPContext, s *Session, i []interface{}) interface{}
// bindingInterface declare binding specific methods.
type bindingInterface interface {
invokeI(Injections, []interface{}) interface{}
argCount() int
argType(int) reflect.Type
signature([]reflect.Type) string
base() *binding
}
// binding declares binding type independent attributes
type binding struct {
elemName string
interfaceName string
injections map[int]reflect.Type
singletons Injections
filters []Filter
container *Container
}
type functionBinding struct {
binding
i interface{}
}
type attributeBinding struct {
binding
elemNum int
i interface{}
}
type methodBinding struct {
binding
elemNum int
i interface{}
}
type remoteBinding struct {
binding
remoteSignature string
i remoteBinder
}
type handlerBinding struct {
binding
handler http.HandlerFunc
i http.Handler
}
// :-D
func (b *binding) base() *binding {
return b
}
//InterfaceName returns the name of the interface this binding is assigned to.
func (b *binding) InterfaceName() string {
return b.interfaceName
}
//MEthodName returns the method name of this binding.
func (b *binding) MethodName() string {
return b.elemName
}
// ValidationString generate a string that represents the signature of a method or function. It
// is used to perform a runtime validation when calling a JS proxy method.
func (b Binding) ValidationString() (ret string) {
return b.signature(parameterTypeArray(b, false))
}
func (b *binding) signature(a []reflect.Type) (ret string) {
ret = ""
//TODO: check direct Mapping
for _, v := range a {
ret += string(kindMapping[v.Kind()])
}
return
}
func (b *remoteBinding) signature(a []reflect.Type) string {
return b.remoteSignature
}
func (b *handlerBinding) signature(a []reflect.Type) string {
return ""
}
// argType is an internally used method that returns the type of i-th binding parameter.
// The index i does not respect the receiver but includes the injected parameters.
func (b *attributeBinding) argType(i int) reflect.Type {
return reflect.TypeOf(b.i)
}
func (b *remoteBinding) argType(i int) reflect.Type {
return reflect.TypeOf(b.i).In(i)
}
func (b *methodBinding) argType(i int) reflect.Type {
t := reflect.TypeOf(b.i)
return t.Method(b.elemNum).Type.In(i + 1)
}
func (b *functionBinding) argType(i int) reflect.Type {
return reflect.TypeOf(b.i).In(i)
}
func (b *handlerBinding) argType(i int) reflect.Type {
return reflect.TypeOf("")
}
// argCount is an internally Function that returns the effective amount of parameters
// this binding needs. This includes injections and excludes the receiver.
func (b *attributeBinding) argCount() int {
return 0
}
func (b *methodBinding) argCount() int {
return reflect.TypeOf(b.i).Method(b.elemNum).Func.Type().NumIn() - 1
}
func (b *functionBinding) argCount() int {
return reflect.TypeOf(b.i).NumIn()
}
func (b *remoteBinding) argCount() int {
//TODO: check if this is correct ? Statically return 3 ?
return reflect.TypeOf(b.i).NumIn()
}
func (b *handlerBinding) argCount() int {
return 0
}
//NewRemoteBinding creates a new remote binding. All details are kept in the closure of the given proxy function.
func (b *Container) newRemoteBinding(i remoteBinder, sig, in, mn string) (ret Binding) {
ret = Binding{bindingInterface: &remoteBinding{
binding: *b.newBinding(in, mn),
remoteSignature: sig,
i: i,
}}
ret.addGlobalInjections()
b.bindingContainer[in][mn] = ret
return
}
// newBinding creates a new binding object that is associated with the given container.
// All existing global Injections will be added to this binding.
func (b *Container) newBinding(in, mn string) *binding {
_, found := b.Binding(in, mn)
if found {
log.Printf("Binding \"%s\" already exposed for interface \"%s\". Overwriting.", mn, in)
} else {
if _, f := b.bindingContainer[in]; !f {
im := make(map[string]Binding)
b.bindingContainer[in] = im
}
if _, f := b.bindingContainer[in][mn]; !f {
r := Binding{}
b.bindingContainer[in][mn] = r
}
}
p := &binding{
elemName: mn,
interfaceName: in,
container: b,
singletons: make(Injections),
injections: make(map[int]reflect.Type)}
return p
}
func (b *Container) newHandlerBinding(handler http.Handler, in, mn string) (ret Binding) {
ret = Binding{
bindingInterface: &handlerBinding{
binding: *b.newBinding(in, mn),
handler: nil,
i: handler,
}}
// No Injections needed here
b.bindingContainer[in][mn] = ret
return
}
func (b *Container) newHandlerFuncBinding(handler http.HandlerFunc, in, mn string) (ret Binding) {
ret = Binding{
bindingInterface: &handlerBinding{
binding: *b.newBinding(in, mn),
handler: handler,
i: nil,
}}
// No Injections needed here
b.bindingContainer[in][mn] = ret
return
}
//newMethodBinding creates a new method binding with the given interface and method name, whereby
// x specifies the x-th method of the given object.
func (b *Container) newMethodBinding(i interface{}, x int, in, mn string) (ret Binding) {
ret = Binding{bindingInterface: &methodBinding{
binding: *b.newBinding(in, mn),
elemNum: x,
i: i,
}}
ret.addGlobalInjections()
b.bindingContainer[in][mn] = ret
return
}
//newFunctionBinding creates a new function binding with the given interface and method name.
func (b *Container) newFunctionBinding(i interface{}, in, mn string) (ret Binding) {
ret = Binding{bindingInterface: &functionBinding{
binding: *b.newBinding(in, mn),
i: i,
}}
ret.addGlobalInjections()
b.bindingContainer[in][mn] = ret
return
}
//newAttributeBinding creates a new attribute getter binding whereby x specifies the x-th
// field of there referenced object.
func (b *Container) newAttributeBinding(i interface{}, x int, in, mn string) (ret Binding) {
ret = Binding{bindingInterface: &attributeBinding{
binding: *b.newBinding(in, mn),
elemNum: x,
i: i,
}}
ret.addGlobalInjections()
b.bindingContainer[in][mn] = ret
return
}
//invokeI is an internally used method to invoke a proxy type binding
// using the given injection objects and binding parameters
func (b *methodBinding) invokeI(inj Injections, args []interface{}) interface{} {
val := reflect.ValueOf(b.i)
//Sanity check whether binding object is not of kind function.
if val.Kind() == reflect.Func {
panic(fmt.Errorf("MethodBinding for \"%s.%s\" does not bind an object.", b.interfaceName, b.elemName))
}
av := callValuesI(b, inj, args)
// Prepend the receiver
cav := make([]reflect.Value, len(av)+1)
cav[0] = val
for i, v := range av {
cav[i+1] = v
}
meth := reflect.TypeOf(b.i).Method(b.elemNum).Func
return convertReturnValue(meth.Call(cav)) // Call with receiver and consider injected objects.
}
//invokeI is an internally used method to invoke a function type binding
// using the given injection objects and binding parameters
func (b *functionBinding) invokeI(inj Injections, args []interface{}) interface{} {
val := reflect.ValueOf(b.i)
// Sanity check whether binding object is actually of kind function
if val.Kind() != reflect.Func {
panic(fmt.Errorf("FunctionBinding for \"%s.%s\" does not bind a function.", b.interfaceName, b.elemName))
}
meth := reflect.ValueOf(b.i)
av := callValuesI(b, inj, args)
return convertReturnValue(meth.Call(av)) // Call with receiver and consider injected objects.
}
//invokeI is an internally used method to invoke a proxy type binding
// using the given injection objects and binding parameters
func (b *remoteBinding) invokeI(inj Injections, args []interface{}) interface{} {
meth := reflect.ValueOf(b.i)
//wrap parameters in an []interface{} array
aa := make([]interface{}, 1)
aa[0] = args
av := callValuesI(b, inj, aa) //Important: use args as array parameter (NOT exploded !)
return convertReturnValue(meth.Call(av))
}
//invokeI returns the value of the attribute this binding is referring.
func (b *attributeBinding) invokeI(inj Injections, args []interface{}) interface{} {
return reflect.ValueOf(b.i).Elem().Field(b.elemNum).Interface()
}
//invokeI returns invokes the associated Handler
func (b *handlerBinding) invokeI(inj Injections, args []interface{}) interface{} {
o, ok := inj[reflect.TypeOf(&HTTPContext{})]
if !ok {
panic(fmt.Errorf("No web context found: %s", inj))
}
httpContext, ok := o.(*HTTPContext)
if !ok {
panic(fmt.Errorf("No http context found."))
}
if b.i != nil {
b.i.ServeHTTP(httpContext.Response, httpContext.Request)
return nil
}
if b.handler != nil {
b.handler(httpContext.Response, httpContext.Request)
return nil
}
return nil
}