-
Notifications
You must be signed in to change notification settings - Fork 19
/
transition_callback_def.go
156 lines (129 loc) · 4.05 KB
/
transition_callback_def.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
package statemachine
import (
"fmt"
"reflect"
)
type TransitionCallbackFuncDef struct {
Label string `json:",omitempty" hcl:"label" hcle:"omitempty"`
RegisteredFunc string `json:",omitempty" hcl:"registered_func" hcle:"omitempty"`
Func TransitionCallbackFunc `json:"-" hcle:"omit"`
}
type TransitionCallbackDef struct {
From []string `json:",omitempty" hcl:"from" hcle:"omitempty"`
ExceptFrom []string `json:",omitempty" hcl:"except_from" hcle:"omitempty"`
To []string `json:",omitempty" hcl:"to" hcle:"omitempty"`
ExceptTo []string `json:",omitempty" hcl:"except_to" hcle:"omitempty"`
Do []*TransitionCallbackFuncDef `json:",omitempty" hcl:"do" hcle:"omitempty"`
ExitToState string `json:",omitempty" hcl:"exit_to_state" hcle:"omitempty"`
validateFor string `json:"-" hcle:"omit"`
}
func (s *TransitionCallbackDef) Matches(from, to string) bool {
// except from
for _, exceptState := range s.ExceptFrom {
if from == exceptState {
return false
}
}
// except to
for _, exceptState := range s.ExceptTo {
if to == exceptState {
return false
}
}
matchesFrom := len(s.From) == 0
matchesTo := len(s.To) == 0
if !matchesFrom {
for _, state := range s.From {
if from == state {
matchesFrom = true
}
}
}
if !matchesTo {
for _, state := range s.To {
if to == state {
matchesTo = true
}
}
}
if matchesFrom && matchesTo {
return true
}
return false
}
func (s *TransitionCallbackDef) SetFrom(states ...string) {
for _, state := range states {
s.From = append(s.From, state)
}
}
func (s *TransitionCallbackDef) SetFromAnyExcept(exceptStates ...string) {
for _, exceptState := range exceptStates {
s.ExceptFrom = append(s.ExceptFrom, exceptState)
}
}
func (s *TransitionCallbackDef) SetTo(states ...string) {
for _, state := range states {
s.To = append(s.To, state)
}
}
func (s *TransitionCallbackDef) SetSame() {
s.To = s.From
}
func (s *TransitionCallbackDef) SetToAnyExcept(exceptStates ...string) {
for _, exceptState := range exceptStates {
s.ExceptTo = append(s.ExceptTo, exceptState)
}
}
func (s *TransitionCallbackDef) SetExitToState(supermachineState string) {
s.ExitToState = supermachineState
}
func (s *TransitionCallbackDef) AddCallbackFunc(callbackFuncs ...TransitionCallbackFunc) {
for _, callbackFunc := range callbackFuncs {
s.assertCallbackKind(callbackFunc)
s.Do = append(s.Do, &TransitionCallbackFuncDef{Func: callbackFunc})
}
}
func (s *TransitionCallbackDef) assertCallbackKind(callbackFunc TransitionCallbackFunc) {
t := reflect.TypeOf(callbackFunc)
switch t.Kind() {
case reflect.Func:
if t.NumOut() != 0 {
panic("callback func must not return anything")
}
optionalArgs := make(map[reflect.Type]struct{})
requiredArgs := make(map[reflect.Type]struct{})
optionalArgs[reflect.TypeOf(new(Machine))] = struct{}{}
switch s.validateFor {
case "BeforeTransition":
optionalArgs[reflect.TypeOf(new(Transition))] = struct{}{}
case "AroundTransition":
optionalArgs[reflect.TypeOf(new(Transition))] = struct{}{}
requiredArgs[reflect.TypeOf(new(func()))] = struct{}{}
case "AfterTransition":
optionalArgs[reflect.TypeOf(new(Transition))] = struct{}{}
}
// ensure all args are of expected types, whether optional or required
for i := 0; i < t.NumIn(); i++ {
argType := t.In(i)
if _, ok := optionalArgs[reflect.PtrTo(argType)]; ok {
continue
}
if _, ok := requiredArgs[reflect.PtrTo(argType)]; ok {
continue
}
panic(fmt.Sprintf("unexpected argument with type '%s' in %s callback", argType, s.validateFor))
}
outer:
// ensure required args are present
for requiredArg := range requiredArgs {
for i := 0; i < t.NumIn(); i++ {
if requiredArg.Elem() == t.In(i) {
continue outer
}
}
panic(fmt.Sprintf("missing required arg '%s' in %s callback", requiredArg.Elem().String(), s.validateFor))
}
return
}
panic("callback must be a compatible func")
}