-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
46 lines (39 loc) · 896 Bytes
/
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
package eventbus
import (
"bytes"
"encoding/json"
)
//--------------------------------------------------
// Event
//--------------------------------------------------
type Event struct {
Origin string `json:"origin"`
Type EventType `json:"type"`
Data []byte `json:"data"`
}
type EventType struct {
Name string `json:"name"`
}
//--------------------------------------------------
// Private Operations
//--------------------------------------------------
func toEvent(buf []byte) (*Event, error) {
buffer := bytes.NewBuffer(buf)
dec := json.NewDecoder(buffer)
evn := new(Event)
err := dec.Decode(evn)
if err != nil {
return nil, err
}
return evn, nil
}
func fromEvent(e *Event) ([]byte, error) {
byt := make([]byte, 0)
buf := bytes.NewBuffer(byt)
enc := json.NewEncoder(buf)
err := enc.Encode(e)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
}