Files
rekhoff a546bc8961 Rekhoff/csharp default field values (#3235)
# Description of Changes

This is the implementation of issue #3191. This adds a Default attribute
to C# module fields.

**Note**: In C#, attribute arguments must be compile-time constants,
which means you can't directly use non-constant expressions like new
expressions, method calls, or dynamic values in attribute constructors.
(Ref:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes#2324-attribute-parameter-types)

For this reason, these default values are limited to primitive types,
enums, and strings.
This includes (shown as `C# Type` (`BSATN type`):

* `bool` (`Bool`)
* `sbyte` (`I8`)
* `byte` (`U8`)
* `short` (`I16`)
* `ushort` (`U16`)
* `int` (`I32`)
* `unit` (`U32`)
* `long` (`I64`)
* `ulong` (`U64`)
* `float` (`F32`)
* `double` (`F64`)
* `enum` (`Enum`)
* `string` (`String`)
* `null` (`RefOption`) <- Nullable type

Because of C# limitations, for nullable and complex data types, such as
a struct, can take use `[Default(null)]` to populate values with null
defaults. This allows things like structs to workaround the non-constant
expressions in attribute constructors limitation by allowing these
complex types to still be able to be added as new column tables.

The `int` type can also be in the form of Hex or Binary literals, such
as `[Default(0x2A)]` or `[Default(0b00101010)]`

Both Decimal (like `[Default(3.14m)]`) and Char (like `[Default('A')]`)
are unsupported types in BSATN and will still return `BSATN0001` errors.

# API and ABI breaking changes

Not API breaking.
This change only adds the `[Default(value)]` attribute logic.
Using the `[Default(value)]` attribute with older versions SpacetimeDB
C# modules will result in an error.

# Expected complexity level and risk

2

# Testing

Local testing of this requires use of CLI changes in #3278 

- [x] Regression test of functionality added.
2025-10-06 16:56:37 +00:00
..

SpacetimeDB.Codegen

This project contains Roslyn incremental source generators that augment types and tables with static methods for self-describing and registration. They look for different attributes to know which types to augment:

  • [SpacetimeDB.Type] - generates a GetSatsTypeInfo() static method that registers this type with the runtime and returns a TypeInfo object. It supports only structs for now to explicitly forbid infinitely recursive types and to make the implementation simpler, as it doesn't need to deal with type references - each table is registered as an entirely self-contained type together with its nested structs if any. This is unlikely to be a problem in common scenarios, but it will be optimised in the future.

    All the nested fields will be added to the product type. Because it's not possible to implement static extension methods on 3rd-party types (including built-ins) in C#, the codegen is responsible for manually routing different types to their TypeInfo descriptors. See various static TypeInfo properties and helper methods on SpacetimeDB.BSATN.AlgebraicType (Runtime/AlgebraicType.cs) and routing logic in Utils.GetTypeInfo (Codegen/Utils.cs) for more details.

    Also, for the same reason - absence of static extension methods in C# - the codegen expects that your struct, as well as any of its parents, is partial so methods can be added from extra source files generated by the codegen.

  • [SpacetimeDB.Type] - also supports emulation of tagged enums in C#. For that, the struct needs to inherit a marker interface SpacetimeDB.TaggedEnum<Variants> where Variants is a named tuple of all possible variants, e.g.:

    [SpacetimeDB.Type]
    partial record Option<T> : SpacetimeDB.TaggedEnum<(T Some, Unit None)>;
    

    will generate inherited records Option.Some(T Some_) and Option.None(Unit None_). It allows you to use tagged enums in C# in a similar way to Rust enums by leveraging C# pattern-matching on any instance of Option<T>.

  • [SpacetimeDB.Table] - generates code to register this table in the FFI upon startup so that they can be enumerated by the __describe_module__ FFI API. It implies [SpacetimeDB.Type], so you must not specify both attributes on the same struct.

    The fields can be marked with [SpacetimeDB.ColumnAttrs] and those will be detected by the codegen and passed on to the runtime as well. Example:

    [SpacetimeDB.Table]
    public partial struct Person
    {
        [SpacetimeDB.Column(ColumnAttrs.Identity)]
        public int Id;
        public string Name;
    }
    
  • [SpacetimeDB.Reducer] - generates code to register a static function as a SpacetimeDB reducer in the FFI upon startup and creates a wrapper that will parse SATS binary blob into individual arguments and invoke the underlying function for the __call_reducer__ FFI API.