-
Notifications
You must be signed in to change notification settings - Fork 27
/
event.go
69 lines (62 loc) · 1.36 KB
/
event.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
package live
// EventHandler a function to handle events, returns the data that should
// be set to the socket after handling.
type EventHandler func(*Socket, map[string]interface{}) (interface{}, error)
// Live events.
const (
EventError = "err"
EventPatch = "patch"
)
// Event messages that are sent and received by the
// socket.
type Event struct {
T string `json:"t"`
Data interface{} `json:"d"`
}
// Params extract params from inbound message.
func (e Event) Params() (map[string]interface{}, error) {
if e.Data == nil {
return map[string]interface{}{}, nil
}
p, ok := e.Data.(map[string]interface{})
if !ok {
return nil, ErrMessageMalformed
}
return p, nil
}
// ParamString helper to return a string from the params.
func ParamString(params map[string]interface{}, key string) string {
v, ok := params[key]
if !ok {
return ""
}
out, ok := v.(string)
if !ok {
return ""
}
return out
}
// ParamInt helper to return an int from the params.
func ParamInt(params map[string]interface{}, key string) int {
v, ok := params[key]
if !ok {
return 0
}
out, ok := v.(int)
if !ok {
return 0
}
return out
}
// ParamFloat32 helper to return a float32 from the params.
func ParamFloat32(params map[string]interface{}, key string) float32 {
v, ok := params[key]
if !ok {
return 0.0
}
out, ok := v.(float32)
if !ok {
return 0.0
}
return out
}