-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample_test.go
More file actions
180 lines (146 loc) · 3.85 KB
/
example_test.go
File metadata and controls
180 lines (146 loc) · 3.85 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
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]()
)
// 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]()
)
// Print all enum values.
for _, e := range enum.All[Role]() {
fmt.Println(enum.ToString(e))
}
// Output:
// user
// admin
}
func ExampleWrapEnum() {
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]()
)
// 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() {
type role string
type Role = enum.SafeEnum[role]
var (
RoleUser = enum.New[Role]("user")
RoleAdmin = enum.New[Role]("admin")
_ = enum.Finalize[Role]()
)
// As Role is a SafeEnum, it can utilize methods from SafeEnum, 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
}
func ExampleNullable() {
type Role int
type NullRole = enum.Nullable[Role]
var (
_ = enum.New[Role]("user")
RoleAdmin = enum.New[Role]("admin")
_ = enum.Finalize[Role]()
)
// Define a struct that includes the Role enum.
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Role NullRole `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: NullRole{Enum: RoleAdmin, Valid: true}}
data, _ = json.Marshal(user1)
fmt.Println(string(data))
// Output:
// {"id":0,"name":"","role":null}
// {"id":0,"name":"tester","role":"admin"}
}