Skip to content
Open
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/bindings-csharp/Runtime/Attrs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ public string Value
{
return value.ToString()?.ToLower()!;
}
if (value is float f)
{
return $"{f.ToString("R", System.Globalization.CultureInfo.InvariantCulture)}F";
}
var str = value.ToString();
if (value is string)
{
Expand Down
6 changes: 5 additions & 1 deletion crates/bindings-macro/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,10 +1026,14 @@ 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,
value: #val.serialize(spacetimedb::sats::algebraic_value::ser::ValueSerializer).expect("default value serialization failed"),
value: {
let value: #ty = #val;
value.serialize(spacetimedb::sats::algebraic_value::ser::ValueSerializer).expect("default value serialization failed")
},
},
})
} else {
Expand Down
23 changes: 23 additions & 0 deletions crates/bindings/tests/defaults.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use spacetimedb::sats::{F32, F64};
use spacetimedb::table::TableInternal;
use spacetimedb::AlgebraicValue;

#[spacetimedb::table(accessor = defaults)]

@bfops bfops Jul 30, 2026

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should either

  1. remove this file
  2. extend it to cover all default datatypes

I don't have a strong opinion about which, but I would like the reviewer to have one.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the custom_defaults smoketest already test this behavior? With that understanding, I'd like to remove this file and keep the tests in the smoketest.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it does in a sense, just a bit less directly; it doesn't check that it gets coerced to the right
AlgebraicValue, but if you think it's close enough to be redundant (or maybe just doesn't matter) then I'm happy to remove this.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The smoketest does check that the default value in a newly-inserted row is the same as the declared one, right? I don't see how that test could pass while this one fails.

pub struct Defaults {
pub id: u32,
#[default(32.5)]
pub f32_value: f32,
#[default(64.25)]
pub f64_value: f64,
}

#[test]
fn float_defaults_use_the_column_type() {
let defaults = defaults__TableHandle::get_default_col_values();

assert_eq!(defaults.len(), 2);
assert_eq!(defaults[0].col_id, 1);
assert_eq!(defaults[0].value, AlgebraicValue::F32(F32::from(32.5)));
assert_eq!(defaults[1].col_id, 2);
assert_eq!(defaults[1].value, AlgebraicValue::F64(F64::from(64.25)));
}
72 changes: 24 additions & 48 deletions crates/smoketests/tests/smoketests/column_defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,17 @@ use spacetimedb_smoketests::{
const EXPECTED_DEFAULTS: &[(&str, &str)] = &[
("bool_value", "true"),
("u8_value", "8"),
// TODO: uncomment this once negative defaults are fixed in Rust
// https://github.com/clockworklabs/SpacetimeDB/issues/5622
//("i8_value", "-8"),
("i8_value", "-8"),
("u16_value", "16"),
// TODO: uncomment this once negative defaults are fixed in Rust
// https://github.com/clockworklabs/SpacetimeDB/issues/5622
//("i16_value", "-16"),
("i16_value", "-16"),
("u32_value", "32"),
// TODO: uncomment this once negative defaults are fixed in Rust
// https://github.com/clockworklabs/SpacetimeDB/issues/5622
//("i32_value", "-32"),

// TODO: uncomment once u64s are fixed.
// https://github.com/clockworklabs/SpacetimeDB/issues/5623
//("u64_value", "64"),

// TODO: uncomment this once negative defaults are fixed in Rust
// https://github.com/clockworklabs/SpacetimeDB/issues/5622
//("i64_value", "-64"),

// TODO: uncomment once floats are fixed in C# and f32s are fixed in Rust
// https://github.com/clockworklabs/SpacetimeDB/issues/5624
// https://github.com/clockworklabs/SpacetimeDB/issues/5627
//("f32_positive_value", "32.5"),

// TODO: uncomment once floats are fixed in C#, f32s are fixed in Rust, and negative defaults are fixed in Rust
// https://github.com/clockworklabs/SpacetimeDB/issues/5624
// https://github.com/clockworklabs/SpacetimeDB/issues/5627
// https://github.com/clockworklabs/SpacetimeDB/issues/5622
//("f32_negative_value", "-32.5"),
("i32_value", "-32"),
("u64_value", "64"),
("i64_value", "-64"),
("f32_positive_value", "32.5"),
("f32_negative_value", "-32.5"),
("f64_positive_value", "64.25"),
// TODO: uncomment once negative defaults are fixed in Rust
// https://github.com/clockworklabs/SpacetimeDB/issues/5622
//("f64_negative_value", "-64.25"),

("f64_negative_value", "-64.25"),
// TODO: uncomment this once string default values are fixed in Rust
//("string_value", r#""default string""#),
];
Expand Down Expand Up @@ -134,30 +110,30 @@ pub struct DefaultsTestTable {
pub id: u32,
#[default(true)]
pub bool_value: bool,
// #[default(-8)]
// pub i8_value: i8,
#[default(-8)]
pub i8_value: i8,
#[default(8)]
pub u8_value: u8,
// #[default(-16)]
// pub i16_value: i16,
#[default(-16)]
pub i16_value: i16,
#[default(16)]
pub u16_value: u16,
// #[default(-32)]
// pub i32_value: i32,
#[default(-32)]
pub i32_value: i32,
#[default(32)]
pub u32_value: u32,
// #[default(-64)]
// pub i64_value: i64,
// #[default(64)]
// pub u64_value: u64,
#[default(-64)]
pub i64_value: i64,
#[default(64)]
pub u64_value: u64,
#[default(32.5)]
pub f32_positive_value: f32,
// #[default(-32.5)]
// pub f32_negative_value: f32,
#[default(-32.5)]
pub f32_negative_value: f32,
#[default(64.25)]
pub f64_positive_value: f64,
// #[default(-64.25)]
// pub f64_negative_value: f64,
#[default(-64.25)]
pub f64_negative_value: f64,
// #[default("default string")]
// pub string_value: String,
}
Expand Down Expand Up @@ -232,8 +208,8 @@ public static partial class Module
[Default(32U)] public uint u32_value;
[Default(-64L)] public long i64_value;
[Default(64UL)] public ulong u64_value;
//[Default(32.5f)] public float f32_positive_value;
//[Default(-32.5f)] public float f32_negative_value;
[Default(32.5f)] public float f32_positive_value;
[Default(-32.5f)] public float f32_negative_value;
[Default(64.25)] public double f64_positive_value;
[Default(-64.25)] public double f64_negative_value;
[Default("default string")] public string string_value;
Expand Down
Loading