-
Notifications
You must be signed in to change notification settings - Fork 1
/
event.go
46 lines (39 loc) · 912 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 particle
import (
"encoding/json"
"github.com/donovanhide/eventsource"
)
// Event represent an event from the Particle Cloud
type Event struct {
ID string
Name string
Data *Data
}
// Data represent the data struct from the Particle Cloud
type Data struct {
Data string `json:"data"`
TTL string `json:"ttl"`
PublishTime string `json:"published_at"`
PhotonID string `json:"coreid"`
}
// ToEvent convert an eventsource.Event into an Event struct
func ToEvent(e eventsource.Event) (*Event, error) {
event := Event{}
event.ID = e.Id()
event.Name = e.Event()
data, err := ToData(e.Data())
if err != nil {
return nil, err
}
event.Data = data
return &event, nil
}
// ToData convert a json string to Data struct
func ToData(j string) (*Data, error) {
data := Data{}
err := json.Unmarshal([]byte(j), &data)
if err != nil {
return nil, err
}
return &data, nil
}