-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
90 lines (69 loc) · 1.52 KB
/
driver.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
package faq
type Driver struct {
RootQuestion Question
BackCmd string
RepeatCmd string
ResetCmd string
stack Stack
}
func NewDriver(rootQuestion Question, backCmd string, repeatCmd string, resetCmd string) *Driver {
d := Driver{
RootQuestion: rootQuestion,
BackCmd: backCmd,
RepeatCmd: repeatCmd,
ResetCmd: resetCmd,
stack: NewStack(),
}
d.stack.Push(rootQuestion)
return &d
}
func (d *Driver) getCommandChoices() []string {
choices := []string{d.BackCmd, d.RepeatCmd, d.ResetCmd}
return choices
}
func (d *Driver) CurrentQuestion() interface{} {
return d.stack.Peek(0)
}
func (d *Driver) PreviousQuestion() interface{} {
return d.stack.Peek(1)
}
func (d *Driver) Boot() Reply {
return d.RootQuestion.Reply()
}
func (d *Driver) Ask(cmd string) Reply {
switch cmd {
case d.BackCmd:
return d.Back()
case d.RepeatCmd:
return d.Repeat()
case d.ResetCmd:
return d.Reset()
}
q, ok := d.CurrentQuestion().(Question)
if !ok {
return Reply{Text: "Answer is not available"}
}
nextQ, _ := q.Ask(cmd).(Question)
d.stack.Push(nextQ)
if !nextQ.HasChoices() {
return Reply{
Text: nextQ.Answer,
Choices: d.getCommandChoices(),
}
}
return nextQ.Reply()
}
func (d *Driver) Back() Reply {
d.stack.Pop()
q, _ := d.CurrentQuestion().(Question)
return q.Reply()
}
func (d *Driver) Repeat() Reply {
q, _ := d.CurrentQuestion().(Question)
return q.Reply()
}
func (d *Driver) Reset() Reply {
d.stack = Stack{}
d.stack.Push(d.RootQuestion)
return d.Boot()
}