use std::collections::HashMap;
use std::sync::OnceLock;
use crate::runtime::descriptor_manifest::descriptor_contract_manifest_static;
use crate::runtime::service::native_registry::canonical_service_id;
pub(crate) fn proto_native_store_backend() -> &'static str {
static BACKEND: OnceLock<&'static str> = OnceLock::new();
*BACKEND.get_or_init(|| {
let manifest = descriptor_contract_manifest_static();
let requires_postgres = manifest
.services
.iter()
.filter_map(|service| service.native_service.as_ref())
.any(|native| native.requires_postgres);
if requires_postgres { "postgres" } else { "" }
})
}
fn native_store_backend_map() -> &'static HashMap<String, &'static str> {
static MAP: OnceLock<HashMap<String, &'static str>> = OnceLock::new();
MAP.get_or_init(|| {
let manifest = descriptor_contract_manifest_static();
let mut map = HashMap::new();
for service in &manifest.services {
if let Some(native) = service.native_service.as_ref() {
if native.requires_postgres {
map.insert(canonical_service_id(&native.service_id), "postgres");
}
}
}
map
})
}
pub(crate) fn native_service_store_backend(service_id: &str) -> Option<&'static str> {
native_store_backend_map()
.get(&canonical_service_id(service_id))
.copied()
}
#[cfg(test)]
mod tests {
use super::{native_service_store_backend, proto_native_store_backend};
#[test]
fn native_store_backend_is_derived_from_proto_contracts() {
assert_eq!(proto_native_store_backend(), "postgres");
}
#[test]
fn per_service_store_backend_is_derived_from_proto() {
for id in [
"storage",
"notification",
"tenant",
"analytics",
"asset",
"authn",
"authz",
"control",
"idp",
"webrtc.room",
] {
assert_eq!(
native_service_store_backend(id),
Some("postgres"),
"service {id} must derive its store from proto"
);
}
assert_eq!(native_service_store_backend("does-not-exist"), None);
}
}