Skip to content

Commit 65ce423

Browse files
RUST-1826 Use serde attribute to remove empty write concerns (#1392)
1 parent eee60bf commit 65ce423

File tree

15 files changed

+27
-72
lines changed

15 files changed

+27
-72
lines changed

src/client/options.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::{
4343
options::ReadConcernLevel,
4444
sdam::{verify_max_staleness, DEFAULT_HEARTBEAT_FREQUENCY, MIN_HEARTBEAT_FREQUENCY},
4545
selection_criteria::{ReadPreference, SelectionCriteria, TagSet},
46-
serde_util,
46+
serde_util::{self, write_concern_is_empty},
4747
srv::{OriginalSrvInfo, SrvResolver},
4848
};
4949

@@ -2670,6 +2670,7 @@ pub struct TransactionOptions {
26702670

26712671
/// The write concern to use when committing or aborting a transaction.
26722672
#[builder(default)]
2673+
#[serde(skip_serializing_if = "write_concern_is_empty")]
26732674
pub write_concern: Option<WriteConcern>,
26742675

26752676
/// The selection criteria to use for all read operations in a transaction.

src/client/options/bulk_write.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
bson_util::{get_or_prepend_id_field, replacement_document_check, update_document_check},
1111
error::Result,
1212
options::{UpdateModifications, WriteConcern},
13-
serde_util::serialize_bool_or_true,
13+
serde_util::{serialize_bool_or_true, write_concern_is_empty},
1414
Collection,
1515
Namespace,
1616
};
@@ -48,6 +48,7 @@ pub struct BulkWriteOptions {
4848
pub let_vars: Option<Document>,
4949

5050
/// The write concern to use for this operation.
51+
#[serde(skip_serializing_if = "write_concern_is_empty")]
5152
pub write_concern: Option<WriteConcern>,
5253
}
5354

src/coll/options.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ pub struct InsertOneOptions {
117117
pub bypass_document_validation: Option<bool>,
118118

119119
/// The write concern for the operation.
120+
#[serde(skip_serializing_if = "write_concern_is_empty")]
120121
pub write_concern: Option<WriteConcern>,
121122

122123
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
@@ -295,6 +296,7 @@ pub struct ReplaceOptions {
295296
pub hint: Option<Hint>,
296297

297298
/// The write concern for the operation.
299+
#[serde(skip_serializing_if = "write_concern_is_empty")]
298300
pub write_concern: Option<WriteConcern>,
299301

300302
/// Map of parameter names and values. Values must be constant or closed
@@ -335,6 +337,7 @@ pub struct DeleteOptions {
335337
pub collation: Option<Collation>,
336338

337339
/// The write concern for the operation.
340+
#[serde(skip_serializing_if = "write_concern_is_empty")]
338341
pub write_concern: Option<WriteConcern>,
339342

340343
/// The index to use for the operation.
@@ -379,6 +382,7 @@ pub struct FindOneAndDeleteOptions {
379382
pub sort: Option<Document>,
380383

381384
/// The level of the write concern
385+
#[serde(skip_serializing_if = "write_concern_is_empty")]
382386
pub write_concern: Option<WriteConcern>,
383387

384388
/// The collation to use for the operation.
@@ -438,6 +442,7 @@ pub struct FindOneAndReplaceOptions {
438442
pub upsert: Option<bool>,
439443

440444
/// The level of the write concern
445+
#[serde(skip_serializing_if = "write_concern_is_empty")]
441446
pub write_concern: Option<WriteConcern>,
442447

443448
/// The collation to use for the operation.
@@ -503,6 +508,7 @@ pub struct FindOneAndUpdateOptions {
503508
pub upsert: Option<bool>,
504509

505510
/// The level of the write concern
511+
#[serde(skip_serializing_if = "write_concern_is_empty")]
506512
pub write_concern: Option<WriteConcern>,
507513

508514
/// The collation to use for the operation.
@@ -615,6 +621,7 @@ pub struct AggregateOptions {
615621
///
616622
/// If none is specified, the write concern defined on the object executing this operation will
617623
/// be used.
624+
#[serde(skip_serializing_if = "write_concern_is_empty")]
618625
pub write_concern: Option<WriteConcern>,
619626

620627
/// A document with any amount of parameter names, each followed by definitions of constants in
@@ -1056,6 +1063,7 @@ pub struct CreateIndexOptions {
10561063
pub max_time: Option<Duration>,
10571064

10581065
/// The write concern for the operation.
1066+
#[serde(skip_serializing_if = "write_concern_is_empty")]
10591067
pub write_concern: Option<WriteConcern>,
10601068

10611069
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the
@@ -1075,6 +1083,7 @@ pub struct CreateIndexOptions {
10751083
#[export_tokens]
10761084
pub struct DropCollectionOptions {
10771085
/// The write concern for the operation.
1086+
#[serde(skip_serializing_if = "write_concern_is_empty")]
10781087
pub write_concern: Option<WriteConcern>,
10791088

10801089
/// Map of encrypted fields for the collection.
@@ -1108,6 +1117,7 @@ pub struct DropIndexOptions {
11081117
pub max_time: Option<Duration>,
11091118

11101119
/// The write concern for the operation.
1120+
#[serde(skip_serializing_if = "write_concern_is_empty")]
11111121
pub write_concern: Option<WriteConcern>,
11121122

11131123
/// Tags the query with an arbitrary [`Bson`] value to help trace the operation through the

src/db/options.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
concern::{ReadConcern, WriteConcern},
1212
options::{Collation, CursorType},
1313
selection_criteria::SelectionCriteria,
14-
serde_util,
14+
serde_util::{self, write_concern_is_empty},
1515
};
1616

1717
/// These are the valid options for creating a [`Database`](../struct.Database.html) with
@@ -84,6 +84,7 @@ pub struct CreateCollectionOptions {
8484
pub collation: Option<Collation>,
8585

8686
/// The write concern for the operation.
87+
#[serde(skip_serializing_if = "write_concern_is_empty")]
8788
pub write_concern: Option<WriteConcern>,
8889

8990
/// The default configuration for indexes created on this collection, including the _id index.
@@ -288,6 +289,7 @@ pub enum TimeseriesGranularity {
288289
#[export_tokens]
289290
pub struct DropDatabaseOptions {
290291
/// The write concern for the operation.
292+
#[serde(skip_serializing_if = "write_concern_is_empty")]
291293
pub write_concern: Option<WriteConcern>,
292294
}
293295

src/operation.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -531,17 +531,3 @@ where
531531
Ok(SingleCursorResult(full_body.cursor.first_batch.pop()))
532532
}
533533
}
534-
535-
macro_rules! remove_empty_write_concern {
536-
($opts:expr) => {
537-
if let Some(ref mut options) = $opts {
538-
if let Some(ref write_concern) = options.write_concern {
539-
if write_concern.is_empty() {
540-
options.write_concern = None;
541-
}
542-
}
543-
}
544-
};
545-
}
546-
547-
pub(crate) use remove_empty_write_concern;

src/operation/aggregate.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
cmap::{Command, RawCommandResponse, StreamDescription},
77
cursor::CursorSpecification,
88
error::Result,
9-
operation::{append_options, remove_empty_write_concern, Retryability},
9+
operation::{append_options, Retryability},
1010
options::{AggregateOptions, ReadPreference, SelectionCriteria, WriteConcern},
1111
Namespace,
1212
};
@@ -55,7 +55,6 @@ impl OperationWithDefaults for Aggregate {
5555
"cursor": {}
5656
};
5757

58-
remove_empty_write_concern!(self.options);
5958
append_options(&mut body, self.options.as_ref())?;
6059

6160
if self.is_out_or_merge() {

src/operation/commit_transaction.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@ use crate::bson::rawdoc;
55
use crate::{
66
cmap::{Command, RawCommandResponse, StreamDescription},
77
error::Result,
8-
operation::{
9-
append_options_to_raw_document,
10-
remove_empty_write_concern,
11-
OperationWithDefaults,
12-
Retryability,
13-
},
8+
operation::{append_options_to_raw_document, OperationWithDefaults, Retryability},
149
options::{Acknowledgment, TransactionOptions, WriteConcern},
1510
};
1611

@@ -36,7 +31,6 @@ impl OperationWithDefaults for CommitTransaction {
3631
Self::NAME: 1,
3732
};
3833

39-
remove_empty_write_concern!(self.options);
4034
append_options_to_raw_document(&mut body, self.options.as_ref())?;
4135

4236
Ok(Command::new(

src/operation/create.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ use crate::bson::rawdoc;
33
use crate::{
44
cmap::{Command, RawCommandResponse, StreamDescription},
55
error::Result,
6-
operation::{
7-
append_options_to_raw_document,
8-
remove_empty_write_concern,
9-
OperationWithDefaults,
10-
WriteConcernOnlyBody,
11-
},
6+
operation::{append_options_to_raw_document, OperationWithDefaults, WriteConcernOnlyBody},
127
options::{CreateCollectionOptions, WriteConcern},
138
Namespace,
149
};
@@ -37,7 +32,6 @@ impl OperationWithDefaults for Create {
3732
Self::NAME: self.ns.coll.clone(),
3833
};
3934

40-
remove_empty_write_concern!(self.options);
4135
append_options_to_raw_document(&mut body, self.options.as_ref())?;
4236

4337
Ok(Command::new(

src/operation/create_indexes.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ use crate::{
55
cmap::{Command, RawCommandResponse, StreamDescription},
66
error::{ErrorKind, Result},
77
index::IndexModel,
8-
operation::{
9-
append_options_to_raw_document,
10-
remove_empty_write_concern,
11-
OperationWithDefaults,
12-
},
8+
operation::{append_options_to_raw_document, OperationWithDefaults},
139
options::{CreateIndexOptions, WriteConcern},
1410
results::CreateIndexesResult,
1511
Namespace,
@@ -65,7 +61,6 @@ impl OperationWithDefaults for CreateIndexes {
6561
"indexes": indexes,
6662
};
6763

68-
remove_empty_write_concern!(self.options);
6964
append_options_to_raw_document(&mut body, self.options.as_ref())?;
7065

7166
Ok(Command::new(

src/operation/delete.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,7 @@ use crate::{
44
coll::Namespace,
55
collation::Collation,
66
error::{convert_insert_many_error, Result},
7-
operation::{
8-
append_options,
9-
remove_empty_write_concern,
10-
OperationWithDefaults,
11-
Retryability,
12-
WriteResponseBody,
13-
},
7+
operation::{append_options, OperationWithDefaults, Retryability, WriteResponseBody},
148
options::{DeleteOptions, Hint, WriteConcern},
159
results::DeleteResult,
1610
};
@@ -70,7 +64,6 @@ impl OperationWithDefaults for Delete {
7064
"ordered": true, // command monitoring tests expect this (SPEC-1130)
7165
};
7266

73-
remove_empty_write_concern!(self.options);
7467
append_options(&mut body, self.options.as_ref())?;
7568

7669
Ok(Command::new(

0 commit comments

Comments
 (0)