feat: add defaultable field support with companion struct generation

Introduces support for `#[georm(defaultable)]` attribute on entity
fields. When fields are marked as defaultable, generates companion
`<Entity>Default` structs where defaultable fields become `Option<T>`,
enabling easier entity creation when some fields have database defaults
or are auto-generated.

Key features:
- Generates `<Entity>Default` structs with optional defaultable fields
- Implements `Defaultable<Id, Entity>` trait with async `create` method
- Validates that `Option<T>` fields cannot be marked as defaultable
- Preserves field visibility in generated companion structs
- Only generates companion struct when defaultable fields are present
This commit is contained in:
2025-06-04 22:15:38 +02:00
parent aafbfb7964
commit a38b8e873d
10 changed files with 998 additions and 101 deletions

View File

@@ -0,0 +1,144 @@
//! This module creates the defaultable version of a structured derived with
//! Georm. It creates a new struct named `<StructName>Default` where the fields
//! marked as defaultable become an `Option<T>`, where `T` is the initial type
//! of the field.
//!
//! The user does not have to mark a field defaultable if the field already has
//! a type `Option<T>`. It is intended only for fields marked as `NOT NULL` in
//! the database, but not required when creating the entity due to a `DEFAULT`
//! or something similar. The type `<StructName>Default` implements the
//! `Defaultable` trait.
use super::ir::{GeormField, GeormStructAttributes};
use quote::quote;
fn create_defaultable_field(field: &GeormField) -> proc_macro2::TokenStream {
let ident = &field.ident;
let ty = &field.ty;
let vis = &field.field.vis;
// If the field is marked as defaultable, wrap it in Option<T>
// Otherwise, keep the original type
let field_type = if field.defaultable {
quote! { Option<#ty> }
} else {
quote! { #ty }
};
quote! {
#vis #ident: #field_type
}
}
fn generate_defaultable_trait_impl(
struct_name: &syn::Ident,
defaultable_struct_name: &syn::Ident,
struct_attrs: &GeormStructAttributes,
fields: &[GeormField],
) -> proc_macro2::TokenStream {
let table = &struct_attrs.table;
// Find the ID field
let id_field = fields
.iter()
.find(|field| field.id)
.expect("Must have an ID field");
let id_type = &id_field.ty;
// Separate defaultable and non-defaultable fields
let non_defaultable_fields: Vec<_> = fields.iter().filter(|f| !f.defaultable).collect();
let defaultable_fields: Vec<_> = fields.iter().filter(|f| f.defaultable).collect();
// Build static parts for non-defaultable fields
let static_field_names: Vec<String> = non_defaultable_fields.iter().map(|f| f.ident.to_string()).collect();
let static_field_idents: Vec<&syn::Ident> = non_defaultable_fields.iter().map(|f| &f.ident).collect();
// Generate field checks for defaultable fields
let mut field_checks = Vec::new();
let mut bind_checks = Vec::new();
for field in &defaultable_fields {
let field_name = field.ident.to_string();
let field_ident = &field.ident;
field_checks.push(quote! {
if self.#field_ident.is_some() {
dynamic_fields.push(#field_name);
}
});
bind_checks.push(quote! {
if let Some(ref value) = self.#field_ident {
query_builder = query_builder.bind(value);
}
});
}
quote! {
impl ::georm::Defaultable<#id_type, #struct_name> for #defaultable_struct_name {
async fn create(&self, pool: &::sqlx::PgPool) -> ::sqlx::Result<#struct_name> {
let mut dynamic_fields = Vec::new();
#(#field_checks)*
let mut all_fields = vec![#(#static_field_names),*];
all_fields.extend(dynamic_fields);
let placeholders: Vec<String> = (1..=all_fields.len())
.map(|i| format!("${}", i))
.collect();
let query = format!(
"INSERT INTO {} ({}) VALUES ({}) RETURNING *",
#table,
all_fields.join(", "),
placeholders.join(", ")
);
let mut query_builder = ::sqlx::query_as::<_, #struct_name>(&query);
// Bind non-defaultable fields first
#(query_builder = query_builder.bind(&self.#static_field_idents);)*
// Then bind defaultable fields that have values
#(#bind_checks)*
query_builder.fetch_one(pool).await
}
}
}
}
pub fn derive_defaultable_struct(
ast: &syn::DeriveInput,
struct_attrs: &GeormStructAttributes,
fields: &[GeormField],
) -> proc_macro2::TokenStream {
// Only generate if there are defaultable fields
if fields.iter().all(|field| !field.defaultable) {
return quote! {};
}
let struct_name = &ast.ident;
let vis = &ast.vis;
let defaultable_struct_name = quote::format_ident!("{}Default", struct_name);
let defaultable_fields: Vec<proc_macro2::TokenStream> =
fields.iter().map(create_defaultable_field).collect();
let trait_impl = generate_defaultable_trait_impl(
struct_name,
&defaultable_struct_name,
struct_attrs,
fields,
);
quote! {
#[derive(Debug, Clone)]
#vis struct #defaultable_struct_name {
#(#defaultable_fields),*
}
#trait_impl
}
}

View File

@@ -25,6 +25,8 @@ struct GeormFieldAttributes {
pub id: bool,
#[deluxe(default = None)]
pub relation: Option<O2ORelationship>,
#[deluxe(default = false)]
pub defaultable: bool,
}
#[derive(deluxe::ParseMetaItem, Clone, Debug)]
@@ -45,6 +47,7 @@ pub struct GeormField {
pub ty: syn::Type,
pub id: bool,
pub relation: Option<O2ORelationship>,
pub defaultable: bool,
}
impl GeormField {
@@ -53,13 +56,42 @@ impl GeormField {
let ty = field.clone().ty;
let attrs: GeormFieldAttributes =
deluxe::extract_attributes(field).expect("Could not extract attributes from field");
let GeormFieldAttributes { id, relation } = attrs;
let GeormFieldAttributes {
id,
relation,
defaultable,
} = attrs;
// Validate that defaultable is not used on Option<T> fields
if defaultable && Self::is_option_type(&ty) {
panic!(
"Field '{}' is already an Option<T> and cannot be marked as defaultable. \
Remove the #[georm(defaultable)] attribute.",
ident
);
}
Self {
ident,
field: field.to_owned(),
id,
ty,
relation,
defaultable,
}
}
/// Check if a type is Option<T>
fn is_option_type(ty: &syn::Type) -> bool {
match ty {
syn::Type::Path(type_path) => {
if let Some(segment) = type_path.path.segments.last() {
segment.ident == "Option"
} else {
false
}
}
_ => false,
}
}
}

View File

@@ -1,6 +1,7 @@
use ir::GeormField;
use quote::quote;
mod defaultable_struct;
mod ir;
mod relationships;
mod trait_implementation;
@@ -51,9 +52,34 @@ pub fn georm_derive_macro2(
let (fields, id) = extract_georm_field_attrs(&mut ast)?;
let relationships = relationships::derive_relationships(&ast, &struct_attrs, &fields, &id);
let trait_impl = trait_implementation::derive_trait(&ast, &struct_attrs.table, &fields, &id);
let defaultable_struct =
defaultable_struct::derive_defaultable_struct(&ast, &struct_attrs, &fields);
let from_row_impl = generate_from_row_impl(&ast, &fields);
let code = quote! {
#relationships
#trait_impl
#defaultable_struct
#from_row_impl
};
Ok(code)
}
fn generate_from_row_impl(
ast: &syn::DeriveInput,
fields: &[GeormField],
) -> proc_macro2::TokenStream {
let struct_name = &ast.ident;
let field_idents: Vec<&syn::Ident> = fields.iter().map(|f| &f.ident).collect();
let field_names: Vec<String> = fields.iter().map(|f| f.ident.to_string()).collect();
quote! {
impl<'r> ::sqlx::FromRow<'r, ::sqlx::postgres::PgRow> for #struct_name {
fn from_row(row: &'r ::sqlx::postgres::PgRow) -> ::sqlx::Result<Self> {
use ::sqlx::Row;
Ok(Self {
#(#field_idents: row.try_get(#field_names)?),*
})
}
}
}
}