/
state_result.go
132 lines (111 loc) · 3.26 KB
/
state_result.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
package step
import (
"math/rand"
)
////////////////////////////////////////////////////////////////////////////////
// SuccessState
////////////////////////////////////////////////////////////////////////////////
// SuccessState represents the end of the state machine
type SuccessState struct {
baseInnerState
}
// Name returns the WaitDelay name
func (ss *SuccessState) Name() string {
return ss.name
}
// Next sets the step after the wait delay
func (ss *SuccessState) Next(nextState MachineState) MachineState {
ss.next = nextState
return ss
}
// AdjacentStates returns nodes reachable from this node
func (ss *SuccessState) AdjacentStates() []MachineState {
if ss.next == nil {
return nil
}
return []MachineState{ss.next}
}
// WithComment returns the WaitDelay comment
func (ss *SuccessState) WithComment(comment string) TransitionState {
ss.comment = comment
return ss
}
// WithInputPath returns the TaskState input data selector
func (ss *SuccessState) WithInputPath(inputPath string) TransitionState {
ss.inputPath = inputPath
return ss
}
// WithOutputPath returns the TaskState output data selector
func (ss *SuccessState) WithOutputPath(outputPath string) TransitionState {
ss.outputPath = outputPath
return ss
}
// MarshalJSON for custom marshalling
func (ss *SuccessState) MarshalJSON() ([]byte, error) {
return ss.marshalStateJSON("Succeed", nil)
}
// NewSuccessState returns a "SuccessState" with the supplied
// name
func NewSuccessState(name string) *SuccessState {
return &SuccessState{
baseInnerState: baseInnerState{
name: name,
id: rand.Int63(),
isEndStateInvalid: true,
},
}
}
////////////////////////////////////////////////////////////////////////////////
// FailState represents the end of state machine
type FailState struct {
baseInnerState
ErrorName string
Cause error
}
// Name returns the WaitDelay name
func (fs *FailState) Name() string {
return fs.name
}
// Next sets the step after the wait delay
func (fs *FailState) Next(nextState MachineState) MachineState {
return fs
}
// AdjacentStates returns nodes reachable from this node
func (fs *FailState) AdjacentStates() []MachineState {
return nil
}
// WithComment returns the WaitDelay comment
func (fs *FailState) WithComment(comment string) TransitionState {
fs.comment = comment
return fs
}
// WithInputPath returns the TaskState input data selector
func (fs *FailState) WithInputPath(inputPath string) TransitionState {
return fs
}
// WithOutputPath returns the TaskState output data selector
func (fs *FailState) WithOutputPath(outputPath string) TransitionState {
return fs
}
// MarshalJSON for custom marshaling
func (fs *FailState) MarshalJSON() ([]byte, error) {
additionalParams := make(map[string]interface{})
additionalParams["Error"] = fs.ErrorName
if fs.Cause != nil {
additionalParams["Cause"] = fs.Cause.Error()
}
return fs.marshalStateJSON("Fail", additionalParams)
}
// NewFailState returns a "FailState" with the supplied
// information
func NewFailState(failStateName string, errorName string, cause error) *FailState {
return &FailState{
baseInnerState: baseInnerState{
name: failStateName,
id: rand.Int63(),
isEndStateInvalid: true,
},
ErrorName: errorName,
Cause: cause,
}
}