use async_trait::async_trait;
use ciborium::Value as CborValue;
use indexmap::IndexMap;
use vantage_core::{Result, error};
use vantage_dataset::traits::ReadableValueSet;
use vantage_table::table::Table;
use vantage_table::traits::table_like::TableLike;
use vantage_types::{EmptyEntity, Record};
use vantage_vista::{
Column as VistaColumn, Reference as VistaReference, TableShell, Vista, VistaCapabilities,
VistaMetadata,
};
use super::factory::{ModelResolver, RestApiVistaFactory};
use crate::RestApi;
#[derive(Clone)]
pub(crate) struct YamlReference {
pub target: String,
pub kind: YamlReferenceKind,
pub foreign_key: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum YamlReferenceKind {
HasMany,
HasOne,
}
pub struct RestApiTableShell {
pub(crate) table: Table<RestApi, EmptyEntity>,
pub(crate) capabilities: VistaCapabilities,
pub(crate) metadata: VistaMetadata,
pub(crate) yaml_refs: IndexMap<String, YamlReference>,
pub(crate) resolver: Option<ModelResolver>,
}
impl RestApiTableShell {
pub(crate) fn new(
table: Table<RestApi, EmptyEntity>,
capabilities: VistaCapabilities,
metadata: VistaMetadata,
) -> Self {
Self {
table,
capabilities,
metadata,
yaml_refs: IndexMap::new(),
resolver: None,
}
}
pub(crate) fn with_yaml_refs(mut self, refs: IndexMap<String, YamlReference>) -> Self {
self.yaml_refs = refs;
self
}
pub(crate) fn with_resolver(mut self, resolver: ModelResolver) -> Self {
self.resolver = Some(resolver);
self
}
}
#[async_trait]
impl TableShell for RestApiTableShell {
fn columns(&self) -> &IndexMap<String, VistaColumn> {
&self.metadata.columns
}
fn references(&self) -> &IndexMap<String, VistaReference> {
&self.metadata.references
}
fn id_column(&self) -> Option<&str> {
self.metadata.id_column.as_deref()
}
async fn list_vista_values(
&self,
_vista: &Vista,
) -> Result<IndexMap<String, Record<CborValue>>> {
self.table.list_values().await
}
async fn get_vista_value(
&self,
_vista: &Vista,
id: &String,
) -> Result<Option<Record<CborValue>>> {
let mut data = self.table.list_values().await?;
Ok(data.shift_remove(id))
}
async fn get_vista_some_value(
&self,
_vista: &Vista,
) -> Result<Option<(String, Record<CborValue>)>> {
let data = self.table.list_values().await?;
Ok(data.into_iter().next())
}
async fn get_vista_count(&self, _vista: &Vista) -> Result<i64> {
self.table.get_count().await
}
fn add_eq_condition(&mut self, field: &str, value: &CborValue) -> Result<()> {
let condition = crate::eq_condition(field, value.clone());
self.table.add_condition(condition);
Ok(())
}
fn get_ref(&self, relation: &str, row: &Record<CborValue>) -> Result<Vista> {
if let Some(yref) = self.yaml_refs.get(relation) {
let resolver = self.resolver.as_ref().ok_or_else(|| {
error!(
"YAML reference requires a model resolver — call \
`with_model_resolver` on the factory or register \
the target spec via `register_yaml`",
relation = relation
)
})?;
let mut child = resolver(&yref.target)?;
let (source_column, target_field) = match yref.kind {
YamlReferenceKind::HasMany => {
let parent_id = self
.table
.id_field()
.map(|c| c.name().to_string())
.ok_or_else(|| {
error!(
"YAML has_many reference needs the parent to have an id field",
relation = relation
)
})?;
(parent_id, yref.foreign_key.clone())
}
YamlReferenceKind::HasOne => {
let child_id = child
.get_id_column()
.map(str::to_string)
.unwrap_or_else(|| "id".to_string());
(yref.foreign_key.clone(), child_id)
}
};
let join_value = row.get(&source_column).cloned().ok_or_else(|| {
error!(
"YAML reference: parent row missing join field",
relation = relation,
source_column = source_column.as_str()
)
})?;
child.add_condition_eq(target_field, join_value)?;
return Ok(child);
}
let target = self.table.get_ref_from_row::<EmptyEntity>(relation, row)?;
let factory = RestApiVistaFactory::new(self.table.data_source().clone());
factory.from_table(target)
}
fn capabilities(&self) -> &VistaCapabilities {
&self.capabilities
}
fn driver_name(&self) -> &'static str {
"rest-api"
}
}