-
Notifications
You must be signed in to change notification settings - Fork 146
macros: Add support for C-style enum serialization via #[scylla(repr = "...")] #1504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds support for deriving SerializeValue and DeserializeValue traits for enums, enabling automatic serialization and deserialization of C-style enums to/from integer CQL types. The implementation converts enum variants to their discriminant values, with configurable representation types.
Key Changes:
- Added enum-specific derive implementations for
SerializeValueandDeserializeValuemacros - Introduced
reprattribute to specify the underlying integer representation type (defaults to i32) - Made helper functions
pub(crate)to support enum deserialization lifetime handling
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 9 comments.
| File | Description |
|---|---|
| scylla-macros/src/serialize/value.rs | Adds EnumAttrs struct and derive_serialize_value_enum function to generate serialization code for enums, converting them to their integer discriminant values |
| scylla-macros/src/deserialize/value.rs | Adds EnumAttrs struct and deserialize_value_derive_enum function to generate deserialization code for enums, parsing integer values back to enum variants |
| scylla-macros/src/deserialize/mod.rs | Exports lifetime helper functions as pub(crate) to enable reuse in enum deserialization |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
04e033e to
6d6796d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 13 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@ziara1 Please first address clippy's complaint and Copilot's comments, then ping me. |
8b54953 to
f1633c6
Compare
f1633c6 to
f09bbb3
Compare
|
@ziara1 Please squash fixes into original commits. |
07a98fa to
3cc1bb1
Compare
3cc1bb1 to
d79a4c1
Compare
|
@wprzytula done |
Motivation
Users often map C-style enums (enums with unit variants and integer discriminants) to integer columns in ScyllaDB (e.g.,
tinyint,smallint,int). Currently, users must manually implementSerializeValueandDeserializeValuefor these enums, which involves tedious boilerplate for matching variants to integers and vice versa.Solution
This commit extends
SerializeValueandDeserializeValuemacros to support C-style enums directly.Users can now annotate their enum with
#[scylla(repr = "TYPE")](e.g.,"i8","i32"), and the macro will generate code that:Behavior details:
i32checks againstIntcolumn).What's done
Refactored DeserializeValue derive
deserialize_value_deriveto handleData::Enumseparately.#[scylla(repr = "...")]attribute viaEnumAttrs.deserialize_value_derive_enum, which generates amatchstatement mapping integers from the database back to enum variants.Refactored SerializeValue derive
serialize_value_deriveto handleData::Enum.derive_serialize_value_enum, which casts the enum variant to the representation type (e.g.,*self as i32) before serialization.What has been tested
Tests
Added comprehensive tests covering:
i8(TinyInt),i16(SmallInt),i32(Int), andi64(BigInt).Textcolumn) produces a type check error.Fixes: #923
Pre-review checklist
./docs/source/.Fixes:annotations to PR description.