pub struct AttributeBehavior {
pub name_str: &'static str,
pub event: HookEvent,
pub target_field: &'static str,
pub generator: Box<dyn Fn(&HookContext) -> Value + Send + Sync>,
}Expand description
通用属性 Behavior — 在指定事件触发时通过闭包设置属性
§示例
use sz_orm_model::behaviors::{AttributeBehavior, BehaviorRegistry, Behavior};
use sz_orm_model::hooks::{HookContext, HookEvent};
use sz_orm_model::Value;
use std::collections::HashMap;
let mut registry = BehaviorRegistry::new();
// 在 before_insert 时设置 uuid 字段
registry.register(Box::new(AttributeBehavior::new(
"uuid_gen",
HookEvent::BeforeInsert,
"uuid",
|_ctx| Value::String("auto-uuid".to_string()),
)));
let ctx = HookContext::default();
let mut attrs = HashMap::new();
registry.before_insert(&ctx, &mut attrs).unwrap();
assert_eq!(attrs.get("uuid"), Some(&Value::String("auto-uuid".to_string())));Fields§
§name_str: &'static strBehavior 名称
event: HookEvent订阅的事件(仅在该事件触发时执行)
target_field: &'static str目标字段名
generator: Box<dyn Fn(&HookContext) -> Value + Send + Sync>值生成闭包
Implementations§
Trait Implementations§
Source§impl Behavior for AttributeBehavior
impl Behavior for AttributeBehavior
Source§fn before_insert(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
fn before_insert( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
在 insert 前触发(默认空实现)
Source§fn before_update(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
fn before_update( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
在 update 前触发(默认空实现)
Source§fn after_find(
&self,
ctx: &HookContext,
attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
fn after_find( &self, ctx: &HookContext, attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
在 find 后触发(默认空实现,可用于字段后处理)
Source§fn before_delete(
&self,
_ctx: &HookContext,
_attrs: &mut HashMap<String, Value>,
) -> BehaviorResult<()>
fn before_delete( &self, _ctx: &HookContext, _attrs: &mut HashMap<String, Value>, ) -> BehaviorResult<()>
在 delete 前触发(默认空实现)
Auto Trait Implementations§
impl !RefUnwindSafe for AttributeBehavior
impl !UnwindSafe for AttributeBehavior
impl Freeze for AttributeBehavior
impl Send for AttributeBehavior
impl Sync for AttributeBehavior
impl Unpin for AttributeBehavior
impl UnsafeUnpin for AttributeBehavior
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more