-
Notifications
You must be signed in to change notification settings - Fork 19
/
choice_builder.go
103 lines (84 loc) · 2.97 KB
/
choice_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
package statemachine
// ChoiceCondition may accept Transition object as input, and it must
// return a bool type.
//
// Valid ChoiceCondition types:
//
// bool
// func() bool
// func(transition statemachine.Transition) bool
type ChoiceCondition interface{}
// ChoiceBuilder provides the ability to define the conditions and result
// handling of the choice definition.
type ChoiceBuilder interface {
Label(label string) ChoiceBuilder
Unless(guard TransitionGuard) ChoiceBuilder
OnTrue(eventBuilderFn func(eventBuilder EventBuilder)) ChoiceTrueBuilder
OnFalse(eventBuilderFn func(eventBuilder EventBuilder)) ChoiceFalseBuilder
}
// ChoiceTrueBuilder inherits from ChoiceBuilder
type ChoiceTrueBuilder interface {
OnFalse(eventBuilderFn func(eventBuilder EventBuilder))
}
// ChoiceFalseBuilder inherits from ChoiceBuilder
type ChoiceFalseBuilder interface {
OnTrue(eventBuilderFn func(eventBuilder EventBuilder))
}
// newChoiceBuilder returns a zero-valued instance of
// ChoiceBuilder, which implements ChoiceBuilder.
func newChoiceBuilder(choiceDef *ChoiceDef) ChoiceBuilder {
return &chosenBuilder{
choiceDef: choiceDef,
}
}
// chosenBuilder implements ChoiceBuilder
type chosenBuilder struct {
choiceDef *ChoiceDef
}
func (builder *chosenBuilder) Label(label string) ChoiceBuilder {
builder.choiceDef.SetLabel(label)
return builder
}
var _ ChoiceBuilder = (*chosenBuilder)(nil)
func (builder *chosenBuilder) Unless(guard TransitionGuard) ChoiceBuilder {
builder.choiceDef.SetUnlessGuard(guard)
return builder
}
func (builder *chosenBuilder) OnTrue(eventBuilderFn func(eventBuilder EventBuilder)) ChoiceTrueBuilder {
builder.choiceDef.SetOnTrue(eventBuilderFn)
return newChoiceTrueBuilder(builder.choiceDef)
}
func (builder *chosenBuilder) OnFalse(eventBuilderFn func(eventBuilder EventBuilder)) ChoiceFalseBuilder {
builder.choiceDef.SetOnFalse(eventBuilderFn)
return newChoiceFalseBuilder(builder.choiceDef)
}
// newChoiceTrueBuilder returns a zero-valued instance of
// ChoiceTrueBuilder, which implements ChoiceTrueBuilder.
func newChoiceTrueBuilder(choiceDef *ChoiceDef) ChoiceTrueBuilder {
return &choiceTrueBuilder{
choiceDef: choiceDef,
}
}
// choiceTrueBuilder implements ChoiceTrueBuilder
type choiceTrueBuilder struct {
choiceDef *ChoiceDef
}
var _ ChoiceTrueBuilder = (*choiceTrueBuilder)(nil)
func (builder *choiceTrueBuilder) OnFalse(eventBuilderFn func(eventBuilder EventBuilder)) {
builder.choiceDef.SetOnFalse(eventBuilderFn)
}
// newChoiceFalseBuilder returns a zero-valued instance of
// ChoiceFalseBuilder, which implements ChoiceFalseBuilder.
func newChoiceFalseBuilder(choiceDef *ChoiceDef) ChoiceFalseBuilder {
return &choiceFalseBuilder{
choiceDef: choiceDef,
}
}
// choiceFalseBuilder implements ChoiceFalseBuilder
type choiceFalseBuilder struct {
choiceDef *ChoiceDef
}
var _ ChoiceFalseBuilder = (*choiceFalseBuilder)(nil)
func (builder *choiceFalseBuilder) OnTrue(eventBuilderFn func(eventBuilder EventBuilder)) {
builder.choiceDef.SetOnTrue(eventBuilderFn)
}