forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
label.go
51 lines (38 loc) · 1.1 KB
/
label.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
package flows
import (
"encoding/json"
)
// Label represents a msg label
type Label struct {
uuid LabelUUID `validate:"required,uuid"`
name string `validate:"required"`
}
// UUID returns the UUID of this label
func (l *Label) UUID() LabelUUID { return l.uuid }
// Name returns the name of this label
func (l *Label) Name() string { return l.name }
// NewLabel creates a new label given the passed in uuid and name
func NewLabel(uuid LabelUUID, name string) *Label {
return &Label{uuid, name}
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type labelEnvelope struct {
UUID LabelUUID `json:"uuid"`
Name string `json:"name"`
}
func (l *Label) UnmarshalJSON(data []byte) error {
var le labelEnvelope
var err error
err = json.Unmarshal(data, &le)
l.uuid = le.UUID
l.name = le.Name
return err
}
func (l *Label) MarshalJSON() ([]byte, error) {
var le labelEnvelope
le.Name = l.name
le.UUID = l.uuid
return json.Marshal(le)
}