Go: Add helpers for defined enum types #316
Open
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.