-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_test.go
152 lines (124 loc) · 3.5 KB
/
example_test.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
package enum_test
import (
"encoding/json"
"fmt"
"github.com/xybor-x/enum"
)
func ExampleNew() {
type Role int
// Define enum values for Role.
var (
RoleUser = enum.New[Role]("user")
RoleAdmin = enum.New[Role]("admin")
_ = enum.Finalize[Role]() // Optional: ensure no new enum values can be added to Role.
)
// Print the string representation of enum values.
fmt.Println("string(RoleUser):", enum.ToString(RoleUser))
fmt.Println("string(RoleAdmin):", enum.ToString(RoleAdmin))
// Demonstrate converting string to enum value.
fmt.Println(`enum("user"):`, enum.MustFromString[Role]("user"))
fmt.Println(`enum("admin"):`, enum.MustFromString[Role]("admin"))
// Output:
// string(RoleUser): user
// string(RoleAdmin): admin
// enum("user"): 0
// enum("admin"): 1
}
func ExampleMap() {
type Role int
// Define enum values for Role using iota.
const (
RoleUser Role = iota
RoleAdmin
)
// Map string representations to enum values.
var (
_ = enum.Map(RoleUser, "user")
_ = enum.Map(RoleAdmin, "admin")
_ = enum.Finalize[Role]() // Optional: ensure no new enum values can be added to Role.
)
// Print all enum values.
for _, e := range enum.All[Role]() {
fmt.Println(enum.ToString(e))
}
// Output:
// user
// admin
}
func ExampleWrapEnum() {
// Define a generic enum type
type role any
type Role = enum.WrapEnum[role]
// Define enum values for Role using iota
const (
RoleUser Role = iota
RoleAdmin
)
// Map string representations to enum values
var (
_ = enum.Map(RoleUser, "user")
_ = enum.Map(RoleAdmin, "admin")
_ = enum.Finalize[Role]() // Optional: ensure no new enum values can be added to Role.
)
// Define a struct that includes the Role enum.
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Role Role `json:"role"`
}
// Serialize zero struct
data, _ := json.Marshal(User{})
fmt.Println(string(data))
// Serialize the User struct to JSON.
user1 := User{ID: 0, Name: "tester", Role: RoleAdmin}
data, _ = json.Marshal(user1)
fmt.Println(string(data))
// Deserialize JSON back into a User struct and print the Role.
user2 := User{}
json.Unmarshal(data, &user2)
fmt.Println(user2.Role)
// Output:
// {"id":0,"name":"","role":"user"}
// {"id":0,"name":"tester","role":"admin"}
// admin
}
func ExampleSafeEnum() {
// Define a generic enum type
type role string
type Role = enum.SafeEnum[role]
// Define enum values for Role using iota
var (
RoleUser = enum.NewSafe[Role]("user")
RoleAdmin = enum.NewSafe[Role]("admin")
_ = enum.Finalize[Role]() // Optional: ensure no new enum values can be added to Role.
)
// As Role is a StructEnum, it can utilize methods from StructEnum, including
// utility functions and serde operations.
fmt.Println(RoleUser.GoString()) // 0 (user)
fmt.Println(RoleUser.IsValid()) // true
// Define a struct that includes the Role enum.
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Role Role `json:"role"`
}
// Serialize zero struct
data, err := json.Marshal(User{})
if err != nil {
fmt.Println("cannot serialize zero struct")
}
// Serialize the User struct to JSON.
user1 := User{ID: 0, Name: "tester", Role: RoleAdmin}
data, _ = json.Marshal(user1)
fmt.Println(string(data))
// Deserialize JSON back into a User struct and print the Role.
user2 := User{}
json.Unmarshal(data, &user2)
fmt.Println(user2.Role)
// Output:
// 0 (user)
// true
// cannot serialize zero struct
// {"id":0,"name":"tester","role":"admin"}
// admin
}