enum

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2025 License: MIT Imports: 3 Imported by: 0

README

Enum Library

This Go library provides a utility for creating and managing enums in a structured and type-safe manner. The library supports both string and integer enums, as well as nested enums.

Features

  • String Enums: Automatically initializes string fields with their field names.
  • Integer Enums: Supports custom integer values using struct tags.
  • Nested Enums: Allows defining enums with nested structures.

Installation

To install the library, use:

go get github.com/tnnmigga/enum@latest

Usage

String Enums
var HttpStatus = New[struct {
	StatusOK                  string
	StatusNotFound            string
	StatusInternalServerError string
}]()

fmt.Println(HttpStatus.StatusOK) // Output: "StatusOK"
Integer Enums
var HttpStatus = New[struct {
	StatusOK                  int `enum:"200"`
	StatusNotFound            int `enum:"404"`
	StatusInternalServerError int `enum:"500"`
}]()

fmt.Println(HttpStatus.StatusOK) // Output: 200
Nested Enums
var HttpStatus = New[struct {
	Code struct {
		StatusOK                  int `enum:"200"`
		StatusNotFound            int `enum:"404"`
		StatusInternalServerError int `enum:"500"`
	}
	Type struct {
		StatusOK                  string
		StatusNotFound            string
		StatusInternalServerError string
	}
}]()

fmt.Println(HttpStatus.Code.StatusOK) // Output: 200
fmt.Println(HttpStatus.Type.StatusOK) // Output: "StatusOK"

Testing

The library includes comprehensive tests to ensure its functionality. Run the tests using:

go test ./...

Contributing

Contributions are welcome! Please fork the repository and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains added in v1.0.2

func Contains[T enumerable](enum any, value T) bool

Contains checks if the enum has a top-level field with the same type and value as the provided value. It does not recursively check nested structs. Returns true if a matching field is found, false otherwise.

func Keys added in v1.0.2

func Keys(enum any) []string

Keys returns a slice of the names of all top-level fields in the enum. It does not include fields from nested structs or unexported fields.

func New

func New[T any]() T

New initializes an enum instance of type T, which must be a struct. Fields are populated based on their names (for strings), indices (for integers), or values specified in the "enum" tag. Supports nested structs, which are initialized recursively. Pointer fields are not allowed. Panics if T is not a struct, if unsupported field types (including pointers) are used, or if integer values overflow the target field type.

func Values added in v1.0.2

func Values[T enumerable](enum any) []T

Values returns a slice of the values of all top-level fields in the enum that match the type T. T must be an integer or string type. It does not include values from nested structs or unexported fields.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL