-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum.go
205 lines (182 loc) · 5.07 KB
/
enum.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// A struct based golang enum package that handles stringifying, marshalling, and unmarshalling.
// All examples will be built from the following example types
// type CurrencyCodes struct {
// enum.Enum
// USD enum.Const
// Custom enum.Const `enum:"CUSTOM"`
// }
//
// type Money struct {
// CurrencyCode CurrencyCode `json:"currency_code"`
// Amount int `json:"amount"`
// }
package enum
import (
"fmt"
"github.com/pkg/errors"
"reflect"
"strconv"
)
const invalidEnumErrorMsg = "%s is not a valid enum"
const enumNotConstructedErrorMsg = "cannot set a value on an enum that has not be constructed"
const enumNotNilErrorMsg = "cannot set a value on an enum that has not be constructed"
type Enummer interface {
Get() Const
Set(c Const) error
MustSet(c Const)
GetAll() []Const
// Sets the value without any checks. This is a dangerous method and should pretty
// much never be used but if you do, USE WITH CAUTION.
unsafeSet(c Const)
// Adds an additional valid value. This is a dangerous method and should pretty
// much never be used but if you do, USE WITH CAUTION.
unsafeAdd(c Const)
}
// The base type for all Enums. Stores the current value and keeps track of all valid values.
type Enum struct {
val Const
vals []Const
}
// The base value for all Enum fields. The name of the field on the enum struct will be
// the default value for the enum const. For example
// The value of the USD const is "USD". In order to customize this value, add the tag enum:"<name>"
// for example
// type CurrencyCodes struct {
// enum.Enum
// USD enum.Const `enum:"not usd"`
// }
// The value of the USD const is now "not usd"
type Const string
func (e *Enum) unsafeAdd(c Const) {
if !contains(e.vals, c) {
e.vals = append(e.vals, c)
}
}
// A list of all possible Consts on the enum
func (e *Enum) GetAll() []Const {
return e.vals
}
func (e *Enum) unsafeSet(c Const) {
e.val = c
}
func (e Enum) String() string {
return string(e.Get())
}
// Unmarshalls the string into an Enum. NOTE: you must run enum.Validate
// after unmarshalling a string like so
// func main() {
// var money Money
//
// json.Unmarshal([]byte("{\"currency_code\":\"USD\",\"amount\":5}"), &money)
// enum.Validate(&money.CurrencyCode) // <-- Must be run after unmarshal
//
// fmt.Println(money) // Prints "{USD 5}"
// }
func (e *Enum) UnmarshalJSON(b []byte) error {
s, _ := strconv.Unquote(string(b))
c := Const(s)
e.unsafeSet(c)
return nil
}
func (e Enum) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(string(e.val))), nil
}
// Gets the value stored on the enum
func (e *Enum) Get() Const {
return e.val
}
// Set the value stored on the enum. Returns an error if value is invalid
func (e *Enum) Set(c Const) error {
if e.vals != nil {
if contains(e.GetAll(), c) {
e.val = c
return nil
} else {
return errors.New(fmt.Sprintf(invalidEnumErrorMsg, c))
}
} else {
return errors.New(enumNotConstructedErrorMsg)
}
}
// Set the value stored on the enum. Panics if the value is invalid
func (e *Enum) MustSet(s Const) {
err := e.Set(s)
if err != nil {
panic(err.Error())
}
}
// Creates a new Enummer with no value set
// cc := enum.New(new(CurrencyCodes)).(*CurrencyCodes)
func New(e Enummer) Enummer {
construct(e)
return e
}
// Instantiates an Enum with the provided value. If the value is invalid, an error is returned
// otherwise, an Enummer is returned with a nil error
// cc, err := enum.Construct(new(CurrencyCodes), enum.Const("USD"))
// if err != nil {
// panic(err)
// }
// cc = cc.(*CurrencyCodes)
func Construct(e Enummer, c Const) (Enummer, error) {
if e == nil {
return nil, errors.New(enumNotNilErrorMsg)
}
construct(e)
if err := e.Set(c); err != nil {
return nil, err
}
return e, nil
}
// Instantiates an Enum with the provided value. If the value is invalid, a panic occurs
// cc := enum.MustConstruct(new(CurrencyCodes), enum.Const("USD")).(*CurrencyCodes)
func MustConstruct(e Enummer, c Const) Enummer {
out, err := Construct(e, c)
if err != nil {
panic(err.Error())
}
return out
}
// Instantiates the enum if that hasn't been done and validates that its current value is valid.
// Commonly used after unmarshalling an enum like so
// func main() {
// var money Money
//
// json.Unmarshal([]byte("{\"currency_code\":\"USD\",\"amount\":5}"), &money)
// enum.Validate(&money.CurrencyCode) // <-- Must be run after unmarshal
//
// fmt.Println(money) // Prints "{USD 5}"
// }
func Validate(e Enummer) error {
if e.GetAll() == nil {
construct(e)
}
if !contains(e.GetAll(), e.Get()) {
return errors.New(fmt.Sprintf(invalidEnumErrorMsg, e.Get()))
}
return nil
}
func contains(cs []Const, c Const) bool {
for _, v := range cs {
if v == c {
return true
}
}
return false
}
func construct(e Enummer) {
v := reflect.ValueOf(e).Elem()
for i := 0; i < v.NumField(); i++ {
t := v.Type().Field(i)
f := v.Field(i)
if _, ok := f.Interface().(Const); ok {
s := t.Tag.Get("enum")
if s == "" {
s = t.Name
}
c := Const(s)
e.unsafeAdd(c)
f.Set(reflect.ValueOf(c))
}
}
}