-
Notifications
You must be signed in to change notification settings - Fork 0
/
categories.go
90 lines (80 loc) · 1.47 KB
/
categories.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 ironclad
import (
"encoding/json"
)
type Category int
const (
Invalid Category = iota
General
Appliances
Books
Electronics
Jobs
Furniture
Housing
Cars
Bikes
)
var AllCategories = []Category{
General,
Appliances,
Books,
Electronics,
Jobs,
Furniture,
Housing,
Cars,
Bikes,
}
func ParseCategory(c string) Category {
return map[string]Category{
"": General,
"appliances": Appliances,
"books": Books,
"electronics": Electronics,
"jobs": Jobs,
"furniture": Furniture,
"housing": Housing,
"cars": Cars,
"bikes": Bikes,
}[c]
}
func (c Category) String() string {
return map[Category]string{
Invalid: "(invalid category)",
General: "General",
Appliances: "Appliances",
Books: "Books",
Electronics: "Electronics",
Jobs: "Jobs",
Furniture: "Furniture",
Housing: "Housing",
Cars: "Cars",
Bikes: "Bikes",
}[c]
}
func (c Category) ForURL() string {
return map[Category]string{
General: "",
Appliances: "appliances",
Books: "books",
Electronics: "electronics",
Jobs: "jobs",
Furniture: "furniture",
Housing: "housing",
Cars: "cars",
Bikes: "bikes",
}[c]
}
func (c *Category) MarshalJSON() ([]byte, error) {
return json.Marshal(c.ForURL())
}
func (c *Category) UnmarshalJSON(b []byte) error {
v := ""
err := json.Unmarshal(b, &v)
if err != nil {
return err
}
*c = ParseCategory(v)
return nil
}