/
event.go
53 lines (46 loc) · 1.09 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
package main
import (
"encoding/json"
"fmt"
"time"
)
// EventType the type of event from cloud
type EventType string
// The type of event from cloud
const (
Upload EventType = "UPLOAD"
Package EventType = "PACKAGE"
)
// Event event message
type Event struct {
Time time.Time `json:"time"`
Type EventType `json:"type"`
Content interface{} `json:"content"`
}
// EventMessage config
type EventMessage struct {
ID uint64
QOS uint32
Topic string
Event *Event
}
// NewEvent creates a new event
func NewEvent(v []byte) (*Event, error) {
var e Event
json.Unmarshal(v, &e)
switch e.Type {
case Upload:
e.Content = &UploadEvent{}
json.Unmarshal(v, &e)
return &e, nil
default:
return nil, fmt.Errorf("event type unexpected")
}
}
// UploadEvent update event
type UploadEvent struct {
RemotePath string `yaml:"remotePath" json:"remotePath" validate:"nonzero"`
LocalPath string `yaml:"localPath" json:"localPath" validate:"nonzero"`
Zip bool `yaml:"zip" json:"zip"`
Meta map[string]string `yaml:"meta" json:"meta" default:"{}"`
}