#![cfg(feature = "mysql")]
use crate::action::{ActionContext, TypedHandler};
use crate::error::BaseError;
use crate::table::TableEntity;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use yang_base_derive::Action;
#[derive(Deserialize, schemars::JsonSchema, Default)]
#[serde(default)]
pub struct EmptyInput {}
#[derive(Serialize, schemars::JsonSchema)]
pub struct TableSchemaResponse {
pub table_name: String,
pub primary_key: String,
pub input_schema: serde_json::Value,
pub output_schema: serde_json::Value,
}
#[derive(Action)]
#[action(
name = "table",
display_name = "表元信息",
description = "返回表结构与字段 schema"
)]
pub struct TableAction<T: TableEntity> {
_phantom: PhantomData<T>,
}
impl<T: TableEntity> TableAction<T> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<T: TableEntity> Default for TableAction<T> {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl<T: TableEntity> TypedHandler for TableAction<T> {
type Input = EmptyInput;
type Output = TableSchemaResponse;
async fn handle(
&self,
_ctx: ActionContext,
_input: EmptyInput,
) -> Result<TableSchemaResponse, BaseError> {
let schema = schemars::schema_for!(T);
let schema_value = serde_json::to_value(&schema)
.map_err(|e| BaseError::JsonSerializeFailed(e.to_string()))?;
Ok(TableSchemaResponse {
table_name: T::TABLE_NAME.to_string(),
primary_key: T::PK_FIELD.to_string(),
input_schema: schema_value.clone(),
output_schema: schema_value,
})
}
}