-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_usage.go
More file actions
134 lines (109 loc) · 4.3 KB
/
Copy pathexample_usage.go
File metadata and controls
134 lines (109 loc) · 4.3 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
package goenum
import (
"encoding/json"
"fmt"
)
// Status represents an example enum type
type Status struct {
*EnumBase
}
var (
StatusPending = Status{NewEnumBase(0, "PENDING", "The item is waiting to be processed", "WAITING")}
StatusActive = Status{NewEnumBase(1, "ACTIVE", "The item is currently active", "RUNNING", "LIVE")}
StatusDeleted = Status{NewEnumBase(2, "DELETED", "The item has been deleted", "REMOVED")}
)
var StatusEnumSet = NewEnumSet[Status]()
func init() {
// Using chainable Register method
StatusEnumSet.Register(StatusPending).
Register(StatusActive).
Register(StatusDeleted)
}
// MarshalJSON implements JSON marshaling for Status
func (s Status) MarshalJSON() ([]byte, error) {
if s.EnumBase == nil {
return json.Marshal("")
}
return s.EnumBase.MarshalJSON()
}
// UnmarshalJSON implements JSON unmarshaling for Status
func (s *Status) UnmarshalJSON(data []byte) error {
if s.EnumBase == nil {
s.EnumBase = &EnumBase{}
}
return s.EnumBase.UnmarshalJSON(data)
}
// Example demonstrates the usage of the improved enum package
func Example() {
// Basic enum operations
fmt.Printf("Status: %s, Value: %v, Description: %s\n",
StatusActive.String(),
StatusActive.Value(),
StatusActive.Description())
// Check aliases
fmt.Printf("Has alias 'RUNNING': %v\n", StatusActive.HasAlias("RUNNING"))
fmt.Printf("All aliases: %v\n", StatusActive.Aliases())
// EnumSet operations
if status, exists := StatusEnumSet.GetByName("ACTIVE"); exists {
fmt.Printf("Found by name: %s\n", status.String())
}
if status, exists := StatusEnumSet.GetByValue(1); exists {
fmt.Printf("Found by value: %s\n", status.String())
}
// Try finding by alias
if status, exists := StatusEnumSet.GetByName("WAITING"); exists {
fmt.Printf("Found by alias: %s\n", status.String())
}
// JSON operations with different formats
// Default format (name only)
jsonData, _ := json.Marshal(StatusActive)
fmt.Printf("Default JSON: %s\n", jsonData)
// Value format
StatusActive.SetJSONConfig(&EnumJSONConfig{Format: JSONFormatValue})
jsonData, _ = json.Marshal(StatusActive)
fmt.Printf("Value JSON: %s\n", jsonData)
// Full format
StatusActive.SetJSONConfig(&EnumJSONConfig{Format: JSONFormatFull})
jsonData, _ = json.Marshal(StatusActive)
fmt.Printf("Full JSON: %s\n", jsonData)
// Unmarshal examples
var status Status
status.EnumBase = &EnumBase{}
// Unmarshal name format
_ = json.Unmarshal([]byte(`"PENDING"`), &status)
fmt.Printf("Unmarshaled name: %s\n", status.String())
// Unmarshal value format
status.SetJSONConfig(&EnumJSONConfig{Format: JSONFormatValue})
_ = json.Unmarshal([]byte(`1`), &status)
fmt.Printf("Unmarshaled value: %v\n", status.Value())
// Unmarshal full format
status.SetJSONConfig(&EnumJSONConfig{Format: JSONFormatFull})
fullJSON := `{"name":"ACTIVE","value":1,"description":"The item is currently active","aliases":["RUNNING","LIVE"]}`
_ = json.Unmarshal([]byte(fullJSON), &status)
fmt.Printf("Unmarshaled full: %s (value: %v, desc: %s)\n",
status.String(), status.Value(), status.Description())
// New utility methods examples
fmt.Printf("All status names: %v\n", StatusEnumSet.Names())
fmt.Printf("Status map: %v\n", StatusEnumSet.Map())
// Filter active and pending statuses
activeStatuses := StatusEnumSet.Filter(func(s Status) bool {
return s.Value().(int) < 2 // Filter statuses with value less than 2
})
fmt.Printf("Active statuses: %v\n", activeStatuses)
// Composite enum examples
var (
PermissionRead = NewCompositeEnumBase(0, "READ", "Read permission")
PermissionWrite = NewCompositeEnumBase(1, "WRITE", "Write permission")
PermissionExec = NewCompositeEnumBase(2, "EXEC", "Execute permission")
)
// Combine permissions
allPermissions := PermissionRead.Or(PermissionWrite).Or(PermissionExec)
fmt.Printf("All permissions: %s (value: %v)\n", allPermissions.String(), allPermissions.Value())
// Check multiple flags
fmt.Printf("Has read and write: %v\n", allPermissions.HasAllFlags(PermissionRead, PermissionWrite))
fmt.Printf("Has read and exec: %v\n", allPermissions.HasAllFlags(PermissionRead, PermissionExec))
// Remove permission
readWriteOnly := allPermissions.RemoveFlag(PermissionExec)
fmt.Printf("Read and write only: %s (value: %v)\n", readWriteOnly.String(), readWriteOnly.Value())
fmt.Printf("Still has exec: %v\n", readWriteOnly.HasFlag(PermissionExec))
}