-
Notifications
You must be signed in to change notification settings - Fork 20
/
exit.go
54 lines (41 loc) · 1.62 KB
/
exit.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
package definition
import (
"encoding/json"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
"github.com/nyaruka/goflow/utils/uuids"
"github.com/pkg/errors"
)
type exit struct {
uuid flows.ExitUUID
destination flows.NodeUUID
}
// NewExit creates a new exit
func NewExit(uuid flows.ExitUUID, destination flows.NodeUUID) flows.Exit {
return &exit{uuid: uuid, destination: destination}
}
func (e *exit) UUID() flows.ExitUUID { return e.uuid }
func (e *exit) DestinationUUID() flows.NodeUUID { return e.destination }
// LocalizationUUID gets the UUID which identifies this object for localization
func (e *exit) LocalizationUUID() uuids.UUID { return uuids.UUID(e.uuid) }
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type exitEnvelope struct {
UUID flows.ExitUUID `json:"uuid" validate:"required,uuid4"`
DestinationUUID flows.NodeUUID `json:"destination_uuid,omitempty" validate:"omitempty,uuid4"`
}
// UnmarshalJSON unmarshals a node exit from the given JSON
func (e *exit) UnmarshalJSON(data []byte) error {
envelope := &exitEnvelope{}
if err := utils.UnmarshalAndValidate(data, envelope); err != nil {
return errors.Wrap(err, "unable to read exit")
}
e.uuid = envelope.UUID
e.destination = envelope.DestinationUUID
return nil
}
// MarshalJSON marshals this node exit into JSON
func (e *exit) MarshalJSON() ([]byte, error) {
return json.Marshal(&exitEnvelope{e.uuid, e.destination})
}