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

serialize: add more tests and docstrings #878

Merged
merged 7 commits into from
Dec 14, 2023

Conversation

piodul
Copy link
Collaborator

@piodul piodul commented Dec 13, 2023

This PR aims to improve the quality of the serialize module:

  • Adds a couple of tests which check that proper errors are returned in case when serialization fails,
  • Fixes a couple of bugs in errors reported from serialization logic - thanks to the aforementioned tests,
  • Documents remaining public items in the types/serialize module and adds a compile-time warning about missing public items in order to prevent regressions.

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 piodul requested a review from Lorak-mmk December 13, 2023 09:35
@piodul piodul force-pushed the docstrings-and-tests branch from 95acafa to 5511cea Compare December 13, 2023 09:37
scylla-cql/src/types/serialize/row.rs Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

Fix the situation in this commit by
enforcing a consistent naming scheme:

  • If something from missing in Rust data but required by the CQL type,
    use <RustStuff>MissingFor<CqlStuff>.
  • If something is present in Rust data but missing from the CQL type,
    use NoSuch<CqlStuff> or No<CqlStuff>WithName.

Maybe this scheme should be documented in code so that it is enforced if any new errors / variants are added in the future?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure where would be the best way to put it and how to formulate it... Let's leave it for a follow up for now.

scylla-cql/src/types/serialize/row.rs Show resolved Hide resolved
scylla-cql/src/types/serialize/mod.rs Outdated Show resolved Hide resolved
When serializing a Rust struct / CqlValue::UserDefinedType to a CQL UDT,
or a Rust struct / BTreeMap / HashMap as a list of named values, there
can be a fields/columns mismatch between the Rust and CQL definition
and a field/column can be present in one or missing in another.

Current error kind variants have very confusing or plainly wrong
names/descriptions/messages. Fix the situation in this commit by
enforcing a consistent naming scheme:

- If something from missing in Rust data but required by the CQL type,
  use `<RustStuff>MissingFor<CqlStuff>`.
- If something is present in Rust data but missing from the CQL type,
  use `NoSuch<CqlStuff>` or `No<CqlStuff>WithName`.

Apart from that, fix the docstrings and the error messages.
The code that adapts `ValueList` to `SerializeRow` and rewrites named
values to regular values based on the context didn't use to check
whether there are some superfluous values that do not match to any of
the bind markers. Fix this.
Add a dedicated error variant to ValueToSerializeCqlAdapterError,
returned in case when the legacy implementation tried to serialize but
overflowed the maximum allowed size by the protocol. Previously, the
`ValueTooBig` error defined in frame::value would be returned, which was
inconsistent with other error cases for the `Value` -> `SerializeCql`
translation code.
In case when serialization of one of the fields fails, the tuple should
return an error indicating that serialization of the tuple failed, and
nest the error returned by field serialization inside it. The error used
to have the wrong ColumnType put into it - a loop variable containing
the ColumnType of the field shadowed the function argument containing
the ColumnType of the whole tuple. Fix the issue by renaming the loop
variable and un-shadowing the function argument as a result.
It's a good practice to implement common traits for public types, and
some of them will be used in the tests introduced in the next commits.
The PR that introduced impls of the SerializeRow/SerializeCql for the
types that used to implement the old traits relied on the tests in
`value_tests.rs` - they were extended with more test cases and modified
to run on both the old and the new traits. However, this was only done
for the tests that serialize stuff without causing errors. The new impls
return errors that are much more detailed than what the old ones used to
return, and - as evidenced by the bugs fixed in previous commits - badly
needed their own set of tests.

Add such a set. All different kinds of built-in errors should be
covered, with the exception of size overflow errors which would require
allocating huge amounts of memory to trigger them.
Put the `#![warn(missing_docs)]` attribute at the top of the
`types::serialize` module to trigger warnings for undocumented public
items, and then add docstrings to the items reported by the warnings.
@piodul piodul force-pushed the docstrings-and-tests branch from 5511cea to 7cf355b Compare December 14, 2023 10:43
@piodul piodul requested a review from Lorak-mmk December 14, 2023 10:45
@piodul piodul merged commit 10b49ef into scylladb:main Dec 14, 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