use crate::{SqliteBootstrapProvider, TableKeyConfig};
use drasi_plugin_sdk::prelude::*;
use utoipa::OpenApi;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, utoipa::ToSchema)]
#[schema(as = bootstrap::sqlite::SqliteBootstrapConfig)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SqliteBootstrapConfigDto {
#[serde(default)]
#[schema(value_type = Option<ConfigValueString>)]
pub path: Option<ConfigValue<String>>,
#[serde(default)]
pub tables: Option<Vec<String>>,
#[serde(default)]
#[schema(value_type = Vec<bootstrap::sqlite::TableKeyConfig>)]
pub table_keys: Vec<TableKeyConfigDto>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, utoipa::ToSchema)]
#[schema(as = bootstrap::sqlite::TableKeyConfig)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TableKeyConfigDto {
pub table: String,
pub key_columns: Vec<String>,
}
#[derive(OpenApi)]
#[openapi(components(schemas(SqliteBootstrapConfigDto, TableKeyConfigDto,)))]
struct SqliteBootstrapSchemas;
pub struct SqliteBootstrapDescriptor;
#[async_trait]
impl BootstrapPluginDescriptor for SqliteBootstrapDescriptor {
fn kind(&self) -> &str {
"sqlite"
}
fn config_version(&self) -> &str {
"1.0.0"
}
fn config_schema_name(&self) -> &str {
"bootstrap.sqlite.SqliteBootstrapConfig"
}
fn config_schema_json(&self) -> String {
let api = SqliteBootstrapSchemas::openapi();
serde_json::to_string(
&api.components
.as_ref()
.expect("OpenAPI components missing")
.schemas,
)
.expect("Failed to serialize config schema")
}
async fn create_bootstrap_provider(
&self,
config_json: &serde_json::Value,
_source_config_json: &serde_json::Value,
) -> anyhow::Result<Box<dyn drasi_lib::bootstrap::BootstrapProvider>> {
let dto: SqliteBootstrapConfigDto = serde_json::from_value(config_json.clone())?;
let mapper = DtoMapper::new();
let mut builder = SqliteBootstrapProvider::builder();
if let Some(path_cv) = &dto.path {
builder = builder.with_path(mapper.resolve_string(path_cv).await?);
}
if let Some(tables) = dto.tables {
builder = builder.with_tables(tables);
}
let table_keys = dto
.table_keys
.into_iter()
.map(|tk| TableKeyConfig {
table: tk.table,
key_columns: tk.key_columns,
})
.collect::<Vec<_>>();
if !table_keys.is_empty() {
builder = builder.with_table_keys(table_keys);
}
Ok(Box::new(builder.build()))
}
}