-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum.go
More file actions
457 lines (410 loc) · 10.6 KB
/
enum.go
File metadata and controls
457 lines (410 loc) · 10.6 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package goenum
import (
"encoding/json"
"fmt"
"strings"
)
// Enum represents a basic enum interface
type Enum interface {
String() string
Value() interface{}
IsValid() bool
Description() string
HasAlias(alias string) bool
Aliases() []string
}
// CompositeEnum represents an enum that can be combined with other enums
type CompositeEnum interface {
Enum
// Bitwise operations
Or(other CompositeEnum) CompositeEnum
And(other CompositeEnum) CompositeEnum
Xor(other CompositeEnum) CompositeEnum
Not() CompositeEnum
// Checks
HasFlag(flag CompositeEnum) bool
HasAllFlags(flags ...CompositeEnum) bool
IsEmpty() bool
// Flag manipulation
RemoveFlag(flag CompositeEnum) CompositeEnum
}
// JSONFormat defines how an enum should be serialized to JSON
type JSONFormat int
const (
// JSONFormatName serializes only the enum name (default)
JSONFormatName JSONFormat = iota
// JSONFormatValue serializes only the enum value
JSONFormatValue
// JSONFormatFull serializes a complete struct with all enum information
JSONFormatFull
)
// EnumJSONConfig holds configuration for JSON serialization
type EnumJSONConfig struct {
Format JSONFormat
}
// DefaultJSONConfig returns the default JSON configuration
func DefaultJSONConfig() *EnumJSONConfig {
return &EnumJSONConfig{
Format: JSONFormatName,
}
}
// EnumBase provides a basic implementation of Enum interface
type EnumBase struct {
value interface{}
name string
description string
aliases []string
jsonConfig *EnumJSONConfig
}
// String returns the string representation of the enum
func (e *EnumBase) String() string {
if e == nil {
return ""
}
return e.name
}
// Value returns the value of the enum
func (e *EnumBase) Value() interface{} {
if e == nil {
return nil
}
return e.value
}
// IsValid checks if the enum value is valid
func (e *EnumBase) IsValid() bool {
return e != nil && e.name != ""
}
// Description returns the description of the enum
func (e *EnumBase) Description() string {
if e == nil {
return ""
}
return e.description
}
// HasAlias checks if the enum has a specific alias
func (e *EnumBase) HasAlias(alias string) bool {
if e == nil {
return false
}
for _, a := range e.aliases {
if strings.EqualFold(a, alias) {
return true
}
}
return false
}
// Aliases returns all aliases of the enum
func (e *EnumBase) Aliases() []string {
if e == nil {
return nil
}
return e.aliases
}
// NewEnumSet creates a new EnumSet instance
func NewEnumSet[T Enum]() *EnumSet[T] {
return &EnumSet[T]{
values: make(map[string]T),
byValue: make(map[interface{}]T),
}
}
// EnumSet represents a collection of enum values
type EnumSet[T Enum] struct {
values map[string]T
byValue map[interface{}]T
}
// Register adds an enum value to the set and returns the EnumSet for chaining
func (es *EnumSet[T]) Register(enum T) *EnumSet[T] {
name := enum.String()
value := enum.Value()
// Check for duplicate name
if _, exists := es.values[name]; exists {
panic(fmt.Sprintf("duplicate enum name: %s", name))
}
// Check for duplicate value
if _, exists := es.byValue[value]; exists {
panic(fmt.Sprintf("duplicate enum value: %v", value))
}
es.values[name] = enum
es.byValue[value] = enum
return es
}
// GetByName retrieves an enum by its string name
func (es *EnumSet[T]) GetByName(name string) (T, bool) {
enum, exists := es.values[strings.ToUpper(name)]
if exists {
return enum, true
}
// Check aliases
for _, e := range es.values {
if e.HasAlias(name) {
return e, true
}
}
var zero T
return zero, false
}
// GetByValue retrieves an enum by its value
func (es *EnumSet[T]) GetByValue(value interface{}) (T, bool) {
enum, exists := es.byValue[value]
return enum, exists
}
// Values returns all registered enum values
func (es *EnumSet[T]) Values() []T {
result := make([]T, 0, len(es.values))
for _, v := range es.values {
result = append(result, v)
}
return result
}
// Contains checks if an enum exists in the set
func (es *EnumSet[T]) Contains(enum T) bool {
_, exists := es.values[enum.String()]
return exists
}
// SetJSONConfig sets the JSON serialization configuration
func (e *EnumBase) SetJSONConfig(config *EnumJSONConfig) {
if e == nil {
return
}
e.jsonConfig = config
}
// GetJSONConfig returns the current JSON configuration
func (e *EnumBase) GetJSONConfig() *EnumJSONConfig {
if e == nil || e.jsonConfig == nil {
return DefaultJSONConfig()
}
return e.jsonConfig
}
// MarshalJSON implements JSON marshaling for enum
func (e *EnumBase) MarshalJSON() ([]byte, error) {
if e == nil {
return json.Marshal("")
}
config := e.GetJSONConfig()
switch config.Format {
case JSONFormatValue:
return json.Marshal(e.Value())
case JSONFormatFull:
type FullEnum struct {
Name string `json:"name"`
Value interface{} `json:"value"`
Description string `json:"description"`
Aliases []string `json:"aliases,omitempty"`
}
return json.Marshal(FullEnum{
Name: e.name,
Value: e.value,
Description: e.description,
Aliases: e.aliases,
})
default: // JSONFormatName
return json.Marshal(e.String())
}
}
// UnmarshalJSON implements JSON unmarshaling for enum
func (e *EnumBase) UnmarshalJSON(data []byte) error {
if e == nil {
return fmt.Errorf("cannot unmarshal into nil EnumBase")
}
config := e.GetJSONConfig()
switch config.Format {
case JSONFormatValue:
var value interface{}
if err := json.Unmarshal(data, &value); err != nil {
return err
}
// Convert float64 to int if necessary
if f, ok := value.(float64); ok {
e.value = int(f)
} else {
e.value = value
}
return nil
case JSONFormatFull:
type FullEnum struct {
Name string `json:"name"`
Value interface{} `json:"value"`
Description string `json:"description"`
Aliases []string `json:"aliases,omitempty"`
}
var full FullEnum
if err := json.Unmarshal(data, &full); err != nil {
return err
}
e.name = full.Name
// Convert float64 to int if necessary
if f, ok := full.Value.(float64); ok {
e.value = int(f)
} else {
e.value = full.Value
}
e.description = full.Description
e.aliases = full.Aliases
return nil
default: // JSONFormatName
var name string
if err := json.Unmarshal(data, &name); err != nil {
return err
}
e.name = name
return nil
}
}
// NewEnumBase creates a new EnumBase with the given parameters
func NewEnumBase(value interface{}, name string, description string, aliases ...string) *EnumBase {
return &EnumBase{
value: value,
name: name,
description: description,
aliases: aliases,
jsonConfig: DefaultJSONConfig(),
}
}
// Names returns a slice of all enum names in the set
func (es *EnumSet[T]) Names() []string {
names := make([]string, 0, len(es.values))
for name := range es.values {
names = append(names, name)
}
return names
}
// Map returns a map of enum names to their values
func (es *EnumSet[T]) Map() map[string]interface{} {
result := make(map[string]interface{}, len(es.values))
for name, enum := range es.values {
result[name] = enum.Value()
}
return result
}
// Filter returns a slice of enums that satisfy the given predicate
func (es *EnumSet[T]) Filter(predicate func(T) bool) []T {
result := make([]T, 0)
for _, enum := range es.values {
if predicate(enum) {
result = append(result, enum)
}
}
return result
}
// CompositeEnumBase provides a basic implementation of CompositeEnum interface
type CompositeEnumBase struct {
*EnumBase
flags uint64
}
// NewCompositeEnumBase creates a new CompositeEnumBase with the given parameters
func NewCompositeEnumBase(value interface{}, name string, description string, aliases ...string) *CompositeEnumBase {
flags, ok := value.(uint64)
if !ok {
// If value is not uint64, use 1 << value as the flag
if intVal, ok := value.(int); ok {
flags = 1 << uint(intVal)
} else {
flags = 0
}
}
return &CompositeEnumBase{
EnumBase: NewEnumBase(flags, name, description, aliases...),
flags: flags,
}
}
// Or performs a bitwise OR operation with another enum
func (e *CompositeEnumBase) Or(other CompositeEnum) CompositeEnum {
if e == nil || other == nil {
return e
}
otherBase, ok := other.(*CompositeEnumBase)
if !ok {
return e
}
return &CompositeEnumBase{
EnumBase: NewEnumBase(e.flags|otherBase.flags, e.name+"|"+other.String(), e.description),
flags: e.flags | otherBase.flags,
}
}
// And performs a bitwise AND operation with another enum
func (e *CompositeEnumBase) And(other CompositeEnum) CompositeEnum {
if e == nil || other == nil {
return e
}
otherBase, ok := other.(*CompositeEnumBase)
if !ok {
return e
}
return &CompositeEnumBase{
EnumBase: NewEnumBase(e.flags&otherBase.flags, e.name+"&"+other.String(), e.description),
flags: e.flags & otherBase.flags,
}
}
// Xor performs a bitwise XOR operation with another enum
func (e *CompositeEnumBase) Xor(other CompositeEnum) CompositeEnum {
if e == nil || other == nil {
return e
}
otherBase, ok := other.(*CompositeEnumBase)
if !ok {
return e
}
return &CompositeEnumBase{
EnumBase: NewEnumBase(e.flags^otherBase.flags, e.name+"^"+other.String(), e.description),
flags: e.flags ^ otherBase.flags,
}
}
// Not performs a bitwise NOT operation
func (e *CompositeEnumBase) Not() CompositeEnum {
if e == nil {
return e
}
return &CompositeEnumBase{
EnumBase: NewEnumBase(^e.flags, "~"+e.name, e.description),
flags: ^e.flags,
}
}
// HasFlag checks if the enum has a specific flag set
func (e *CompositeEnumBase) HasFlag(flag CompositeEnum) bool {
if e == nil || flag == nil {
return false
}
flagBase, ok := flag.(*CompositeEnumBase)
if !ok {
return false
}
return (e.flags & flagBase.flags) == flagBase.flags
}
// IsEmpty checks if the enum has no flags set
func (e *CompositeEnumBase) IsEmpty() bool {
return e == nil || e.flags == 0
}
// Value returns the flags value
func (e *CompositeEnumBase) Value() interface{} {
if e == nil {
return nil
}
return e.flags
}
// HasAllFlags checks if all given flags are present in the composite enum
func (e *CompositeEnumBase) HasAllFlags(flags ...CompositeEnum) bool {
if e == nil || len(flags) == 0 {
return false
}
for _, flag := range flags {
if !e.HasFlag(flag) {
return false
}
}
return true
}
// RemoveFlag removes a specific flag from the composite enum
func (e *CompositeEnumBase) RemoveFlag(flag CompositeEnum) CompositeEnum {
if e == nil || flag == nil {
return e
}
flagBase, ok := flag.(*CompositeEnumBase)
if !ok {
return e
}
newFlags := e.flags &^ flagBase.flags
return &CompositeEnumBase{
EnumBase: NewEnumBase(newFlags, e.name+"-"+flag.String(), e.description),
flags: newFlags,
}
}