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::any::AnyTable;
use vantage_table::traits::table_like::TableLike;
use vantage_types::Record;
use vantage_vista::{TableShell, Vista, VistaCapabilities};
use crate::rest::vista::AnyTableShell;
pub struct GraphqlApiTableShell {
pub(crate) table: AnyTable,
pub(crate) capabilities: VistaCapabilities,
}
impl GraphqlApiTableShell {
pub(crate) fn new(table: AnyTable, capabilities: VistaCapabilities) -> Self {
Self {
table,
capabilities,
}
}
}
#[async_trait]
impl TableShell for GraphqlApiTableShell {
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>>> {
self.table.get_value(id).await
}
async fn get_vista_some_value(
&self,
_vista: &Vista,
) -> Result<Option<(String, Record<CborValue>)>> {
self.table.get_some_value().await
}
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 s = match value {
CborValue::Text(s) => s.clone(),
CborValue::Integer(i) => i128::from(*i).to_string(),
CborValue::Float(f) => f.to_string(),
CborValue::Bool(b) => b.to_string(),
CborValue::Null => String::new(),
other => {
return Err(error!(
"GraphqlApiTableShell: eq value must be scalar",
field = field,
value = format!("{:?}", other)
));
}
};
self.table.add_condition_eq(field, &s)
}
fn get_ref(&self, relation: &str) -> Result<Vista> {
let any_table = self.table.get_ref(relation)?;
AnyTableShell::into_vista(any_table)
}
fn capabilities(&self) -> &VistaCapabilities {
&self.capabilities
}
fn driver_name(&self) -> &'static str {
"graphql"
}
}