Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions crates/bindings-macro/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,10 +1013,21 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
if let Some(val) = &col.default_value {
let ty = &col.ty;
let ident_span = col.ident.span();
Some(quote_spanned! { ident_span =>

// The upper branch of this if clause is special handling for the string type.
if let syn::Type::Path(type_path) = ty
&& let Some(segment) = type_path.path.segments.last()
&& segment.ident.to_string() == "String"
{
Some(quote_spanned! { ident_span =>
let _check: &'static str = #val;
})
} else {
// This closure enforces that `val` is of type `ty` at compile-time.
let _check: #ty = #val;
})
Some(quote_spanned! { ident_span =>
let _check: #ty = #val;
})
}
} else {
None
}
Expand All @@ -1026,6 +1037,7 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R
let col_defaults: Vec<TokenStream> = columns.iter().filter_map(|col| {
if let Some(val) = &col.default_value {
let col_id = col.index;
let ty=&col.ty;
Some(quote! {
spacetimedb::table::ColumnDefault {
col_id: #col_id,
Expand Down
3 changes: 3 additions & 0 deletions modules/module-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct Person {
age: u8,
#[default(false)]
edited: bool,
#[default("test")]
string: String,
}

#[cfg(not(feature = "test-add-column"))]
Expand Down Expand Up @@ -274,6 +276,7 @@ pub fn add(ctx: &ReducerContext, name: String, age: u8) {
name,
age,
edited: false,
string: String::new(),
});
#[cfg(not(feature = "test-add-column"))]
ctx.db.person().insert(Person { id: 0, name, age });
Expand Down