use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::database::{FieldId, TableId};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Field {
pub id: FieldId,
pub table_id: TableId,
pub name: String,
pub display_name: String,
pub database_type: String,
pub base_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub semantic_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default = "default_true")]
pub active: bool,
pub position: i32,
#[serde(default)]
pub is_pk: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub fk_target_field_id: Option<FieldId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub visibility_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fingerprint: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_field_values: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub settings: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created_at: Option<DateTime<Utc>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub updated_at: Option<DateTime<Utc>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FieldValues {
pub field_id: FieldId,
pub values: Vec<Value>,
#[serde(default)]
pub has_more_values: bool,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct UpdateFieldRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub semantic_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub visibility_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub settings: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_field_values: Option<String>,
}
fn default_true() -> bool {
true
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_field_serialization() {
let field = Field {
id: FieldId(1),
table_id: TableId(10),
name: "email".to_string(),
display_name: "Email Address".to_string(),
database_type: "VARCHAR(255)".to_string(),
base_type: "type/Text".to_string(),
semantic_type: Some("type/Email".to_string()),
description: Some("User email address".to_string()),
active: true,
position: 3,
is_pk: false,
fk_target_field_id: None,
visibility_type: Some("normal".to_string()),
fingerprint: None,
has_field_values: Some("list".to_string()),
settings: None,
created_at: None,
updated_at: None,
};
let json = serde_json::to_value(&field).unwrap();
assert_eq!(json["name"], "email");
assert_eq!(json["base_type"], "type/Text");
assert_eq!(json["semantic_type"], "type/Email");
}
#[test]
fn test_field_values() {
let values = FieldValues {
field_id: FieldId(5),
values: vec![json!("option1"), json!("option2"), json!("option3")],
has_more_values: false,
};
assert_eq!(values.values.len(), 3);
assert!(!values.has_more_values);
}
#[test]
fn test_update_field_request() {
let request = UpdateFieldRequest {
display_name: Some("Updated Name".to_string()),
semantic_type: Some("type/Category".to_string()),
..Default::default()
};
let json = serde_json::to_value(&request).unwrap();
assert_eq!(json["display_name"], "Updated Name");
assert_eq!(json["semantic_type"], "type/Category");
assert_eq!(json.get("description"), None);
}
}