forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
167 lines (134 loc) · 4.16 KB
/
node.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package definition
import (
"encoding/json"
"fmt"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/actions"
"github.com/nyaruka/goflow/flows/routers"
"github.com/nyaruka/goflow/flows/waits"
"github.com/nyaruka/goflow/utils"
)
type exit struct {
uuid flows.ExitUUID
destination flows.NodeUUID
name string
}
func (e *exit) UUID() flows.ExitUUID { return e.uuid }
func (e *exit) DestinationNodeUUID() flows.NodeUUID { return e.destination }
func (e *exit) Name() string { return e.name }
type node struct {
uuid flows.NodeUUID
actions []flows.Action
router flows.Router
exits []flows.Exit
wait flows.Wait
}
func (n *node) UUID() flows.NodeUUID { return n.uuid }
func (n *node) Router() flows.Router { return n.router }
func (n *node) Actions() []flows.Action { return n.actions }
func (n *node) Exits() []flows.Exit { return n.exits }
func (n *node) Wait() flows.Wait { return n.wait }
func (n *node) Resolve(key string) interface{} {
switch key {
case "uuid":
return n.uuid
}
return fmt.Errorf("No field '%s' on node", key)
}
func (n *node) Default() interface{} { return n.uuid }
func (n *node) String() string { return n.uuid.String() }
var _ utils.VariableResolver = (*node)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type nodeEnvelope struct {
UUID flows.NodeUUID `json:"uuid" validate:"required,uuid4"`
Actions []*utils.TypedEnvelope `json:"actions,omitempty"`
Router *utils.TypedEnvelope `json:"router,omitempty"`
Exits []*exit `json:"exits"`
Wait *utils.TypedEnvelope `json:"wait,omitempty"`
}
func (n *node) UnmarshalJSON(data []byte) error {
envelope := nodeEnvelope{}
err := json.Unmarshal(data, &envelope)
err = utils.ValidateAllUnlessErr(err, &envelope)
if err != nil {
return err
}
n.uuid = envelope.UUID
// instantiate the right kind of router
if envelope.Router != nil {
n.router, err = routers.RouterFromEnvelope(envelope.Router)
if err != nil {
return err
}
}
// and the right kind of wait
if envelope.Wait != nil {
n.wait, err = waits.WaitFromEnvelope(envelope.Wait)
if err != nil {
return err
}
}
// and the right kind of actions
n.actions = make([]flows.Action, len(envelope.Actions))
for i := range envelope.Actions {
n.actions[i], err = actions.ActionFromEnvelope(envelope.Actions[i])
if err != nil {
return err
}
}
// populate our exits
n.exits = make([]flows.Exit, len(envelope.Exits))
for i := range envelope.Exits {
n.exits[i] = envelope.Exits[i]
}
return nil
}
func (n *node) MarshalJSON() ([]byte, error) {
envelope := nodeEnvelope{}
var err error
envelope.UUID = n.uuid
envelope.Router, err = utils.EnvelopeFromTyped(n.router)
if err != nil {
return nil, err
}
envelope.Wait, err = utils.EnvelopeFromTyped(n.wait)
if err != nil {
return nil, err
}
// and the right kind of actions
envelope.Actions = make([]*utils.TypedEnvelope, len(n.actions))
for i := range n.actions {
envelope.Actions[i], err = utils.EnvelopeFromTyped(n.actions[i])
if err != nil {
return nil, err
}
}
envelope.Exits = make([]*exit, len(n.exits))
for i := range n.exits {
envelope.Exits[i] = n.exits[i].(*exit)
}
return json.Marshal(envelope)
}
type exitEnvelope struct {
UUID flows.ExitUUID `json:"uuid" validate:"required,uuid4"`
DestinationNodeUUID flows.NodeUUID `json:"destination_node_uuid,omitempty" validate:"omitempty,uuid4"`
Name string `json:"name,omitempty"`
}
func (e *exit) UnmarshalJSON(data []byte) error {
envelope := exitEnvelope{}
err := json.Unmarshal(data, &envelope)
err = utils.ValidateAllUnlessErr(err, &envelope)
if err != nil {
return err
}
e.uuid = envelope.UUID
e.destination = envelope.DestinationNodeUUID
e.name = envelope.Name
return nil
}
func (e *exit) MarshalJSON() ([]byte, error) {
envelope := exitEnvelope{e.uuid, e.destination, e.name}
return json.Marshal(envelope)
}