Skip to content

Commit

Permalink
feat(core): impl delete_with on blocking operator (#2633)
Browse files Browse the repository at this point in the history
feat(core): delete_with on blocking operator

Signed-off-by: suyanhanx <[email protected]>
  • Loading branch information
suyanhanx authored Jul 13, 2023
1 parent c0205fd commit 4eadb78
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
36 changes: 34 additions & 2 deletions core/src/types/operator/blocking_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,11 +673,43 @@ impl BlockingOperator {
/// # }
/// ```
pub fn delete(&self, path: &str) -> Result<()> {
self.delete_with(path).call()?;

Ok(())
}

/// Delete given path with options.
///
/// # Notes
///
/// - Delete not existing error won't return errors.
///
/// # Examples
///
/// ```no_run
/// # use anyhow::Result;
/// # use futures::io;
/// # use opendal::BlockingOperator;
/// # fn test(op: BlockingOperator) -> Result<()> {
/// let _ = op
/// .delete_with("path/to/file")
/// .version("example_version").call()?;
/// # Ok(())
/// # }
/// ```
pub fn delete_with(&self, path: &str) -> FunctionDelete {
let path = normalize_path(path);

let _ = self.inner().blocking_delete(&path, OpDelete::new())?;
FunctionDelete(OperatorFunction::new(
self.inner().clone(),
path,
OpDelete::new(),
|inner, path, args| {
let _ = inner.blocking_delete(&path, args)?;

Ok(())
Ok(())
},
))
}

/// remove will remove files via the given paths.
Expand Down
19 changes: 19 additions & 0 deletions core/src/types/operator/operator_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,22 @@ impl FunctionWrite {
self.0.call()
}
}

/// Function that generated by [`BlockingOperator::delete_with`].
///
/// Users can add more options by public functions provided by this struct.
pub struct FunctionDelete(pub(crate) OperatorFunction<OpDelete, ()>);

impl FunctionDelete {
/// Set the version for this operation.
pub fn version(mut self, v: &str) -> Self {
self.0 = self.0.map_args(|args| args.with_version(v));
self
}

/// Call the function to consume all the input and generate a
/// result.
pub fn call(self) -> Result<()> {
self.0.call()
}
}

0 comments on commit 4eadb78

Please sign in to comment.