enums

module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT

README

enumgen

enumgen is a type-safe enum generator for Go. It generates robust, type-safe wrappers around string constants, providing validation, string conversion, and helper methods.

Features

  • Type Safety: Generates custom types for enums to prevent accidental misuse.
  • Validation: Includes an IsValid() method to check if a value is a valid enum member.
  • String Support: Implements the fmt.Stringer interface.
  • Parsing: Provides a FromString() method to safely convert strings to enum types.
  • Auto-testing: Automatically generates unit tests for the generated enums.
  • Easy Integration: Designed to work seamlessly with go generate.

Installation

go install github.com/dekey/enums/cmd/enumgen@latest

Usage

  1. Define your constants in a Go file.
  2. Add the //go:generate enumgen --name=<Name> directive above the const block.
Example

In internal/examples/enums/contract.go:

package enums

//go:generate enumgen --name=Role
const (
	admin  = "admin"
	editor = "editor"
	viewer = "viewer"
)

Run the generator:

go generate ./...
Generated Code API

The generator will create enum_role_gen.go with the following API:

type RoleType string

var RoleTypes struct {
    Admin() RoleType
    Editor() RoleType
    Viewer() RoleType
    FromString(string) (RoleType, error)
}

func (e RoleType) IsValid() bool
func (e RoleType) String() string

Development

The project includes a Makefile for common tasks:

  • make build: Build the enumgen binary.
  • make test: Run tests.
  • make lint: Run the linter.
  • make format: Format the code.

License

MIT (See LICENSE)

Directories

Path Synopsis
cmd
enumgen command
examples
enums
Package enums is generated by https://github.com/dekey/enumgen; DO NOT EDIT;
Package enums is generated by https://github.com/dekey/enumgen; DO NOT EDIT;
internal
app

Jump to

Keyboard shortcuts

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