package vote import ( "fmt" "strings" ) // Choice defines a custom int8 type for use as choice in a vote. type Choice int8 // String implements fmt.Stringer func (choice Choice) String() string { switch choice { case Yes: return "YIP" case No: return "NOPE" case Abstain: return "DUNNO" default: panic("this code should never be reached") } } // Available choices are: const ( Abstain Choice = 0 Yes Choice = 1 No Choice = -1 ) // ValidChoices returns a []Choice with all available choices. func ValidChoices() []Choice { return []Choice{Yes, No, Abstain} } // ChoiceFromString takes a string and returns an initialized Choice or an // error if the string couldn't be parsed. func ChoiceFromString(s string) (Choice, error) { for _, c := range ValidChoices() { if strings.ToUpper(c.String()) == strings.ToUpper(s) { return c, nil } } return Abstain, fmt.Errorf("invalid choice: %s", s) }