Support custom field values and simplify UnmarshalText#8
Merged
Conversation
11f4c13 to
fac14ab
Compare
4e419ca to
26a227c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-fieldflag. Additionally, the code now consistently uses context-aware logging and has refactored its internal data structures for clarity and maintainability.Feature enhancements
-name-fieldflag tocmd/enumr/main.goand extended the template logic inpkg/enumr/enum.tmplto allow users to specify which struct field should be used for the string representation of enum instances. This affects both theString()andUnmarshalText()methods. [1] [2] [3]pkg/enumr/generate.goto ensure all enum instances have the specified name field, returning a clear error if not.Logging and error handling
cmd/enumr/main.goand related helpers to use context-aware logging (logger.ErrorContext) for better traceability. [1] [2] [3] [4]Data structure and code refactoring
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]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
buildInitStringfunction 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.