A Go code generator that automates the functional options pattern, eliminating boilerplate for type-safe constructors and validation.
options-gen is a Go code generation tool that automatically creates type-safe constructors, setter functions, and validation logic for structs using the functional options pattern. It eliminates the boilerplate and error-prone nature of writing functional options by hand, making API design cleaner and more maintainable.
Go developers building libraries or applications with configurable structs, particularly those implementing the functional options pattern for clean, scalable APIs.
Developers choose options-gen because it automates the tedious and repetitive task of writing functional options, ensuring compile-time type safety and integrating built-in validation with go-playground/validator, all while supporting modern Go features like generics and flexible defaults.
Codegen for functional options in go projects
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Generates complete functional options implementation from struct definitions, directly addressing the 'tedious and error-prone' manual writing highlighted in the README's problem statement.
Produces compile-time safe WithX() functions for each optional field, ensuring API safety without runtime casts, a core feature listed in the Key Features.
Seamlessly incorporates go-playground/validator via struct tags, enabling runtime validation with minimal effort, as shown in the validation examples and tag explanations.
Supports defaults via tags, variables, or functions with configurable CLI flags, offering versatility for different project needs, detailed in the 'Defaults' section.
Works with Go generics and allows generating options for external package structs using type aliases, making it adaptable to modern and complex codebases.
Integrates with go-playground/validator, adding a runtime dependency that may not fit projects using other validation libraries or aiming for minimal dependencies.
The CLI tool has numerous flags (e.g., -defaults-from, -out-prefix) and requires careful management of go generate, which can be overwhelming for simple use cases.
Only generates code for struct types, so it's unsuitable for configuration approaches that don't use structs, such as functional closures or dynamic maps.
Changes to structs necessitate regeneration, and if not integrated into CI/CD, can lead to outdated code or inconsistencies, especially with external structs where validation tags aren't preserved.