#[allow(unused_variables)] #[allow(dead_code)] #[allow(unused_must_use)]
#[allow(non_snake_case)]
extern crate rbatis;
extern crate rbdc ;
pub use genies_core as core;
pub use genies_config as config;
pub use genies_context as context;
pub use genies_cache as cache;
pub use genies_dapr as dapr;
pub use genies_ddd as ddd ;
pub use genies_k8s as k8s ;
pub use genies_core::id_gen::next_id;
pub use genies_dapr::{
collect_topic_routers,
collect_topic_subscriptions,
dapr_subscribe_handler,
dapr_event_router,
};
#[macro_export]
macro_rules! pool {
() => {
&mut genies::context::CONTEXT.rbatis.clone()
};
}
#[macro_export]
macro_rules! tx_defer {
() => {
pool!()
.acquire_begin()
.await
.unwrap()
.defer_async(|mut tx| async move {
if !tx.done() {
tx.rollback().await;
log::warn!("tx 没有手动commit 自动执行 rollback,请检查代码!");
} else {
log::debug!("tx 已经 commit 成功");
}
})
};
($rb:ident) => {
$rb.acquire_begin()
.await
.unwrap()
.defer_async(|mut tx| async move {
if !tx.done() {
tx.rollback().await;
log::warn!("tx 没有手动commit 自动执行 rollback,请检查代码!");
} else {
log::debug!("tx 已经 commit 成功");
}
})
};
}
#[macro_export]
macro_rules! copy {
($src:expr,$dest:ty) => {
serde_json::from_slice::<$dest>(&serde_json::to_vec($src).unwrap()).unwrap()
};
}
#[macro_export]
macro_rules! config_gateway {
($servlet_path:expr) => {
once_cell::sync::Lazy::new(|| {
let service_name = $servlet_path.to_string();
let mut gateway = genies::context::CONTEXT
.config
.gateway
.clone()
.unwrap_or_default();
let dapr_url = format!(
"http://localhost:3500/v1.0/invoke{}-service/method",
service_name.clone()
);
let gateway_url = format!("{}{}", gateway, service_name);
if gateway.contains("http://") || gateway.contains("https://") {
gateway.clear();
gateway.push_str(&gateway_url);
} else {
gateway.clear();
gateway.push_str(&dapr_url);
}
gateway
})
};
}
#[cfg(test)]
mod tests {
#[test]
fn copy_string() {
let src1 = "测试字符串".to_string();
let dest = copy!(&src1, String);
println!("{}", dest);
assert_eq!(dest, src1);
}
#[test]
fn copy_obj() {
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
struct A {
name: String,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
struct B {
age: u16,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq)]
struct C {
#[serde(flatten)]
b: Option<B>,
name: Option<String>,
}
let a = A {
name: "A的名字".to_string(),
};
let b = B { age: 10 };
let c = C {
b: Option::from(b.clone()),
name: Option::from("C的名字".to_string()),
};
let dest = copy!(&a, C);
println!("{:?}", dest);
assert_eq!(
dest,
C {
b: None,
name: Option::from("A的名字".to_string())
}
);
let dest = copy!(&c, B);
println!("{:?}", dest);
assert_eq!(dest, B { age: 10 });
let mut dest = copy!(&c, A);
println!("{:?}", dest);
assert_eq!(
dest,
A {
name: "C的名字".to_string()
}
);
dest.name = "修改的名字".to_string();
println!("{:?}", dest);
assert_eq!(
dest,
A {
name: "修改的名字".to_string()
}
);
}
}