[Feature] Add support for casting pure enums to integers instead of strings #53358
+14
−6
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.
[Feature] Add support for casting pure enums to integers instead of strings
This PR modifies the default behavior of storing pure enum values as strings to storing them as integers. The motivation behind this change is that, in most cases I use enums as casts I use integer backed enums —which typically requires maintaining specific value for each enum case— because int columns are more effiecent in storing and indexing, and don't care what is the value of each enum. This PR takes care of giving each case a unique value and it was inspired by implicit enum values in C++.
Cons
Enum::cases()
method which also depends on the order of the cases in the enum.Changes Made
getStorableEnumValue
method in theHasAttributes
trait to return an integer instead of a string.getEnumCaseFromValue
method in theHasAttributes
trait to interpret enum values as integers or strings for old values.Compatibility
This change is expected to be backward-compatible, If you can think of a scenario where this might introduce a breaking change, please let me know.
Tests
Example
Here’s an example to illustrate the change:
Before:
Using a pure enum cast in the model:
After:
With this PR, using the same pure enum cast will store integer values instead:
Notes
This change makes it easier to use pure enums in the casts without needing to define explicit integer-backed values for each case.