add reference_of!() macro
This commit is contained in:
parent
c7f0caf34a
commit
d473a32820
2 changed files with 25 additions and 1 deletions
|
@ -84,3 +84,27 @@ pub trait Referencable {
|
||||||
Reference(format!("{}::{}", Self::collection_name(), self.id()))
|
Reference(format!("{}::{}", Self::collection_name(), self.id()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a `Reference` of `Model` with specific ID
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
/// * `model`: The `Model` to look for
|
||||||
|
/// * `id`: The ID of the `Model`
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// ```ignore
|
||||||
|
/// let myref: Option<Reference> = reference_of!(Person, "someid");
|
||||||
|
/// ```
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! reference_of {
|
||||||
|
($model:ident, $id:ident) => {{
|
||||||
|
$model::get_partial($id, &serde_json::json!({}))
|
||||||
|
.await
|
||||||
|
.map(|x| x.reference())
|
||||||
|
}};
|
||||||
|
($model:ident, $id:literal) => {{
|
||||||
|
$model::get_partial($id, &serde_json::json!({}))
|
||||||
|
.await
|
||||||
|
.map(|x| x.reference())
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ macro_rules! validate {
|
||||||
/// It will check all supplied types and return `false` if none are matching.
|
/// It will check all supplied types and return `false` if none are matching.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```
|
/// ```ignore
|
||||||
/// fn validate(&self) -> bool {
|
/// fn validate(&self) -> bool {
|
||||||
/// assert_reference_of!(self.owner, Person);
|
/// assert_reference_of!(self.owner, Person);
|
||||||
/// true
|
/// true
|
||||||
|
|
Loading…
Reference in a new issue