Go: Add helpers for defined enum types#316
Open
kendallgoto wants to merge 1 commit intokaitai-io:masterfrom
Open
Go: Add helpers for defined enum types#316kendallgoto wants to merge 1 commit intokaitai-io:masterfrom
kendallgoto wants to merge 1 commit intokaitai-io:masterfrom
Conversation
8b37e45 to
56f208d
Compare
Adds basic string conversion / lookup helpers for compiled enums Signed-off-by: Kendall Goto <kendall@level.co>
56f208d to
30c172b
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.
Adds various basic helpers to compiled enums, including
Based on the enum definitions typically provided by Golang enum helpers, such as dmarkham/enumer
Given a simple example such as:
Previously, the Go implementation would generate a new Enum type for colors, and appropriately named constants for the values. However, it did not generate any exposed methods related to this type or attached to it, preventing the use of basic operations such as converting the enum value to a readable string, validating enum bounds, etc:
With this change, additional helper methods are automatically generated for each enum, allowing more flexibility for their usage.
The specific generated code in this code expands as such:
This implementation makes generated enums a lot more versatile and replicates some of the functionality e.g. of IntEnum in the compiled python3 code, which allows you to iterate over a list of defined enums, and convert enums to their string name.