add reference_of!() macro

This commit is contained in:
JMARyA 2024-07-18 17:15:12 +02:00
parent c7f0caf34a
commit d473a32820
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
2 changed files with 25 additions and 1 deletions

View file

@ -84,3 +84,27 @@ pub trait Referencable {
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())
}};
}

View file

@ -18,7 +18,7 @@ macro_rules! validate {
/// It will check all supplied types and return `false` if none are matching.
///
/// # Example
/// ```
/// ```ignore
/// fn validate(&self) -> bool {
/// assert_reference_of!(self.owner, Person);
/// true