Documentation
¶
Overview ¶
Package enum provides a way to define and work with enums in Go. The main benefit is to have IsValid function with zero-maintenance.
Example (Empty) ¶
package main
import (
"fmt"
"github.com/0xcafe-io/enum"
)
func main() {
type Nothing int
if err := enum.Validate[Nothing](1); err != nil {
fmt.Println(err)
}
}
Output: Nothing doesn't have any definition
Example (Int) ¶
package main
import (
"fmt"
"github.com/0xcafe-io/enum"
)
type Access int
var AccessComment = enum.Def[Access](2)
func main() {
var access Access
if !enum.IsValid(access) {
fmt.Println("access denied")
}
access = 2
if enum.IsValid(access) {
fmt.Println("access granted")
}
if access == AccessComment {
fmt.Println("can comment")
}
if err := enum.Validate[Access](99); err != nil {
fmt.Println(err)
}
}
Output: access denied access granted can comment 99 is not a valid choice, allowed values are: 1, 2, 4
Example (Scope) ¶
package main
import (
"fmt"
"github.com/0xcafe-io/enum"
)
func main() {
type OrderStatus string
type UserStatus string
var (
OrderStatusPending = enum.Def[OrderStatus]("pending")
OrderStatusInProgress = enum.Def[OrderStatus]("in_progress")
UserStatusActive = enum.Def[UserStatus]("pending")
UserStatusBanned = enum.Def[UserStatus]("banned")
)
_, _, _, _ = OrderStatusPending, OrderStatusInProgress, UserStatusActive, UserStatusBanned
if enum.IsValid[OrderStatus]("pending") {
fmt.Println("pending is a valid order status")
}
if enum.IsValid[UserStatus]("pending") {
fmt.Println("pending is a valid user status")
}
if !enum.IsValid[OrderStatus]("banned") {
fmt.Println("banned is not a valid order status")
}
if !enum.IsValid[UserStatus]("in_progress") {
fmt.Println("in_progress is not a valid user status")
}
}
Output: pending is a valid order status pending is a valid user status banned is not a valid order status in_progress is not a valid user status
Example (String) ¶
package main
import (
"fmt"
"github.com/0xcafe-io/enum"
)
type Status string
var StatusMerged = enum.Def[Status]("merged")
func main() {
userInput := "postponed"
status := Status(userInput)
if !enum.IsValid(status) {
fmt.Println("bad status")
}
if err := enum.Validate(status); err != nil {
fmt.Println(err)
}
status = "merged"
if enum.IsValid(status) {
fmt.Println("good status")
}
if status == StatusMerged {
fmt.Println("nice job")
}
statuses := enum.ValuesOf[Status]()
fmt.Println(statuses)
}
Output: bad status "postponed" is not a valid choice, allowed values are: "draft", "open", "merged", "closed" good status nice job [draft open merged closed]
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Def ¶
func Def[T enumType](v T) T
Def defines v as a valid value of enum T and returns it. Value is returned as-is, without any wrapping or conversion. Duplicate definitions are ignored. Usage:
type Status string
var (
StatusDraft = enum.Def(Status("draft")) // type of StatusDraft is Status
StatusOpen = enum.Def[Status]("open") // same thing, alternative syntax
)
func Validate ¶
func Validate[T enumType](v T) error
Validate checks whether v is defined for enum T. If not, returns an error, otherwise returns nil.
func ValuesOf ¶
func ValuesOf[T enumType]() []T
ValuesOf returns defined values of enum T. Values are returned in the order they were mentioned (see https://go.dev/ref/spec#Package_initialization). It is safe to modify the returned slice.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.