-
Notifications
You must be signed in to change notification settings - Fork 72
/
transition.go
241 lines (203 loc) · 5.92 KB
/
transition.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package transition
import (
"fmt"
"strings"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
"github.com/qor/qor/resource"
"github.com/qor/roles"
)
// Transition is a struct, embed it in your struct to enable state machine for the struct
type Transition struct {
State string
StateChangeLogs []StateChangeLog `sql:"-"`
}
// SetState set state to Stater, just set, won't save it into database
func (transition *Transition) SetState(name string) {
transition.State = name
}
// GetState get current state from
func (transition Transition) GetState() string {
return transition.State
}
// Stater is a interface including methods `GetState`, `SetState`
type Stater interface {
SetState(name string)
GetState() string
}
// New initialize a new StateMachine that hold states, events definitions
func New(value interface{}) *StateMachine {
return &StateMachine{
states: map[string]*State{},
events: map[string]*Event{},
}
}
// StateMachine a struct that hold states, events definitions
type StateMachine struct {
initialState string
states map[string]*State
events map[string]*Event
}
// Initial define the initial state
func (sm *StateMachine) Initial(name string) *StateMachine {
sm.initialState = name
return sm
}
// State define a state
func (sm *StateMachine) State(name string) *State {
state := &State{Name: name}
sm.states[name] = state
return state
}
// Event define an event
func (sm *StateMachine) Event(name string) *Event {
event := &Event{Name: name}
sm.events[name] = event
return event
}
// Trigger trigger an event
func (sm *StateMachine) Trigger(name string, value Stater, tx *gorm.DB, notes ...string) error {
var (
newTx *gorm.DB
stateWas = value.GetState()
)
if tx != nil {
newTx = tx.New()
}
if stateWas == "" {
stateWas = sm.initialState
value.SetState(sm.initialState)
}
if event := sm.events[name]; event != nil {
var matchedTransitions []*EventTransition
for _, transition := range event.transitions {
var validFrom = len(transition.froms) == 0
if len(transition.froms) > 0 {
for _, from := range transition.froms {
if from == stateWas {
validFrom = true
}
}
}
if validFrom {
matchedTransitions = append(matchedTransitions, transition)
}
}
if len(matchedTransitions) == 1 {
transition := matchedTransitions[0]
// State: exit
if state, ok := sm.states[stateWas]; ok {
for _, exit := range state.exits {
if err := exit(value, newTx); err != nil {
return err
}
}
}
// Transition: before
for _, before := range transition.befores {
if err := before(value, newTx); err != nil {
return err
}
}
value.SetState(transition.to)
// State: enter
if state, ok := sm.states[transition.to]; ok {
for _, enter := range state.enters {
if err := enter(value, newTx); err != nil {
value.SetState(stateWas)
return err
}
}
}
// Transition: after
for _, after := range transition.afters {
if err := after(value, newTx); err != nil {
value.SetState(stateWas)
return err
}
}
if newTx != nil {
scope := newTx.NewScope(value)
log := StateChangeLog{
ReferTable: scope.TableName(),
ReferID: GenerateReferenceKey(value, tx),
From: stateWas,
To: transition.to,
Note: strings.Join(notes, ""),
}
return newTx.Save(&log).Error
}
return nil
}
}
return fmt.Errorf("failed to perform event %s from state %s", name, stateWas)
}
// State contains State information, including enter, exit hooks
type State struct {
Name string
enters []func(value interface{}, tx *gorm.DB) error
exits []func(value interface{}, tx *gorm.DB) error
}
// Enter register an enter hook for State
func (state *State) Enter(fc func(value interface{}, tx *gorm.DB) error) *State {
state.enters = append(state.enters, fc)
return state
}
// Exit register an exit hook for State
func (state *State) Exit(fc func(value interface{}, tx *gorm.DB) error) *State {
state.exits = append(state.exits, fc)
return state
}
// Event contains Event information, including transition hooks
type Event struct {
Name string
transitions []*EventTransition
}
// To define EventTransition of go to a state
func (event *Event) To(name string) *EventTransition {
transition := &EventTransition{to: name}
event.transitions = append(event.transitions, transition)
return transition
}
// EventTransition hold event's to/froms states, also including befores, afters hooks
type EventTransition struct {
to string
froms []string
befores []func(value interface{}, tx *gorm.DB) error
afters []func(value interface{}, tx *gorm.DB) error
}
// From used to define from states
func (transition *EventTransition) From(states ...string) *EventTransition {
transition.froms = states
return transition
}
// Before register before hooks
func (transition *EventTransition) Before(fc func(value interface{}, tx *gorm.DB) error) *EventTransition {
transition.befores = append(transition.befores, fc)
return transition
}
// After register after hooks
func (transition *EventTransition) After(fc func(value interface{}, tx *gorm.DB) error) *EventTransition {
transition.afters = append(transition.afters, fc)
return transition
}
// ConfigureQorResource used to configure transition for qor admin
func (transition *Transition) ConfigureQorResource(res resource.Resourcer) {
if res, ok := res.(*admin.Resource); ok {
if meta := res.GetMeta("State"); meta.Permission == nil {
meta.Permission = roles.Deny(roles.Update, roles.Anyone).Deny(roles.Create, roles.Anyone)
}
res.OverrideIndexAttrs(func() {
res.IndexAttrs(res.IndexAttrs(), "-StateChangeLogs")
})
res.OverrideShowAttrs(func() {
res.ShowAttrs(res.ShowAttrs(), "-StateChangeLogs")
})
res.OverrideNewAttrs(func() {
res.NewAttrs(res.NewAttrs(), "-StateChangeLogs")
})
res.OverrideEditAttrs(func() {
res.EditAttrs(res.EditAttrs(), "-StateChangeLogs")
})
}
}