use crate::core::SqlValue;
pub type DeriveFn = fn(&mut Vec<(&'static str, SqlValue)>, Option<&serde_json::Value>);
pub struct AdminDerivedField {
pub table: &'static str,
pub derive: DeriveFn,
}
inventory::collect!(AdminDerivedField);
pub(crate) fn apply(
table: &str,
values: &mut Vec<(&'static str, SqlValue)>,
before: Option<&serde_json::Value>,
) {
for entry in inventory::iter::<AdminDerivedField> {
if entry.table == table {
(entry.derive)(values, before);
}
}
}
#[cfg_attr(not(feature = "sso"), allow(dead_code))]
pub(crate) fn text(
values: &[(&'static str, SqlValue)],
before: Option<&serde_json::Value>,
column: &str,
) -> Option<String> {
values
.iter()
.find(|(c, _)| *c == column)
.and_then(|(_, v)| match v {
SqlValue::String(s) => Some(s.clone()),
_ => None,
})
.or_else(|| before?.get(column)?.as_str().map(str::to_owned))
}