@@ -23,6 +23,7 @@ use serde::{Deserialize, Serialize};
23
23
/// .with_add("/color", "silver")?
24
24
/// .with_move("/from", "/to")?;
25
25
/// # assert_eq!(patch, PatchDocument {
26
+ /// # condition: None,
26
27
/// # operations: vec![
27
28
/// # PatchOperation::Add {
28
29
/// # path: "/color".into(),
@@ -39,10 +40,20 @@ use serde::{Deserialize, Serialize};
39
40
/// ```
40
41
#[ derive( Default , Debug , Serialize , Deserialize , PartialEq , Eq ) ]
41
42
pub struct PatchDocument {
43
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
44
+ pub condition : Option < Cow < ' static , str > > ,
42
45
pub operations : Vec < PatchOperation > ,
43
46
}
44
47
45
48
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 < Cow < ' static , str > > ) -> Self {
53
+ self . condition = Some ( condition. into ( ) ) ;
54
+ self
55
+ }
56
+
46
57
/// Adds a new "add" operation to the patch document.
47
58
///
48
59
/// See the [type documentation](PatchDocument) for more information on patch operations.
@@ -255,6 +266,18 @@ mod tests {
255
266
Ok ( ( ) )
256
267
}
257
268
269
+ #[ test]
270
+ pub fn serialize_condition ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
271
+ let patch_document = PatchDocument :: default ( ) . with_condition ( "from c where c.value = 0" ) ;
272
+
273
+ let serialized = serde_json:: to_string ( & patch_document) . unwrap ( ) ;
274
+ assert_eq ! (
275
+ serialized,
276
+ "{\" condition\" :\" from c where c.value = 0\" ,\" operations\" :[]}"
277
+ ) ;
278
+ Ok ( ( ) )
279
+ }
280
+
258
281
#[ test]
259
282
pub fn serialize_add ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
260
283
let patch_document = PatchDocument :: default ( ) . with_add (
@@ -380,6 +403,30 @@ mod tests {
380
403
Ok ( ( ) )
381
404
}
382
405
406
+ #[ test]
407
+ pub fn cosmos_docs_conditional_patch_example ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
408
+ const TEST_DOC : & str = r#"{
409
+ "condition": "from c where c.Address.ZipCode = '98101'",
410
+ "operations": [
411
+ {
412
+ "op":"replace",
413
+ "path":"/Address/ZipCode",
414
+ "value":98107
415
+ }
416
+ ]
417
+ }"# ;
418
+
419
+ let doc: PatchDocument = serde_json:: from_str ( TEST_DOC ) ?;
420
+
421
+ assert_eq ! (
422
+ doc,
423
+ PatchDocument :: default ( )
424
+ . with_condition( "from c where c.Address.ZipCode = '98101'" )
425
+ . with_replace( "/Address/ZipCode" , 98107 ) ?
426
+ ) ;
427
+ Ok ( ( ) )
428
+ }
429
+
383
430
#[ test]
384
431
pub fn to_json_number_f64 ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
385
432
assert_eq ! (
0 commit comments