use once_cell::sync::Lazy;
use serde::Deserialize;
use serde_yaml::from_str;
use std::collections::HashMap;
static TABLE_ID_YAML: &str = include_str!("../../../../table_id_map.yaml");
#[derive(Debug, Deserialize)]
struct TableIdConfig {
mappings: HashMap<String, String>,
}
static TABLE_ID_MAP: Lazy<HashMap<String, String>> = Lazy::new(|| {
let cfg: TableIdConfig = from_str(TABLE_ID_YAML).expect("Invalid table_id_map.yaml");
cfg.mappings
});
pub fn get_resource_id_key(table_name: &str) -> &str {
TABLE_ID_MAP
.get(table_name)
.map(|s| s.as_str())
.unwrap_or("id")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn returns_user_id_for_users_table() {
assert_eq!(get_resource_id_key("users"), "user_id");
}
#[test]
fn defaults_to_id_when_unknown_table() {
assert_eq!(get_resource_id_key("abracadabra_table"), "id");
}
}