-
Notifications
You must be signed in to change notification settings - Fork 0
/
question.go
54 lines (42 loc) · 857 Bytes
/
question.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
package faq
type Reply struct {
Text string `json:"text"`
Choices []string `json:"choices"`
}
type Question struct {
Text string
Answer string
Choices []Question
}
func (q *Question) getChoicesText() []string {
texts := make([]string, 0)
for _, choice := range q.Choices {
texts = append(texts, choice.Text)
}
return texts
}
func (q *Question) findChoiceQuestion(text string) Question {
var found Question
for _, choice := range q.Choices {
if choice.Text == text {
found = choice
break
}
}
return found
}
func (q *Question) HasChoices() bool {
return len(q.Choices) > 0
}
func (q *Question) Ask(text string) interface{} {
if !q.HasChoices() {
return q.Answer
}
return q.findChoiceQuestion(text)
}
func (q *Question) Reply() Reply {
return Reply{
Text: q.Answer,
Choices: q.getChoicesText(),
}
}