Skip to content

RUST-740 Return Deserialize structs from list_databases and list_collections #328

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

Conversation

patrickfreed
Copy link
Contributor

@patrickfreed patrickfreed commented Apr 27, 2021

RUST-740

This PR updates Client::list_databases and Database::list_collections to return structs that implement Deserialize rather than just Document.

@patrickfreed patrickfreed changed the title RUST-740 RUST-740 Return Deserialize structs from list_databases and list_collections Apr 28, 2021
Copy link
Contributor Author

@patrickfreed patrickfreed left a comment

Choose a reason for hiding this comment

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

Tagging in @kmahar for the public API changes.

#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct IndexOptionDefaults {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This struct was missing a few attributes so I added them. I don't think any of our users will use this though.

Copy link
Contributor

@kmahar kmahar Apr 28, 2021

Choose a reason for hiding this comment

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

TIL create accepts indexOptionDefaults

(I guess we do support it in Swift, so I knew this at some point, but we just take in a document so it was easier to forget about it than if it had a concrete type)

@@ -85,5 +85,4 @@ impl Operation for ListDatabases {
#[derive(Debug, Deserialize)]
struct ResponseBody {
databases: Vec<Document>,
total_size: Option<i64>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This wasn't being used and in fact wasn't even being deserialized, since the server returned this value as a double until 5.0.

src/results.rs Outdated

/// The amount of disk space in bytes that is consumed by the database.
#[serde(deserialize_with = "crate::bson_util::deserialize_i64_from_bson_number")]
pub size_on_disk: i64,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This value is returned as a bunch of different types depending on the server version, so we need a helper to deserialize it from whatever type to an integer. See here for an overview if interested.

Copy link
Contributor

Choose a reason for hiding this comment

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

any reason for using an i64 here rather than a u64?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree u64 does seem to make sense for a size field

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought so too originally, but this is a value that's deserialized from server responses, meaning it can possibly have negative values. I checked elsewhere in the driver for precedent and saw that we use i64 for similar values, e.g. matched_count in UpdateResult. That being said, it seems like we probably would be totally fine using an unsigned value here if we wanted to, so I've updated it.

I also updated get_int to be a little more lenient when reading integers from doubles in case of a floating point rounding issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I filed RUST-764 to discuss updating other such fields.

pub collection_type: CollectionType,

/// The options used to create the collection.
pub options: CreateCollectionOptions,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about creating a separate type for this in case they ever diverge (and also because it contains fields like write_concern that don't make sense in this context), but the docs explicitly state that this field will contain the exact options provided to createCollection, and looking through the server code confirms also this, so I figured this was pretty low risk. Let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree it seems low risk

Copy link
Contributor

Choose a reason for hiding this comment

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

sgtm

@patrickfreed patrickfreed marked this pull request as ready for review April 28, 2021 20:55
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct IndexOptionDefaults {
Copy link
Contributor

@kmahar kmahar Apr 28, 2021

Choose a reason for hiding this comment

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

TIL create accepts indexOptionDefaults

(I guess we do support it in Swift, so I knew this at some point, but we just take in a document so it was easier to forget about it than if it had a concrete type)

pub collection_type: CollectionType,

/// The options used to create the collection.
pub options: CreateCollectionOptions,
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree it seems low risk

Copy link
Contributor

@kmahar kmahar left a comment

Choose a reason for hiding this comment

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

LGTM mod Isabel's comment, I don't need to re-review

src/results.rs Outdated

/// The amount of disk space in bytes that is consumed by the database.
#[serde(deserialize_with = "crate::bson_util::deserialize_i64_from_bson_number")]
pub size_on_disk: i64,
Copy link
Contributor

Choose a reason for hiding this comment

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

I agree u64 does seem to make sense for a size field

@patrickfreed patrickfreed merged commit 5aa29c2 into mongodb:master May 3, 2021
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.

3 participants