-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathevent.go
141 lines (116 loc) · 3.27 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
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
package data
import(
"encoding/json"
"errors"
)
type Event struct {
Payload interface{}
}
// Map type asserts to `map`
func (j *Event) Map() (map[string]interface{}, error) {
var copy map[string]interface{}
switch (j.Payload).(type) {
default:
return nil, errors.New("type assertion to map[string]interface{} failed")
case map[string]interface{}:
copy, _ = (j.Payload).(map[string]interface{})
case map[interface{}]interface{}:
copy = make(map[string]interface{})
attrs, _ := (j.Payload).(map[interface{}]interface{})
for i, k := range(attrs) {
st, _ := i.(string)
copy[st] = k
}
}
return copy, nil
}
// Get returns a pointer to a new `Json` object
// for `key` in its `map` representation
//
// useful for chaining operations (to traverse a nested JSON):
// js.Get("top_level").Get("dict").Get("value").Int()
func (j *Event) Get(key string) *Event {
m, err := j.Map()
if err == nil {
if val, ok := m[key]; ok {
return &Event{val}
}
}
return &Event{nil}
}
// Array type asserts to an `array`
func (j *Event) Array() ([]interface{}, error) {
if a, ok := (j.Payload).([]interface{}); ok {
return a, nil
}
return nil, errors.New("type assertion to []interface{} failed")
}
// Bool type asserts to `bool`
func (j *Event) Bool() (bool, error) {
if s, ok := (j.Payload).(bool); ok {
return s, nil
}
return false, errors.New("type assertion to bool failed")
}
// String type asserts to `string`
func (j *Event) String() (string, error) {
if s, ok := (j.Payload).(string); ok {
return s, nil
}
return "", errors.New("type assertion to string failed")
}
// Float64 type asserts to `float64`
func (j *Event) Float64() (float64, error) {
if i, ok := (j.Payload).(float64); ok {
return i, nil
}
return -1, errors.New("type assertion to float64 failed")
}
// Int type asserts to `float64` then converts to `int`
func (j *Event) Int() (int, error) {
if f, ok := (j.Payload).(int); ok {
return int(f), nil
}
if f, ok := (j.Payload).(int8); ok {
return int(f), nil
}
if f, ok := (j.Payload).(float64); ok {
return int(f), nil
}
return -1, errors.New("type assertion to float64 failed")
}
// Int type asserts to `float64` then converts to `int64`
func (j *Event) Int64() (int64, error) {
if f, ok := (j.Payload).(float64); ok {
return int64(f), nil
}
return -1, errors.New("type assertion to float64 failed")
}
// Bytes type asserts to `[]byte`
func (j *Event) Bytes() ([]byte, error) {
if s, ok := (j.Payload).(string); ok {
return []byte(s), nil
}
return nil, errors.New("type assertion to []byte failed")
}
type EventsChannel chan *Event
func Decode(payload []byte) (*Event, error) {
// event := &Event{}
// err := msgpack.Unmarshal(payload, &event.Payload, nil)
event, err := DecodeJSON(payload)
return event, err
}
func Encode(event *Event) (data []byte, err error) {
data, err = EncodeJSON(event)
// data, err = msgpack.Marshal(&event.Payload)
return
}
func DecodeJSON(payload []byte) (*Event, error) {
event := &Event{}
err := json.Unmarshal(payload, &event.Payload)
return event, err
}
func EncodeJSON(event *Event) (data []byte, err error) {
data, err = json.Marshal(&event.Payload)
return
}