Skip to content

Support custom field values and simplify UnmarshalText#8

Merged
jmfrees merged 5 commits intomainfrom
jmfrees/allow-field-value-naming
Jan 1, 2026
Merged

Support custom field values and simplify UnmarshalText#8
jmfrees merged 5 commits intomainfrom
jmfrees/allow-field-value-naming

Conversation

@jmfrees
Copy link
Owner

@jmfrees jmfrees commented Jan 1, 2026

This pull request introduces several improvements and refactors to the enum generation tool, focusing on enhanced flexibility, improved error handling, and codebase simplification. The main feature added is support for specifying a custom struct field to use as the string representation of enum instances via a new -name-field flag. Additionally, the code now consistently uses context-aware logging and has refactored its internal data structures for clarity and maintainability.

Feature enhancements

  • Added a -name-field flag to cmd/enumr/main.go and extended the template logic in pkg/enumr/enum.tmpl to allow users to specify which struct field should be used for the string representation of enum instances. This affects both the String() and UnmarshalText() methods. [1] [2] [3]
  • Enforced validation in pkg/enumr/generate.go to ensure all enum instances have the specified name field, returning a clear error if not.

Logging and error handling

  • Updated all error logging in cmd/enumr/main.go and related helpers to use context-aware logging (logger.ErrorContext) for better traceability. [1] [2] [3] [4]

Data structure and code refactoring

  • Renamed exported types (TypeSpec, FieldInfo, InstanceData, etc.) to unexported forms (typeSpec, fieldInfo, instanceData) throughout the codebase for improved encapsulation and clarity. Refactored related function signatures and usages accordingly. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
  • Changed how instance field values are handled: replaced the previous initialization string approach (Init) with a map of field names to values (Fields). Updated all directive parsing, instance collection, and tests to use this new structure. [1] [2] [3] [4]

Internal logic improvements

  • Improved instance collection from source code by supporting both named and positional struct field assignments and using AST printing for accurate value extraction. [1] [2]
  • Removed the unused buildInitString function and replaced its usage with direct field mapping.

These changes collectively make the enum generator more flexible, robust, and maintainable, especially for projects with complex struct-based enums.

@jmfrees jmfrees force-pushed the jmfrees/allow-field-value-naming branch from 11f4c13 to fac14ab Compare January 1, 2026 22:52
@jmfrees jmfrees force-pushed the jmfrees/allow-field-value-naming branch from 4e419ca to 26a227c Compare January 1, 2026 23:14
@jmfrees jmfrees merged commit a575ace into main Jan 1, 2026
1 check passed
@jmfrees jmfrees deleted the jmfrees/allow-field-value-naming branch January 1, 2026 23:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments