-
Notifications
You must be signed in to change notification settings - Fork 19
/
category.go
60 lines (47 loc) · 1.8 KB
/
category.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
package routers
import (
"fmt"
"github.com/nyaruka/gocommon/jsonx"
"github.com/nyaruka/gocommon/uuids"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
)
type Category struct {
uuid flows.CategoryUUID
name string
exitUUID flows.ExitUUID
}
// NewCategory creates a new category
func NewCategory(uuid flows.CategoryUUID, name string, exit flows.ExitUUID) *Category {
return &Category{uuid: uuid, name: name, exitUUID: exit}
}
func (c *Category) UUID() flows.CategoryUUID { return c.uuid }
func (c *Category) Name() string { return c.name }
func (c *Category) ExitUUID() flows.ExitUUID { return c.exitUUID }
// LocalizationUUID gets the UUID which identifies this object for localization
func (c *Category) LocalizationUUID() uuids.UUID { return uuids.UUID(c.uuid) }
var _ flows.Category = (*Category)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type categoryEnvelope struct {
UUID flows.CategoryUUID `json:"uuid" validate:"required,uuid4"`
Name string `json:"name,omitempty"`
ExitUUID flows.ExitUUID `json:"exit_uuid,omitempty" validate:"required,uuid4"`
}
// ReadCategory unmarshals a router category from the given JSON
func ReadCategory(data []byte) (flows.Category, error) {
e := &categoryEnvelope{}
if err := utils.UnmarshalAndValidate(data, e); err != nil {
return nil, fmt.Errorf("unable to read category: %w", err)
}
return NewCategory(e.UUID, e.Name, e.ExitUUID), nil
}
// MarshalJSON marshals this node category into JSON
func (c *Category) MarshalJSON() ([]byte, error) {
return jsonx.Marshal(&categoryEnvelope{
c.uuid,
c.name,
c.exitUUID,
})
}