-
Notifications
You must be signed in to change notification settings - Fork 0
/
getWebSocketOps.go
60 lines (49 loc) · 1.87 KB
/
getWebSocketOps.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
package stonelizard
import (
"strings"
"reflect"
)
// Scans the websocket definition struct, identifies which fields defines websocket operations
// and returns its specifications in extended swagger format.
// Websocket operations are defined by:
// a) MUST be an unexported symbol (names starting with LOWERCASE letter)
// b) having the "in" tag defined
// c) Its type MUST not be WSEventTrigger (reserved for events)
// d) MUST exist an exported method whose name is equal to the field (except for the first letter which MUST be UPPERCASE)
func getWebSocketOps(field reflect.StructField) (map[string]*SwaggerWSOperationT, error) {
var ops map[string]*SwaggerWSOperationT
var err error
var retv reflect.Type
var fld reflect.StructField
var WSMethodName string
var WSMethod reflect.Method
var ok bool
var i int
var webSocketSpec *SwaggerWSOperationT
retv = field.Type
for i=0; i<retv.NumField(); i++ {
fld = retv.Field(i)
// WSEventTrigger fields defines events, not operations (rule 'c')
if fld.Type == typeWSEventTrigger {
continue
} else {
// Checks the "in" tag and if it is unexported field (rules 'a' and 'b')
if fld.Tag.Get("in") == "" || len(fld.Name)==0 || strings.ToLower(fld.Name[:1])!=fld.Name[:1] {
continue
}
// Checks the correspondent method (rule 'd')
WSMethodName = strings.ToUpper(fld.Name[:1]) + fld.Name[1:]
if WSMethod, ok = retv.MethodByName(WSMethodName); !ok {
continue
}
// Get the swagger specification
webSocketSpec, err = GetWebSocketSpec(fld, WSMethodName, WSMethod)
if err != nil {
return nil, err
}
// stores it in the mapping key is operation name, value is swagger operation
ops[WSMethodName] = webSocketSpec
}
}
return ops, nil
}