PitchHut logo
goenums
by zarldev
Generate type-safe enums for Go with ease and efficiency.
Pitch

goenums addresses the lack of native enum support in Go by transforming simple constant declarations into comprehensive, type-safe enum implementations. With features like string conversion, validation, JSON handling, and database integration, it enhances the language's capabilities, ensuring safety and ease of use.

Description

goenums provides a solution to Go's absence of native enum support by generating comprehensive, type-safe enum implementations from simple constant declarations. It transforms basic iota based constants into advanced enums featuring string conversion, validation, JSON handling, database integration, and additional functionalities.

Key Features

  • Type Safety: Prevents accidental misuse of enum values through wrapper types.
  • String Conversion: Automatically generates string representations and facilitates parsing.
  • JSON Support: Built-in capabilities for marshaling and unmarshaling JSON data.
  • Database Integration: Implements SQL Scanner and Valuer interfaces for seamless database interactions.
  • Validation: Methods available to ensure validity of enum values.
  • Iteration: Supports modern Go 1.21+ iteration while providing a fallback for legacy versions.
  • Extensibility: Allows addition of custom fields to enums via comments.
  • Exhaustive Handling: Provides helper functions to guarantee all enum values are addressed in your code.
  • Zero Dependencies: Utilizes only Go’s standard library, ensuring a lightweight integration.

Usage

To utilize goenums, include it in your project with a simple command:

goenums [options] filename

Options vary and allow for features like failfast mode, legacy generation, case-insensitive parsing, and more. Below is a basic usage example:

package validation

type status int

//go:generate goenums status.go
const (
	unknown status = iota // invalid
	failed
	passed
	...
)

Run go generate ./... to generate your enum implementations and safely use them throughout your Go code.

Advanced Features

Custom String Representations

Handling custom string representations for enums becomes straightforward with goenums. For example, alternative names with spaces are easily accommodated:

const (
	unknown status = iota // invalid "Not Found"
	...
)

Extended Enum Types with Custom Fields

Enums can integrate custom fields by defining them in type comments, making the resulting enums richer and more informative:

type planet int // Gravity float64, RadiusKm float64, ...

Case Insensitive String Parsing

With the -insensitive flag, enums will recognize variations in case:

status, err := validation.ParseStatus("Pending")

JSON & Database Storage

The generated enum types support JSON encoding and decoding and can be directly integrated with databases, ensuring data consistency across layers:

func (p Status) MarshalJSON() ([]byte, error) {
	return []byte(`"` + p.String() + `"`), nil
}

Exhaustive Enum Processing

Ensure that all enum values are considered in logic to avoid potential errors, especially beneficial in testing situations:

validation.ExhaustiveStatuses(func(status validation.Status) {
	...
})

For a comprehensive overview of the available features, please refer to the documentation.

With goenums, developers can efficiently enhance their code with type-safe enums, allowing for cleaner and more maintainable Go applications.

0 comments

No comments yet.

Sign in to be the first to comment.