Skip to content

Commit f16d165

Browse files
committed
Add PatchDocument::condition and builder method for it
1 parent 4bbc5a9 commit f16d165

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

sdk/cosmos/azure_data_cosmos/examples/cosmos/patch.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ impl PatchCommand {
4040
.iter()
4141
.map(|op| serde_json::from_str(op).expect("Invalid JSON patch operation"))
4242
.collect();
43-
let patch = PatchDocument { operations };
43+
let patch = PatchDocument {
44+
condition: None,
45+
operations,
46+
};
4447

4548
let response = container_client
4649
.patch_item(pk, &self.item_id, patch, None)

sdk/cosmos/azure_data_cosmos/src/models/patch_operations.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use serde::{Deserialize, Serialize};
2323
/// .with_add("/color", "silver")?
2424
/// .with_move("/from", "/to")?;
2525
/// # assert_eq!(patch, PatchDocument {
26+
/// # condition: None,
2627
/// # operations: vec![
2728
/// # PatchOperation::Add {
2829
/// # path: "/color".into(),
@@ -39,10 +40,20 @@ use serde::{Deserialize, Serialize};
3940
/// ```
4041
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
4142
pub struct PatchDocument {
43+
#[serde(skip_serializing_if = "Option::is_none")]
44+
pub condition: Option<String>,
4245
pub operations: Vec<PatchOperation>,
4346
}
4447

4548
impl PatchDocument {
49+
/// Adds a condition, which determines whether or not the patch should be applied.
50+
///
51+
/// The value is an SQL-like filter predicate as a string. For example, `from c where c.taskNum = 3`.
52+
pub fn with_condition(mut self, condition: impl Into<String>) -> Self {
53+
self.condition = Some(condition.into());
54+
self
55+
}
56+
4657
/// Adds a new "add" operation to the patch document.
4758
///
4859
/// See the [type documentation](PatchDocument) for more information on patch operations.

0 commit comments

Comments
 (0)