forked from henvic/gosocketio
/
listener.go
195 lines (152 loc) · 3.65 KB
/
listener.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
package gosocketio
import (
"errors"
"fmt"
"reflect"
)
// Handler for the message
type Handler struct {
Func reflect.Value
Out bool
Variadic bool
args []reflect.Type
}
// ErrorArgument is used when trying to create a non-function listener with an invalid parameter
type ErrorArgument struct {
kind reflect.Kind
}
func (e ErrorArgument) Error() string {
return fmt.Sprintf(`invalid argument type "%v" for handler`, e.kind)
}
type ErrorInvalidInterface struct {
method string
reason string
}
func (e ErrorInvalidInterface) Error() string {
return fmt.Sprintf(`invalid interface for handling request for "%s" call: %s`, e.method, e.reason)
}
func (e ErrorInvalidInterface) Method() string {
return e.method
}
// ErrorNotFunction is used when trying to create a non-function listener
type ErrorNotFunction struct {
kind reflect.Kind
}
func (e ErrorNotFunction) Error() string {
return fmt.Sprintf("listener is a %v instead of a function", e.kind)
}
func isVariadicNonInterface(fType reflect.Type) bool {
if !fType.IsVariadic() {
return false
}
numIn := fType.NumIn()
arg := fType.In(numIn - 1)
if arg.Kind() != reflect.Slice {
return false
}
return arg.Elem().Kind() != reflect.Interface
}
// NewHandler creates a new listener
func NewHandler(f interface{}) (*Handler, error) {
fValue := reflect.ValueOf(f)
if fValue.Kind() != reflect.Func {
return nil, ErrorNotFunction{fValue.Kind()}
}
fType := fValue.Type()
if fType.NumOut() > 1 {
return nil, errors.New("f should return not more than one value")
}
if err := checkHandlerInputParams(fType); err != nil {
return nil, err
}
numIn := fType.NumIn()
if isVariadicNonInterface(fType) {
return nil, errors.New("support for variadic is only partially implemented; see https://github.com/wedeploy/gosocket.io-client-go/issues/1")
}
h := &Handler{
Func: fValue,
Out: fType.NumOut() == 1,
Variadic: fType.IsVariadic(),
}
for c := 0; c < numIn; c++ {
h.args = append(h.args, fType.In(c))
}
return h, nil
}
// Call function
func (h *Handler) Call(args ...interface{}) []reflect.Value {
// nil is untyped, so use the default empty value of correct type
if args == nil {
args = h.Args()
}
a := []reflect.Value{}
if len(h.args) != 0 {
a = h.matchArgs(args)
}
return h.Func.Call(a)
}
func (h *Handler) matchArgs(args []interface{}) (a []reflect.Value) {
lengthFuncArgs := len(h.args)
for pos := range h.args {
if h.Variadic && pos == lengthFuncArgs-1 {
break
}
rfv := reflect.ValueOf(args[pos])
a = append(a, rfv.Elem())
}
if !h.Variadic {
return a
}
for variadicPos := lengthFuncArgs - 1; variadicPos < len(args); variadicPos++ {
a = append(a, reflect.ValueOf(args[variadicPos]))
}
return a
}
// Args returns the interfaces for the given function
func (h *Handler) Args() []interface{} {
var interfaces = []interface{}{}
for _, a := range h.args {
interfaces = append(interfaces, reflect.New(a).Interface())
}
return interfaces
}
func checkParamKind(kind reflect.Kind) error {
switch kind {
case reflect.Bool,
reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64,
reflect.Complex64,
reflect.Complex128,
reflect.Array,
reflect.Interface,
reflect.Map,
reflect.Ptr,
reflect.Slice,
reflect.String,
reflect.Struct:
return nil
}
return ErrorArgument{
kind,
}
}
func checkHandlerInputParams(fType reflect.Type) error {
num := fType.NumIn()
for c := 0; c < num; c++ {
in := fType.In(c)
if err := checkParamKind(in.Kind()); err != nil {
return err
}
}
return nil
}