use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum UdfType {
Scalar,
Aggregate,
Table,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UdfMetadata {
pub name: String,
pub udf_type: UdfType,
pub param_types: Vec<String>,
pub return_type: String,
pub is_variadic: bool,
pub description: Option<String>,
pub created_at: u64,
}
impl UdfMetadata {
pub fn new(
name: impl Into<String>,
udf_type: UdfType,
param_types: Vec<String>,
return_type: impl Into<String>,
is_variadic: bool,
) -> Self {
Self {
name: name.into(),
udf_type,
param_types,
return_type: return_type.into(),
is_variadic,
description: None,
created_at: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
}
}
pub fn with_description(mut self, desc: impl Into<String>) -> Self {
self.description = Some(desc.into());
self
}
pub fn to_json(&self) -> crate::error::DbxResult<String> {
serde_json::to_string(self).map_err(|e| {
crate::error::DbxError::Serialization(format!(
"Failed to serialize UDF metadata: {}",
e
))
})
}
pub fn from_json(json: &str) -> crate::error::DbxResult<Self> {
serde_json::from_str(json).map_err(|e| {
crate::error::DbxError::Serialization(format!(
"Failed to deserialize UDF metadata: {}",
e
))
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_udf_metadata_creation() {
let meta = UdfMetadata::new(
"my_function",
UdfType::Scalar,
vec!["Int".to_string(), "String".to_string()],
"Boolean",
false,
);
assert_eq!(meta.name, "my_function");
assert_eq!(meta.udf_type, UdfType::Scalar);
assert_eq!(meta.param_types.len(), 2);
assert_eq!(meta.return_type, "Boolean");
assert!(!meta.is_variadic);
}
#[test]
fn test_udf_metadata_serialization() {
let meta = UdfMetadata::new(
"test_udf",
UdfType::Aggregate,
vec!["Float".to_string()],
"Float",
true,
)
.with_description("Test aggregate function");
let json = meta.to_json().unwrap();
let deserialized = UdfMetadata::from_json(&json).unwrap();
assert_eq!(meta.name, deserialized.name);
assert_eq!(meta.udf_type, deserialized.udf_type);
assert_eq!(meta.param_types, deserialized.param_types);
assert_eq!(meta.return_type, deserialized.return_type);
assert_eq!(meta.is_variadic, deserialized.is_variadic);
assert_eq!(meta.description, deserialized.description);
}
}