From cd9ccfb30016b82af5d9d79727702279717c1208 Mon Sep 17 00:00:00 2001 From: Jamie Tsai Date: Mon, 9 Jun 2025 11:23:13 -0400 Subject: [PATCH 1/2] Replace remove_empty_write_concern macro with serde attribute write_concern_is_empty --- src/client/options.rs | 3 ++- src/client/options/bulk_write.rs | 3 ++- src/coll/options.rs | 10 ++++++++++ src/db/options.rs | 4 +++- src/operation.rs | 13 ------------- src/operation/aggregate.rs | 3 +-- src/operation/commit_transaction.rs | 2 -- src/operation/create.rs | 2 -- src/operation/create_indexes.rs | 2 -- src/operation/delete.rs | 2 -- src/operation/drop_collection.rs | 2 -- src/operation/drop_database.rs | 2 -- src/operation/drop_indexes.rs | 2 -- src/operation/find_and_modify.rs | 4 ---- src/operation/find_and_modify/options.rs | 3 ++- 15 files changed, 20 insertions(+), 37 deletions(-) diff --git a/src/client/options.rs b/src/client/options.rs index 951245b12..5aa55d569 100644 --- a/src/client/options.rs +++ b/src/client/options.rs @@ -43,7 +43,7 @@ use crate::{ options::ReadConcernLevel, sdam::{verify_max_staleness, DEFAULT_HEARTBEAT_FREQUENCY, MIN_HEARTBEAT_FREQUENCY}, selection_criteria::{ReadPreference, SelectionCriteria, TagSet}, - serde_util, + serde_util::{self, write_concern_is_empty}, srv::{OriginalSrvInfo, SrvResolver}, }; @@ -2670,6 +2670,7 @@ pub struct TransactionOptions { /// The write concern to use when committing or aborting a transaction. #[builder(default)] + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// The selection criteria to use for all read operations in a transaction. diff --git a/src/client/options/bulk_write.rs b/src/client/options/bulk_write.rs index 6ede2d219..916a19dbf 100644 --- a/src/client/options/bulk_write.rs +++ b/src/client/options/bulk_write.rs @@ -10,7 +10,7 @@ use crate::{ bson_util::{get_or_prepend_id_field, replacement_document_check, update_document_check}, error::Result, options::{UpdateModifications, WriteConcern}, - serde_util::serialize_bool_or_true, + serde_util::{serialize_bool_or_true, write_concern_is_empty}, Collection, Namespace, }; @@ -48,6 +48,7 @@ pub struct BulkWriteOptions { pub let_vars: Option, /// The write concern to use for this operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, } diff --git a/src/coll/options.rs b/src/coll/options.rs index adde0a6ed..d15729724 100644 --- a/src/coll/options.rs +++ b/src/coll/options.rs @@ -117,6 +117,7 @@ pub struct InsertOneOptions { pub bypass_document_validation: Option, /// The write concern for the operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the @@ -295,6 +296,7 @@ pub struct ReplaceOptions { pub hint: Option, /// The write concern for the operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// Map of parameter names and values. Values must be constant or closed @@ -335,6 +337,7 @@ pub struct DeleteOptions { pub collation: Option, /// The write concern for the operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// The index to use for the operation. @@ -379,6 +382,7 @@ pub struct FindOneAndDeleteOptions { pub sort: Option, /// The level of the write concern + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// The collation to use for the operation. @@ -438,6 +442,7 @@ pub struct FindOneAndReplaceOptions { pub upsert: Option, /// The level of the write concern + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// The collation to use for the operation. @@ -503,6 +508,7 @@ pub struct FindOneAndUpdateOptions { pub upsert: Option, /// The level of the write concern + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// The collation to use for the operation. @@ -615,6 +621,7 @@ pub struct AggregateOptions { /// /// If none is specified, the write concern defined on the object executing this operation will /// be used. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// A document with any amount of parameter names, each followed by definitions of constants in @@ -1056,6 +1063,7 @@ pub struct CreateIndexOptions { pub max_time: Option, /// The write concern for the operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the @@ -1075,6 +1083,7 @@ pub struct CreateIndexOptions { #[export_tokens] pub struct DropCollectionOptions { /// The write concern for the operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// Map of encrypted fields for the collection. @@ -1108,6 +1117,7 @@ pub struct DropIndexOptions { pub max_time: Option, /// The write concern for the operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the diff --git a/src/db/options.rs b/src/db/options.rs index b1f94119b..ee987ce28 100644 --- a/src/db/options.rs +++ b/src/db/options.rs @@ -11,7 +11,7 @@ use crate::{ concern::{ReadConcern, WriteConcern}, options::{Collation, CursorType}, selection_criteria::SelectionCriteria, - serde_util, + serde_util::{self, write_concern_is_empty}, }; /// These are the valid options for creating a [`Database`](../struct.Database.html) with @@ -84,6 +84,7 @@ pub struct CreateCollectionOptions { pub collation: Option, /// The write concern for the operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, /// The default configuration for indexes created on this collection, including the _id index. @@ -288,6 +289,7 @@ pub enum TimeseriesGranularity { #[export_tokens] pub struct DropDatabaseOptions { /// The write concern for the operation. + #[serde(skip_serializing_if = "write_concern_is_empty")] pub write_concern: Option, } diff --git a/src/operation.rs b/src/operation.rs index 287d9aebe..fb8f8d997 100644 --- a/src/operation.rs +++ b/src/operation.rs @@ -532,16 +532,3 @@ where } } -macro_rules! remove_empty_write_concern { - ($opts:expr) => { - if let Some(ref mut options) = $opts { - if let Some(ref write_concern) = options.write_concern { - if write_concern.is_empty() { - options.write_concern = None; - } - } - } - }; -} - -pub(crate) use remove_empty_write_concern; diff --git a/src/operation/aggregate.rs b/src/operation/aggregate.rs index dd31e8532..ef1c5dbb0 100644 --- a/src/operation/aggregate.rs +++ b/src/operation/aggregate.rs @@ -6,7 +6,7 @@ use crate::{ cmap::{Command, RawCommandResponse, StreamDescription}, cursor::CursorSpecification, error::Result, - operation::{append_options, remove_empty_write_concern, Retryability}, + operation::{append_options, Retryability}, options::{AggregateOptions, ReadPreference, SelectionCriteria, WriteConcern}, Namespace, }; @@ -55,7 +55,6 @@ impl OperationWithDefaults for Aggregate { "cursor": {} }; - remove_empty_write_concern!(self.options); append_options(&mut body, self.options.as_ref())?; if self.is_out_or_merge() { diff --git a/src/operation/commit_transaction.rs b/src/operation/commit_transaction.rs index 81c931b6b..81a7bb0de 100644 --- a/src/operation/commit_transaction.rs +++ b/src/operation/commit_transaction.rs @@ -7,7 +7,6 @@ use crate::{ error::Result, operation::{ append_options_to_raw_document, - remove_empty_write_concern, OperationWithDefaults, Retryability, }, @@ -36,7 +35,6 @@ impl OperationWithDefaults for CommitTransaction { Self::NAME: 1, }; - remove_empty_write_concern!(self.options); append_options_to_raw_document(&mut body, self.options.as_ref())?; Ok(Command::new( diff --git a/src/operation/create.rs b/src/operation/create.rs index ea1ab4d0e..b2f48337c 100644 --- a/src/operation/create.rs +++ b/src/operation/create.rs @@ -5,7 +5,6 @@ use crate::{ error::Result, operation::{ append_options_to_raw_document, - remove_empty_write_concern, OperationWithDefaults, WriteConcernOnlyBody, }, @@ -37,7 +36,6 @@ impl OperationWithDefaults for Create { Self::NAME: self.ns.coll.clone(), }; - remove_empty_write_concern!(self.options); append_options_to_raw_document(&mut body, self.options.as_ref())?; Ok(Command::new( diff --git a/src/operation/create_indexes.rs b/src/operation/create_indexes.rs index f188a2a4c..bb99af3c6 100644 --- a/src/operation/create_indexes.rs +++ b/src/operation/create_indexes.rs @@ -7,7 +7,6 @@ use crate::{ index::IndexModel, operation::{ append_options_to_raw_document, - remove_empty_write_concern, OperationWithDefaults, }, options::{CreateIndexOptions, WriteConcern}, @@ -65,7 +64,6 @@ impl OperationWithDefaults for CreateIndexes { "indexes": indexes, }; - remove_empty_write_concern!(self.options); append_options_to_raw_document(&mut body, self.options.as_ref())?; Ok(Command::new( diff --git a/src/operation/delete.rs b/src/operation/delete.rs index bbbd9260f..7f21804d2 100644 --- a/src/operation/delete.rs +++ b/src/operation/delete.rs @@ -6,7 +6,6 @@ use crate::{ error::{convert_insert_many_error, Result}, operation::{ append_options, - remove_empty_write_concern, OperationWithDefaults, Retryability, WriteResponseBody, @@ -70,7 +69,6 @@ impl OperationWithDefaults for Delete { "ordered": true, // command monitoring tests expect this (SPEC-1130) }; - remove_empty_write_concern!(self.options); append_options(&mut body, self.options.as_ref())?; Ok(Command::new( diff --git a/src/operation/drop_collection.rs b/src/operation/drop_collection.rs index b565542bb..579d5f155 100644 --- a/src/operation/drop_collection.rs +++ b/src/operation/drop_collection.rs @@ -5,7 +5,6 @@ use crate::{ error::{Error, Result}, operation::{ append_options_to_raw_document, - remove_empty_write_concern, OperationWithDefaults, WriteConcernOnlyBody, }, @@ -37,7 +36,6 @@ impl OperationWithDefaults for DropCollection { Self::NAME: self.ns.coll.clone(), }; - remove_empty_write_concern!(self.options); append_options_to_raw_document(&mut body, self.options.as_ref())?; Ok(Command::new( diff --git a/src/operation/drop_database.rs b/src/operation/drop_database.rs index 80b7aa3ae..28915029a 100644 --- a/src/operation/drop_database.rs +++ b/src/operation/drop_database.rs @@ -6,7 +6,6 @@ use crate::{ error::Result, operation::{ append_options_to_raw_document, - remove_empty_write_concern, OperationWithDefaults, WriteConcernOnlyBody, }, @@ -37,7 +36,6 @@ impl OperationWithDefaults for DropDatabase { Self::NAME: 1, }; - remove_empty_write_concern!(self.options); append_options_to_raw_document(&mut body, self.options.as_ref())?; Ok(Command::new( diff --git a/src/operation/drop_indexes.rs b/src/operation/drop_indexes.rs index feda1df37..0a84775d2 100644 --- a/src/operation/drop_indexes.rs +++ b/src/operation/drop_indexes.rs @@ -5,7 +5,6 @@ use crate::{ error::Result, operation::{ append_options_to_raw_document, - remove_empty_write_concern, OperationWithDefaults, }, options::{DropIndexOptions, WriteConcern}, @@ -36,7 +35,6 @@ impl OperationWithDefaults for DropIndexes { "index": self.name.clone(), }; - remove_empty_write_concern!(self.options); append_options_to_raw_document(&mut body, self.options.as_ref())?; Ok(Command::new( diff --git a/src/operation/find_and_modify.rs b/src/operation/find_and_modify.rs index 6716ac292..7f1bf91d0 100644 --- a/src/operation/find_and_modify.rs +++ b/src/operation/find_and_modify.rs @@ -14,7 +14,6 @@ use crate::{ operation::{ append_options_to_raw_document, find_and_modify::options::Modification, - remove_empty_write_concern, OperationWithDefaults, Retryability, }, @@ -82,9 +81,6 @@ impl OperationWithDefaults for FindAndModify { } } - if let Some(ref mut options) = self.options { - remove_empty_write_concern!(Some(options)); - } append_options_to_raw_document(&mut body, self.options.as_ref())?; Ok(Command::new( diff --git a/src/operation/find_and_modify/options.rs b/src/operation/find_and_modify/options.rs index dfcd362be..76b18e217 100644 --- a/src/operation/find_and_modify/options.rs +++ b/src/operation/find_and_modify/options.rs @@ -15,7 +15,7 @@ use crate::{ collation::Collation, concern::WriteConcern, operation::UpdateOrReplace, - serde_util, + serde_util::{self, write_concern_is_empty}, }; #[derive(Clone, Debug)] @@ -37,6 +37,7 @@ pub(crate) struct FindAndModifyOptions { pub(crate) bypass_document_validation: Option, + #[serde(skip_serializing_if = "write_concern_is_empty")] pub(crate) write_concern: Option, pub(crate) array_filters: Option>, From b2c89ee416b8d2aa0e74db2bb7d6efc65543aa0c Mon Sep 17 00:00:00 2001 From: Jamie Tsai Date: Mon, 9 Jun 2025 16:40:22 -0400 Subject: [PATCH 2/2] Fix lint --- src/operation.rs | 1 - src/operation/commit_transaction.rs | 6 +----- src/operation/create.rs | 6 +----- src/operation/create_indexes.rs | 5 +---- src/operation/delete.rs | 7 +------ src/operation/drop_collection.rs | 6 +----- src/operation/drop_database.rs | 6 +----- src/operation/drop_indexes.rs | 5 +---- 8 files changed, 7 insertions(+), 35 deletions(-) diff --git a/src/operation.rs b/src/operation.rs index fb8f8d997..d5cdbadff 100644 --- a/src/operation.rs +++ b/src/operation.rs @@ -531,4 +531,3 @@ where Ok(SingleCursorResult(full_body.cursor.first_batch.pop())) } } - diff --git a/src/operation/commit_transaction.rs b/src/operation/commit_transaction.rs index 81a7bb0de..69fb1f80f 100644 --- a/src/operation/commit_transaction.rs +++ b/src/operation/commit_transaction.rs @@ -5,11 +5,7 @@ use crate::bson::rawdoc; use crate::{ cmap::{Command, RawCommandResponse, StreamDescription}, error::Result, - operation::{ - append_options_to_raw_document, - OperationWithDefaults, - Retryability, - }, + operation::{append_options_to_raw_document, OperationWithDefaults, Retryability}, options::{Acknowledgment, TransactionOptions, WriteConcern}, }; diff --git a/src/operation/create.rs b/src/operation/create.rs index b2f48337c..019659c3f 100644 --- a/src/operation/create.rs +++ b/src/operation/create.rs @@ -3,11 +3,7 @@ use crate::bson::rawdoc; use crate::{ cmap::{Command, RawCommandResponse, StreamDescription}, error::Result, - operation::{ - append_options_to_raw_document, - OperationWithDefaults, - WriteConcernOnlyBody, - }, + operation::{append_options_to_raw_document, OperationWithDefaults, WriteConcernOnlyBody}, options::{CreateCollectionOptions, WriteConcern}, Namespace, }; diff --git a/src/operation/create_indexes.rs b/src/operation/create_indexes.rs index bb99af3c6..008096582 100644 --- a/src/operation/create_indexes.rs +++ b/src/operation/create_indexes.rs @@ -5,10 +5,7 @@ use crate::{ cmap::{Command, RawCommandResponse, StreamDescription}, error::{ErrorKind, Result}, index::IndexModel, - operation::{ - append_options_to_raw_document, - OperationWithDefaults, - }, + operation::{append_options_to_raw_document, OperationWithDefaults}, options::{CreateIndexOptions, WriteConcern}, results::CreateIndexesResult, Namespace, diff --git a/src/operation/delete.rs b/src/operation/delete.rs index 7f21804d2..b6641b63c 100644 --- a/src/operation/delete.rs +++ b/src/operation/delete.rs @@ -4,12 +4,7 @@ use crate::{ coll::Namespace, collation::Collation, error::{convert_insert_many_error, Result}, - operation::{ - append_options, - OperationWithDefaults, - Retryability, - WriteResponseBody, - }, + operation::{append_options, OperationWithDefaults, Retryability, WriteResponseBody}, options::{DeleteOptions, Hint, WriteConcern}, results::DeleteResult, }; diff --git a/src/operation/drop_collection.rs b/src/operation/drop_collection.rs index 579d5f155..276a42e23 100644 --- a/src/operation/drop_collection.rs +++ b/src/operation/drop_collection.rs @@ -3,11 +3,7 @@ use crate::bson::rawdoc; use crate::{ cmap::{Command, RawCommandResponse, StreamDescription}, error::{Error, Result}, - operation::{ - append_options_to_raw_document, - OperationWithDefaults, - WriteConcernOnlyBody, - }, + operation::{append_options_to_raw_document, OperationWithDefaults, WriteConcernOnlyBody}, options::{DropCollectionOptions, WriteConcern}, Namespace, }; diff --git a/src/operation/drop_database.rs b/src/operation/drop_database.rs index 28915029a..314b5f916 100644 --- a/src/operation/drop_database.rs +++ b/src/operation/drop_database.rs @@ -4,11 +4,7 @@ use crate::{ cmap::{Command, RawCommandResponse, StreamDescription}, db::options::DropDatabaseOptions, error::Result, - operation::{ - append_options_to_raw_document, - OperationWithDefaults, - WriteConcernOnlyBody, - }, + operation::{append_options_to_raw_document, OperationWithDefaults, WriteConcernOnlyBody}, options::WriteConcern, }; diff --git a/src/operation/drop_indexes.rs b/src/operation/drop_indexes.rs index 0a84775d2..6201951a2 100644 --- a/src/operation/drop_indexes.rs +++ b/src/operation/drop_indexes.rs @@ -3,10 +3,7 @@ use crate::bson::rawdoc; use crate::{ cmap::{Command, RawCommandResponse, StreamDescription}, error::Result, - operation::{ - append_options_to_raw_document, - OperationWithDefaults, - }, + operation::{append_options_to_raw_document, OperationWithDefaults}, options::{DropIndexOptions, WriteConcern}, Namespace, };