-
Notifications
You must be signed in to change notification settings - Fork 2
/
scenario.go
49 lines (41 loc) · 1016 Bytes
/
scenario.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
package chatgo
type Condition func(Object) bool
type Behavior func(Object) (Scenario, Object)
type Scenario interface {
Next(Object) (Scenario, Object)
}
func RunScenario(scenario Scenario, o Object) Object {
next := scenario
input := o
for {
if next != nil {
next, input = next.Next(input)
} else {
return input
}
}
}
type CondScenario struct {
conditions []Condition
behaviors []Behavior
elseBehavior Behavior
}
func (scenario *CondScenario) Add(condition Condition, behavior Behavior) {
scenario.conditions = append(scenario.conditions, condition)
scenario.behaviors = append(scenario.behaviors, behavior)
}
func (scenario *CondScenario) Else(behavior Behavior) {
scenario.elseBehavior = behavior
}
func (scenario CondScenario) Next(o Object) (Scenario, Object) {
for i, condition := range scenario.conditions {
if condition(o) {
return scenario.behaviors[i](o)
}
}
if scenario.elseBehavior != nil {
return scenario.elseBehavior(o)
} else {
return nil, nil
}
}