forked from ungerik/go-start
/
multiplechoice.go
61 lines (49 loc) · 1.29 KB
/
multiplechoice.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
package model
import (
"strings"
"github.com/ungerik/go-start/errs"
"github.com/ungerik/go-start/utils"
)
type MultipleChoice []string
func (self *MultipleChoice) Get() []string {
return []string(*self)
}
func (self *MultipleChoice) Set(value ...string) {
*self = MultipleChoice(value)
}
func (self *MultipleChoice) String() string {
return strings.Join([]string(*self), "|")
}
func (self *MultipleChoice) SetString(str string) error {
*self = MultipleChoice(strings.Split(str, "|"))
return nil
}
func (self *MultipleChoice) IsSet(option string) bool {
return utils.StringIn(option, self.Get())
}
func (self *MultipleChoice) IsEmpty() bool {
return len(*self) == 0
}
func (self *MultipleChoice) Options(metaData *MetaData) []string {
return choiceOptions(metaData)
}
func (self *MultipleChoice) Required(metaData *MetaData) bool {
return false
}
func (self *MultipleChoice) Validate(metaData *MetaData) error {
options := self.Options(metaData)
if len(options) == 0 {
return errs.Format("model.MultipleChoice needs options")
}
for _, option := range options {
if option == "" {
return errs.Format("model.MultipleChoice options must not be empty strings")
}
}
for _, str := range *self {
if !utils.StringIn(str, options) {
return &InvalidChoice{str, options}
}
}
return nil
}