Skip to content
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

Serialization refactor: macros for value/row serialization #851

Merged
merged 4 commits into from
Dec 9, 2023

Conversation

piodul
Copy link
Collaborator

@piodul piodul commented Oct 20, 2023

Introduce derive macros for both of the new serialization traits: SerializeCql and SerializeRow. They allow to serialize given Rust struct either into a UDT value or a list of values for a query.

The most significant difference from the previous IntoUserType and ValueList is that the new macros match the Rust struct fields to UDT fields or columns by name. This makes the macros less error-prone to use, especially in the UDT case.

For users which are already used to the old macros and are willing to guarantee the field order but do not want to pay performance cost associated with sorting the fields, a separate "flavor" is introduced. With that flavor enabled, the generated code expects that the field order is correct. The field names are still checked - if the expectation about the matching fields is broken serialization will just fail.

Depends on #862

Refs: #802

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

@piodul
Copy link
Collaborator Author

piodul commented Nov 24, 2023

v2:

@piodul piodul force-pushed the serialization-refactor-macros branch from 0b95f92 to 4ea5a69 Compare November 24, 2023 00:59
@piodul
Copy link
Collaborator Author

piodul commented Nov 24, 2023

v2.1: address clippy's complaint

@piodul piodul force-pushed the serialization-refactor-macros branch from 4ea5a69 to dc58365 Compare November 24, 2023 01:48
@piodul piodul changed the title Serialization refactor: macros for value/row serialization with matching on field names Serialization refactor: macros for value/row serialization Nov 24, 2023
@piodul piodul force-pushed the serialization-refactor-macros branch from dc58365 to 571b31c Compare November 24, 2023 07:26
@piodul
Copy link
Collaborator Author

piodul commented Nov 24, 2023

v2.2:

  • Fixed links in docstrings
  • Updated the Cargo.lock.msrv

Copy link
Collaborator

@Lorak-mmk Lorak-mmk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few questions - they are about commits belonging to #862 which I (mistakenly) thought I was reviewing, sorry. I'll review 4 commits of this PR shortly

scylla-cql/src/frame/value_tests.rs Outdated Show resolved Hide resolved
scylla-cql/src/types/serialize/value.rs Outdated Show resolved Hide resolved
scylla-cql/src/types/serialize/value.rs Outdated Show resolved Hide resolved
scylla-cql/src/types/serialize/value.rs Outdated Show resolved Hide resolved
scylla-cql/src/types/serialize/value.rs Outdated Show resolved Hide resolved
@piodul
Copy link
Collaborator Author

piodul commented Nov 29, 2023

I left a few questions - they are about commits belonging to #862 which I (mistakenly) thought I was reviewing, sorry. I'll review 4 commits of this PR shortly

No problem, my bad - the description of the other PR was misleading.

scylla-macros/src/serialize/cql.rs Outdated Show resolved Hide resolved
scylla-cql/src/macros.rs Outdated Show resolved Hide resolved
scylla-macros/src/serialize/row.rs Outdated Show resolved Hide resolved
@piodul piodul force-pushed the serialization-refactor-macros branch from 571b31c to cf63068 Compare December 1, 2023 05:08
@piodul
Copy link
Collaborator Author

piodul commented Dec 1, 2023

v3:

@Lorak-mmk Lorak-mmk mentioned this pull request Dec 1, 2023
8 tasks
scylla-cql/src/macros.rs Outdated Show resolved Hide resolved
@piodul piodul force-pushed the serialization-refactor-macros branch 2 times, most recently from 480b5e2 to 938ed26 Compare December 8, 2023 13:06
@piodul
Copy link
Collaborator Author

piodul commented Dec 8, 2023

v4:

  • rebased
  • updated docstrings, yet again
  • fixed the bug that caused macros not to work for structs with generics

@Lorak-mmk - I manually checked that the test you commented out in your PR now compiles, so you can rebase.

Copy link
Collaborator

@Lorak-mmk Lorak-mmk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good - but maybe add a test for generics so that we have an actual test for that beside the code in examples/?

Introduce a derive macro which serializes a struct into a UDT.

Unlike the previous IntoUserType, the new macro takes care to match
the struct fields to UDT fields by their names. It does not assume that
the order of the fields in the Rust struct is the same as in the UDT.
Introduce a derive macro which serializes a struct into bind markers of
a statement.

Unlike the previous ValueList, the new macro takes care to match
the struct fields to bind markers/columns by their names.
Some users might not need the additional robustness of `SerializeCql`
that comes from sorting the fields before serializing, as they are used
to the current behavior of `Value` and properly set the order of the
fields in their Rust struct. In order to give them some performance
boost, add an additional mode to `SerializeCql` called "enforce_order"
which expects that the order of the fields in the struct is kept in
sync with the DB definition of the UDT.

It's still safe to use because, as the struct fields are serialized,
their names are compared with the fields in the UDT definition order
and serialization fails if the field name on some position is
mismatched.
Like in the case of `SerializeRow`, some people might be used to working
with the old `ValueList` and already order their Rust struct fields with
accordance to the queries they are used with and don't need the overhead
associated with looking up columns by name. The `enforce_order` mode is
added to `SerializeRow` which works analogously as in `SerializeCql` -
expects the columns to be in the correct order and verifies that this is
the case when serializing, but just fails instead of reordering if that
expectation is broken.
@piodul piodul force-pushed the serialization-refactor-macros branch from 938ed26 to a255d81 Compare December 9, 2023 10:12
@piodul
Copy link
Collaborator Author

piodul commented Dec 9, 2023

Looks good - but maybe add a test for generics so that we have an actual test for that beside the code in examples/?

Done.

v4.1: added some minimal tests for UDTs/rows with generics in them, mostly to see whether they compile or not

@piodul piodul merged commit b26e5bd into scylladb:main Dec 9, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants