forked from violetpay-org/go-saga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
definition.go
63 lines (51 loc) · 898 Bytes
/
definition.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
package saga
type Definition struct {
steps []Step
}
func newDefinition(steps []Step) Definition {
return Definition{
steps: steps,
}
}
func (d Definition) FirstStep() Step {
if len(d.steps) == 0 {
return nil
}
return d.steps[0]
}
func (d Definition) FindStep(name string) Step {
for _, step := range d.steps {
if step.Name() == name {
return step
}
}
return nil
}
func (d Definition) Exists(step Step) bool {
for _, s := range d.steps {
if s.Name() == step.Name() {
return true
}
}
return false
}
func (d Definition) NextStep(step Step) Step {
for i, s := range d.steps {
if s.Name() == step.Name() {
if i+1 < len(d.steps) {
return d.steps[i+1]
}
}
}
return nil
}
func (d Definition) PrevStep(step Step) Step {
for i, s := range d.steps {
if s.Name() == step.Name() {
if i-1 >= 0 {
return d.steps[i-1]
}
}
}
return nil
}