Skip to content

Commit d52bfc6

Browse files
authored
Refactor how initialize enum (#25)
Add - NameOf - TrueNameOf Modify - NewExtendedSafe -> NewExtended - Combine New and NewSafe -> New Remove: - NewSafe Edit readme
1 parent 3ad5cb9 commit d52bfc6

14 files changed

+430
-219
lines changed

.github/workflows/test.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ jobs:
1515
- name: Set up Go
1616
uses: actions/setup-go@v3
1717
with:
18-
go-version: 1.21.0
18+
go-version: 1.21.1
1919

2020
- run: |
2121
go work init .
2222
go work use ./testing/
2323
go work sync
2424
2525
- name: Test
26-
run: go test -timeout 30s -v -race ./testing/... -coverprofile=cover.out
26+
run: |
27+
go test -timeout 30s -v -race ./...
28+
go test -timeout 30s -v -race ./testing/...

README.md

+38-20
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
[![GitHub release (release name instead of tag name)](https://img.shields.io/github/v/release/xybor-x/enum?include_prereleases)](https://github.com/xybor-x/enum/releases/latest)
55
[![GitHub Repo stars](https://img.shields.io/github/stars/xybor-x/enum?color=blue)](https://github.com/xybor-x/enum)
66

7-
87
![Golang](./.github/go-enum.png)
98

109
# ⚙️ Go Enum
@@ -19,16 +18,45 @@
1918
[6]: #-serialization-and-deserialization
2019
[7]: #-type-safety
2120

22-
> [!WARNING]
23-
> Please keep in mind that `xybor-x/enum` is still under active development
24-
> and therefore full backward compatibility is not guaranteed before reaching v1.0.0.
25-
2621
## 🔧 Installation
2722

2823
```sh
2924
go get -u github.com/xybor-x/enum
3025
```
3126

27+
## ⚡ Quick start
28+
29+
> [!TIP]
30+
> This is just a ⚡ quick definition for general use cases.
31+
> See more advanced [Features](#-features).
32+
33+
> [!CAUTION]
34+
> Enum definitions are not thread-safe.
35+
> Therefore, they should be finalized during initialization (at the global scope).
36+
37+
38+
```go
39+
package main
40+
41+
type role any
42+
type Role = enum.SafeEnum[role]
43+
44+
var (
45+
RoleUser = enum.New[Role]("user")
46+
RoleAdmin = enum.New[Role]("admin")
47+
_ = enum.Finalize[Role]()
48+
)
49+
50+
func main() {
51+
// Print out the string representation of enum.
52+
fmt.Println(RoleAdmin) // Output: admin
53+
54+
// Serialize a user to json format.
55+
data, _ := json.Marshal(RoleUser)
56+
fmt.Println(string(data)) // Output: "user"
57+
}
58+
```
59+
3260
## 📋 Features
3361

3462
> [!TIP]
@@ -41,11 +69,6 @@ go get -u github.com/xybor-x/enum
4169
| **Serialization and deserialization** ([#][6]) | No | **Yes** | **Yes** |
4270
| **Type safety** ([#][7]) | No | Basic | **Strong** |
4371

44-
> [!CAUTION]
45-
> Enum definitions are not thread-safe.
46-
> Therefore, they should be finalized during initialization (at the global scope).
47-
48-
4972
## ⭐ Basic enum
5073

5174
The basic enum (a.k.a `iota` enum) is the most commonly used enum implementation in Go.
@@ -106,11 +129,6 @@ func init() {
106129
enum.Finalize[Role]() // Optional: ensure no new enum values can be added to Role.
107130
}
108131

109-
type User struct {
110-
ID int `json:"id"`
111-
Role Role `json:"role"`
112-
}
113-
114132
func main() {
115133
// WrapEnum has many built-in methods for handling enum easier.
116134
data, _ := json.Marshal(RoleUser) // Output: "user"
@@ -140,8 +158,8 @@ type role any
140158
type Role = enum.SafeEnum[role] // NOTE: It must use type alias instead of type definition.
141159

142160
var (
143-
RoleUser = enum.NewSafe[Role]("user")
144-
RoleAdmin = enum.NewSafe[Role]("admin")
161+
RoleUser = enum.New[Role]("user")
162+
RoleAdmin = enum.New[Role]("admin")
145163
)
146164

147165
func main() {
@@ -301,9 +319,9 @@ type role any
301319
type Role struct { enum.SafeEnum[role] }
302320

303321
var (
304-
RoleUser = enum.NewExtendedSafe[Role]("user")
305-
RoleMod = enum.NewExtendedSafe[Role]("mod")
306-
RoleAdmin = enum.NewExtendedSafe[Role]("admin")
322+
RoleUser = enum.NewExtended[Role]("user")
323+
RoleMod = enum.NewExtended[Role]("mod")
324+
RoleAdmin = enum.NewExtended[Role]("admin")
307325
_ = enum.Finalize[Role]()
308326
)
309327

0 commit comments

Comments
 (0)