-
Notifications
You must be signed in to change notification settings - Fork 20
/
step.go
130 lines (106 loc) · 3.24 KB
/
step.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
package runs
import (
"encoding/json"
"strings"
"time"
"github.com/nyaruka/goflow/excellent/types"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils"
)
type step struct {
stepUUID flows.StepUUID
nodeUUID flows.NodeUUID
exitUUID flows.ExitUUID
arrivedOn time.Time
}
// NewStep creates a new step
func NewStep(node flows.Node, arrivedOn time.Time) flows.Step {
return &step{
stepUUID: flows.StepUUID(utils.NewUUID()),
nodeUUID: node.UUID(),
arrivedOn: arrivedOn,
}
}
func (s *step) UUID() flows.StepUUID { return s.stepUUID }
func (s *step) NodeUUID() flows.NodeUUID { return s.nodeUUID }
func (s *step) ExitUUID() flows.ExitUUID { return s.exitUUID }
func (s *step) ArrivedOn() time.Time { return s.arrivedOn }
func (s *step) Leave(exit flows.ExitUUID) {
s.exitUUID = exit
}
func (s *step) Resolve(env utils.Environment, key string) types.XValue {
switch strings.ToLower(key) {
case "uuid":
return types.NewXText(string(s.UUID()))
case "node_uuid":
return types.NewXText(string(s.NodeUUID()))
case "arrived_on":
return types.NewXDateTime(s.ArrivedOn())
case "exit_uuid":
return types.NewXText(string(s.ExitUUID()))
default:
return types.NewXResolveError(s, key)
}
}
// Describe returns a representation of this type for error messages
func (s *step) Describe() string { return "step" }
func (s *step) Reduce(env utils.Environment) types.XPrimitive {
return types.NewXText(string(s.UUID()))
}
func (s *step) ToXJSON(env utils.Environment) types.XText {
return types.ResolveKeys(env, s, "uuid", "node_uuid", "arrived_on", "exit_uuid").ToXJSON(env)
}
var _ flows.Step = (*step)(nil)
type Path []flows.Step
func (p Path) Length() int {
return len(p)
}
func (p Path) Index(index int) types.XValue {
return p[index]
}
// Describe returns a representation of this type for error messages
func (p Path) Describe() string { return "path" }
func (p Path) Reduce(env utils.Environment) types.XPrimitive {
array := types.NewXArray()
for _, step := range p {
array.Append(step)
}
return array
}
func (p Path) ToXJSON(env utils.Environment) types.XText {
return p.Reduce(env).ToXJSON(env)
}
var _ types.XValue = (Path)(nil)
var _ types.XIndexable = (Path)(nil)
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
type stepEnvelope struct {
UUID flows.StepUUID `json:"uuid" validate:"required,uuid4"`
NodeUUID flows.NodeUUID `json:"node_uuid" validate:"required,uuid4"`
ExitUUID flows.ExitUUID `json:"exit_uuid,omitempty" validate:"omitempty,uuid4"`
ArrivedOn time.Time `json:"arrived_on"`
}
// UnmarshalJSON unmarshals a run step from the given JSON
func (s *step) UnmarshalJSON(data []byte) error {
var se stepEnvelope
var err error
err = json.Unmarshal(data, &se)
if err != nil {
return err
}
s.stepUUID = se.UUID
s.nodeUUID = se.NodeUUID
s.exitUUID = se.ExitUUID
s.arrivedOn = se.ArrivedOn
return err
}
// MarshalJSON marshals this run step into JSON
func (s *step) MarshalJSON() ([]byte, error) {
return json.Marshal(&stepEnvelope{
UUID: s.stepUUID,
NodeUUID: s.nodeUUID,
ExitUUID: s.exitUUID,
ArrivedOn: s.arrivedOn,
})
}