-
Notifications
You must be signed in to change notification settings - Fork 0
/
category.go
77 lines (66 loc) · 1.36 KB
/
category.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
package object
import (
"fmt"
"strings"
)
type Category int32
//go:generate stringer -type=Category
const (
None Category = iota
Weapon
Wand
Staff
Treasure
Armor
Food
Other
)
type CategorySet map[Category]bool
func (cs CategorySet) ToInt32List() (result []int32) {
for k, v := range cs {
if v {
result = append(result, int32(k))
}
}
return
}
func (cs CategorySet) Add(c Category) {
cs[c] = true
}
func (cs CategorySet) Contains(c Category) bool {
return cs[c]
}
func IntsToCategories(rawcats []int32) (result []Category) {
for _, i := range rawcats {
result = append(result, Category(i))
}
return
}
func CategoriesToString(cats []Category) string {
if len(cats) == 0 {
return ""
} else {
var strs []string
for _, c := range cats {
strs = append(strs, c.String())
}
return strings.Join(strs, ", ")
}
}
func StringToCategory(categoryName string) (Category, error) {
if categoryName == "" {
return None, nil
}
categoryName = strings.ToUpper(categoryName)
stridx := strings.Index(strings.ToUpper(_Category_name), categoryName)
if stridx < 0 {
return None, fmt.Errorf("category '%s' not found", categoryName)
}
for pos, catidx := range _Category_index {
if stridx == int(catidx) {
return Category(pos), nil
}
}
// shouldn't happen?
return None, fmt.Errorf("could not find index %d for category '%s'", stridx, categoryName)
}