-
Notifications
You must be signed in to change notification settings - Fork 33
/
event.go
77 lines (72 loc) · 1.84 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
70
71
72
73
74
75
76
77
package emitter
// Event is a structure to send events contains
// some helpers to cast primitive types easily.
type Event struct {
Topic, OriginalTopic string
Flags Flag
Args []interface{}
}
// Int returns casted into int type argument by index.
// `dflt` argument is an optional default value returned
// either in case of casting error or in case of index error.
func (e Event) Int(index uint, dflt ...int) int {
var d int
for _, first := range dflt {
d = first
break
}
if len(e.Args) > int(index) {
if casted, okey := e.Args[index].(int); okey {
d = casted
}
}
return d
}
// String returns casted into string type argument by index.
// `dflt` argument is an optional default value returned
// either in case of casting error or in case of index error.
func (e Event) String(index uint, dflt ...string) string {
var d string
for _, first := range dflt {
d = first
break
}
if len(e.Args) > int(index) {
if casted, okey := e.Args[index].(string); okey {
d = casted
}
}
return d
}
// Float returns casted into float64 type argument by index.
// `dflt` argument is an optional default value returned
// either in case of casting error or in case of index error.
func (e Event) Float(index uint, dflt ...float64) float64 {
var d float64
for _, first := range dflt {
d = first
break
}
if len(e.Args) > int(index) {
if casted, okey := e.Args[index].(float64); okey {
d = casted
}
}
return d
}
// Bool returns casted into bool type argument by index.
// `dflt` argument is an optional default value returned
// either in case of casting error or in case of index error.
func (e Event) Bool(index uint, dflt ...bool) bool {
var d bool
for _, first := range dflt {
d = first
break
}
if len(e.Args) > int(index) {
if casted, okey := e.Args[index].(bool); okey {
d = casted
}
}
return d
}