-
Notifications
You must be signed in to change notification settings - Fork 19
/
transition_builder.go
187 lines (155 loc) · 6.39 KB
/
transition_builder.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package statemachine
// TransitionGuard may accept Transition object as input, and it must
// return a bool type.
//
// Valid TransitionGuard types:
//
// bool
// func() bool
// func(transition statemachine.Transition) bool
type TransitionGuard interface{}
// TransitionBuilder provides the ability to define the `from` state(s) of
// the transition matcher.
type TransitionBuilder interface {
From(states ...string) TransitionFromBuilder
FromAny() TransitionFromBuilder
FromAnyExcept(states ...string) TransitionFromBuilder
}
// TransitionFromBuilder inherits `from` states from TransitionBuilder
// and provides the ability to define the `to` state as well as the
// `except from` states of the transition matcher.
type TransitionFromBuilder interface {
ExceptFrom(states ...string) TransitionExceptFromBuilder
To(state string) TransitionToBuilder
}
// TransitionExceptFromBuilder inherits from TransitionFromBuilder and
// provides the ability to define the `to` state of the transition matcher.
type TransitionExceptFromBuilder interface {
To(state string) TransitionToBuilder
}
// TransitionToBuilder inherits from TransitionFromBuilder (or
// TransitionExceptFromBuilder) and provides the ability to define the guard
// condition funcs for the transition.
type TransitionToBuilder interface {
If(guards ...TransitionGuard) TransitionAndGuardBuilder
Unless(guards ...TransitionGuard) TransitionAndGuardBuilder
}
// TransitionAndGuardBuilder inherits from TransitionToBuilder and provides
// the ability to define additional guard condition funcs for the transition.
type TransitionAndGuardBuilder interface {
Label(label string) TransitionAndGuardBuilder
AndIf(guards ...TransitionGuard) TransitionAndGuardBuilder
AndUnless(guards ...TransitionGuard) TransitionAndGuardBuilder
}
// newTransitionBuilder returns a zero-valued instance of
// TransitionBuilder, which implements TransitionBuilder.
func newTransitionBuilder(transitionDef *TransitionDef) TransitionBuilder {
return &transitionBuilder{
transitionDef: transitionDef,
}
}
// transitionBuilder implements TransitionBuilder
type transitionBuilder struct {
transitionDef *TransitionDef
}
var _ TransitionBuilder = (*transitionBuilder)(nil)
func (builder *transitionBuilder) From(states ...string) TransitionFromBuilder {
builder.transitionDef.SetFrom(states...)
return newTransitionFromBuilder(builder.transitionDef)
}
func (builder *transitionBuilder) FromAny() TransitionFromBuilder {
builder.transitionDef.SetFromAnyExcept()
return newTransitionFromBuilder(builder.transitionDef)
}
func (builder *transitionBuilder) FromAnyExcept(states ...string) TransitionFromBuilder {
builder.transitionDef.SetFromAnyExcept(states...)
return newTransitionFromBuilder(builder.transitionDef)
}
// newTransitionFromBuilder returns a zero-valued instance of
// TransitionFromBuilder, which implements TransitionFromBuilder.
func newTransitionFromBuilder(transitionDef *TransitionDef) TransitionFromBuilder {
return &transitionFromBuilder{
transitionDef: transitionDef,
}
}
// transitionFromBuilder implements TransitionFromBuilder
type transitionFromBuilder struct {
transitionDef *TransitionDef
}
var _ TransitionFromBuilder = (*transitionFromBuilder)(nil)
func (builder *transitionFromBuilder) ExceptFrom(states ...string) TransitionExceptFromBuilder {
builder.transitionDef.SetFromAnyExcept(states...)
return newTransitionExceptFromBuilder(builder.transitionDef)
}
func (builder *transitionFromBuilder) To(state string) TransitionToBuilder {
builder.transitionDef.SetTo(state)
return newTransitionToBuilder(builder.transitionDef)
}
// newTransitionExceptFromBuilder returns a zero-valued instance of
// TransitionExceptFromBuilder, which implements TransitionExceptFromBuilder.
func newTransitionExceptFromBuilder(transitionDef *TransitionDef) TransitionExceptFromBuilder {
return &transitionExceptFromBuilder{
transitionDef: transitionDef,
}
}
// transitionExceptFromBuilder implements TransitionExceptFromBuilder
type transitionExceptFromBuilder struct {
transitionDef *TransitionDef
}
var _ TransitionExceptFromBuilder = (*transitionExceptFromBuilder)(nil)
func (builder *transitionExceptFromBuilder) To(state string) TransitionToBuilder {
builder.transitionDef.SetTo(state)
return newTransitionToBuilder(builder.transitionDef)
}
// newTransitionToBuilder returns a zero-valued instance of
// TransitionToBuilder, which implements TransitionToBuilder.
func newTransitionToBuilder(transitionDef *TransitionDef) TransitionToBuilder {
return &transitionToBuilder{
transitionDef: transitionDef,
}
}
// transitionToBuilder implements TransitionToBuilder
type transitionToBuilder struct {
transitionDef *TransitionDef
}
var _ TransitionToBuilder = (*transitionToBuilder)(nil)
func (builder *transitionToBuilder) If(guard ...TransitionGuard) TransitionAndGuardBuilder {
builder.transitionDef.AddIfGuard(guard...)
return newTransitionAndGuardBuilder(builder.transitionDef, "if")
}
func (builder *transitionToBuilder) Unless(guard ...TransitionGuard) TransitionAndGuardBuilder {
builder.transitionDef.AddUnlessGuard(guard...)
return newTransitionAndGuardBuilder(builder.transitionDef, "unless")
}
// newTransitionAndGuardBuilder returns a zero-valued instance of
// TransitionAndGuardBuilder, which implements TransitionAndGuardBuilder.
func newTransitionAndGuardBuilder(transitionDef *TransitionDef, lastGuardType string) TransitionAndGuardBuilder {
return &transitionAndGuardBuilder{
transitionDef: transitionDef,
lastGuardType: lastGuardType,
}
}
// transitionAndGuardBuilder implements TransitionAndGuardBuilder
type transitionAndGuardBuilder struct {
transitionDef *TransitionDef
lastGuardType string
}
var _ TransitionAndGuardBuilder = (*transitionAndGuardBuilder)(nil)
func (builder *transitionAndGuardBuilder) Label(label string) TransitionAndGuardBuilder {
if builder.lastGuardType == "if" {
builder.transitionDef.IfGuards[len(builder.transitionDef.IfGuards)-1].Label = label
} else {
builder.transitionDef.UnlessGuards[len(builder.transitionDef.UnlessGuards)-1].Label = label
}
return builder
}
func (builder *transitionAndGuardBuilder) AndIf(guard ...TransitionGuard) TransitionAndGuardBuilder {
builder.transitionDef.AddIfGuard(guard...)
builder.lastGuardType = "if"
return builder
}
func (builder *transitionAndGuardBuilder) AndUnless(guard ...TransitionGuard) TransitionAndGuardBuilder {
builder.transitionDef.AddUnlessGuard(guard...)
builder.lastGuardType = "unless"
return builder
}