From d352e4543ba90dc4183dcd7fd03dca9bd4da580c Mon Sep 17 00:00:00 2001 From: Jeremie Pelletier Date: Fri, 27 Sep 2024 07:23:50 -0400 Subject: [PATCH] Jeremie/cs module (#1719) --- crates/bindings-csharp/BSATN.Runtime/Db.cs | 8 + .../Codegen.Tests/fixtures/server/Lib.cs | 35 +- .../server/snapshots/Module#FFI.verified.cs | 401 +++++++++- .../snapshots/Module#PrivateTable.verified.cs | 45 +- .../snapshots/Module#PublicTable.verified.cs | 254 ++----- ...Module#Timers.SendMessageTimer.verified.cs | 103 +-- .../snapshots/Type#CustomClass.verified.cs | 2 +- crates/bindings-csharp/Codegen/Module.cs | 389 ++++++---- crates/bindings-csharp/Runtime/Attrs.cs | 82 +- .../Runtime/Internal/IReducer.cs | 4 +- .../Runtime/Internal/ITable.cs | 24 +- .../Runtime/Internal/Module.cs | 34 +- .../bindings-csharp/Runtime/LogStopwatch.cs | 6 +- crates/bindings-csharp/Runtime/Runtime.cs | 24 - crates/bindings-csharp/SpacetimeSharpSATS.sln | 24 + .../src/subcommands/project/csharp/Lib._cs | 14 +- modules/sdk-test-connect-disconnect-cs/Lib.cs | 32 +- ... => sdk-test-connect-disconnect-cs.csproj} | 1 + modules/sdk-test-cs/Lib.cs | 701 +++++++++--------- .../{StdbModule.csproj => sdk-test-cs.csproj} | 1 + modules/sdk-test-multi-cs/.gitignore | 2 + modules/sdk-test-multi-cs/Lib.cs | 50 ++ modules/sdk-test-multi-cs/README.md | 3 + .../sdk-test-multi-cs.csproj} | 3 +- modules/spacetimedb-quickstart-cs/Lib.cs | 38 +- .../spacetimedb-quickstart-server-cs.csproj | 21 + 26 files changed, 1406 insertions(+), 895 deletions(-) create mode 100644 crates/bindings-csharp/BSATN.Runtime/Db.cs rename modules/sdk-test-connect-disconnect-cs/{StdbModule.csproj => sdk-test-connect-disconnect-cs.csproj} (94%) rename modules/sdk-test-cs/{StdbModule.csproj => sdk-test-cs.csproj} (95%) create mode 100644 modules/sdk-test-multi-cs/.gitignore create mode 100644 modules/sdk-test-multi-cs/Lib.cs create mode 100644 modules/sdk-test-multi-cs/README.md rename modules/{spacetimedb-quickstart-cs/StdbModule.csproj => sdk-test-multi-cs/sdk-test-multi-cs.csproj} (90%) create mode 100644 modules/spacetimedb-quickstart-cs/spacetimedb-quickstart-server-cs.csproj diff --git a/crates/bindings-csharp/BSATN.Runtime/Db.cs b/crates/bindings-csharp/BSATN.Runtime/Db.cs new file mode 100644 index 00000000000..7f3e6ebe8f3 --- /dev/null +++ b/crates/bindings-csharp/BSATN.Runtime/Db.cs @@ -0,0 +1,8 @@ +namespace SpacetimeDB; + +public abstract record DbContext(DbView Db) + where DbView : class, new() +{ + public DbContext() + : this(new DbView()) { } +} diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs index 32bbc789123..98f2c3816e2 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs @@ -13,16 +13,16 @@ public partial struct CustomStruct } [SpacetimeDB.Type] -public partial struct CustomClass +public partial class CustomClass { public const int IGNORE_ME = 0; public static readonly string IGNORE_ME_TOO = ""; - public int IntField; - public string StringField; + public int IntField = 0; + public string StringField = ""; } [StructLayout(LayoutKind.Auto)] -public partial struct CustomClass +public partial class CustomClass { public int IgnoreExtraFields; } @@ -44,7 +44,8 @@ public partial class PrivateTable { } [SpacetimeDB.Table] public partial struct PublicTable { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKeyAuto)] + [SpacetimeDB.AutoInc] + [SpacetimeDB.PrimaryKey] public int Id; public byte ByteField; @@ -81,18 +82,18 @@ public partial struct PublicTable public static partial class Reducers { [SpacetimeDB.Reducer] - public static void InsertData(PublicTable data) + public static void InsertData(ReducerContext ctx, PublicTable data) { - data.Insert(); + ctx.Db.PublicTable.Insert(data); Log.Info("New list"); - foreach (var item in PublicTable.Iter()) + foreach (var item in ctx.Db.PublicTable.Iter()) { Log.Info($"Item: {item.StringField}"); } } [SpacetimeDB.Reducer] - public static void ScheduleImmediate(PublicTable data) + public static void ScheduleImmediate(ReducerContext ctx, PublicTable data) { VolatileNonatomicScheduleImmediateInsertData(data); } @@ -107,7 +108,7 @@ public static partial class AndClasses [SpacetimeDB.Reducer("test_custom_name_and_reducer_ctx")] public static void InsertData2(ReducerContext ctx, PublicTable data) { - data.Insert(); + ctx.Db.PublicTable.Insert(data); } } } @@ -122,7 +123,7 @@ public partial struct SendMessageTimer } [SpacetimeDB.Reducer] - public static void SendScheduledMessage(SendMessageTimer arg) + public static void SendScheduledMessage(ReducerContext ctx, SendMessageTimer arg) { // verify that fields were auto-added ulong id = arg.ScheduledId; @@ -133,10 +134,12 @@ public static void SendScheduledMessage(SendMessageTimer arg) [SpacetimeDB.Reducer(ReducerKind.Init)] public static void Init(ReducerContext ctx) { - new SendMessageTimer - { - Text = "bot sending a message", - ScheduledAt = ctx.Time.AddSeconds(10), - }.Insert(); + ctx.Db.SendMessageTimer.Insert( + new SendMessageTimer + { + Text = "bot sending a message", + ScheduledAt = ctx.Time.AddSeconds(10), + } + ); } } diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs index 60368e8ec9f..275b221fc9f 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs @@ -6,6 +6,373 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +namespace SpacetimeDB +{ + public sealed record ReducerContext : DbContext, Internal.IReducerContext + { + public readonly Identity Sender; + public readonly Address? Address; + public readonly Random Random; + public readonly DateTimeOffset Time; + + internal ReducerContext( + Identity sender, + Address? address, + Random random, + DateTimeOffset time + ) + { + Sender = sender; + Address = address; + Random = random; + Time = time; + } + } + + namespace Internal.TableHandles + { + public readonly struct PrivateTable + : SpacetimeDB.Internal.ITableView + { + static global::PrivateTable SpacetimeDB.Internal.ITableView< + PrivateTable, + global::PrivateTable + >.ReadGenFields(System.IO.BinaryReader reader, global::PrivateTable row) + { + return row; + } + + public IEnumerable Iter() => + SpacetimeDB.Internal.ITableView.Iter(); + + public IEnumerable Query( + System.Linq.Expressions.Expression> predicate + ) => + SpacetimeDB.Internal.ITableView.Query( + predicate + ); + + public global::PrivateTable Insert(global::PrivateTable row) => + SpacetimeDB.Internal.ITableView.Insert(row); + } + + public readonly struct PublicTable + : SpacetimeDB.Internal.ITableView + { + static global::PublicTable SpacetimeDB.Internal.ITableView< + PublicTable, + global::PublicTable + >.ReadGenFields(System.IO.BinaryReader reader, global::PublicTable row) + { + if (row.Id == default) + { + row.Id = global::PublicTable.BSATN.Id.Read(reader); + } + return row; + } + + public IEnumerable Iter() => + SpacetimeDB.Internal.ITableView.Iter(); + + public IEnumerable Query( + System.Linq.Expressions.Expression> predicate + ) => SpacetimeDB.Internal.ITableView.Query(predicate); + + public global::PublicTable Insert(global::PublicTable row) => + SpacetimeDB.Internal.ITableView.Insert(row); + + public IEnumerable FilterById(int Id) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 0, + Id, + global::PublicTable.BSATN.Id + ) + .Iter(); + + public global::PublicTable? FindById(int Id) => + FilterById(Id).Cast().SingleOrDefault(); + + public bool DeleteById(int Id) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 0, + Id, + global::PublicTable.BSATN.Id + ) + .Delete(); + + public bool UpdateById(int Id, global::PublicTable @this) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 0, + Id, + global::PublicTable.BSATN.Id + ) + .Update(@this); + + public IEnumerable FilterByByteField(byte ByteField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 1, + ByteField, + global::PublicTable.BSATN.ByteField + ) + .Iter(); + + public IEnumerable FilterByUshortField(ushort UshortField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 2, + UshortField, + global::PublicTable.BSATN.UshortField + ) + .Iter(); + + public IEnumerable FilterByUintField(uint UintField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 3, + UintField, + global::PublicTable.BSATN.UintField + ) + .Iter(); + + public IEnumerable FilterByUlongField(ulong UlongField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 4, + UlongField, + global::PublicTable.BSATN.UlongField + ) + .Iter(); + + public IEnumerable FilterByUInt128Field( + System.UInt128 UInt128Field + ) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 5, + UInt128Field, + global::PublicTable.BSATN.UInt128Field + ) + .Iter(); + + public IEnumerable FilterByU128Field(SpacetimeDB.U128 U128Field) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 6, + U128Field, + global::PublicTable.BSATN.U128Field + ) + .Iter(); + + public IEnumerable FilterByU256Field(SpacetimeDB.U256 U256Field) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 7, + U256Field, + global::PublicTable.BSATN.U256Field + ) + .Iter(); + + public IEnumerable FilterBySbyteField(sbyte SbyteField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 8, + SbyteField, + global::PublicTable.BSATN.SbyteField + ) + .Iter(); + + public IEnumerable FilterByShortField(short ShortField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 9, + ShortField, + global::PublicTable.BSATN.ShortField + ) + .Iter(); + + public IEnumerable FilterByIntField(int IntField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 10, + IntField, + global::PublicTable.BSATN.IntField + ) + .Iter(); + + public IEnumerable FilterByLongField(long LongField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 11, + LongField, + global::PublicTable.BSATN.LongField + ) + .Iter(); + + public IEnumerable FilterByInt128Field( + System.Int128 Int128Field + ) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 12, + Int128Field, + global::PublicTable.BSATN.Int128Field + ) + .Iter(); + + public IEnumerable FilterByI128Field(SpacetimeDB.I128 I128Field) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 13, + I128Field, + global::PublicTable.BSATN.I128Field + ) + .Iter(); + + public IEnumerable FilterByI256Field(SpacetimeDB.I256 I256Field) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 14, + I256Field, + global::PublicTable.BSATN.I256Field + ) + .Iter(); + + public IEnumerable FilterByBoolField(bool BoolField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 15, + BoolField, + global::PublicTable.BSATN.BoolField + ) + .Iter(); + + public IEnumerable FilterByStringField(string StringField) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 18, + StringField, + global::PublicTable.BSATN.StringField + ) + .Iter(); + + public IEnumerable FilterByIdentityField( + SpacetimeDB.Identity IdentityField + ) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 19, + IdentityField, + global::PublicTable.BSATN.IdentityField + ) + .Iter(); + + public IEnumerable FilterByAddressField( + SpacetimeDB.Address AddressField + ) => + SpacetimeDB + .Internal.ITableView.ColEq.Where( + 20, + AddressField, + global::PublicTable.BSATN.AddressField + ) + .Iter(); + } + + public readonly struct SendMessageTimer + : SpacetimeDB.Internal.ITableView + { + static global::Timers.SendMessageTimer SpacetimeDB.Internal.ITableView< + SendMessageTimer, + global::Timers.SendMessageTimer + >.ReadGenFields(System.IO.BinaryReader reader, global::Timers.SendMessageTimer row) + { + if (row.ScheduledId == default) + { + row.ScheduledId = global::Timers.SendMessageTimer.BSATN.ScheduledId.Read( + reader + ); + } + return row; + } + + public IEnumerable Iter() => + SpacetimeDB.Internal.ITableView< + SendMessageTimer, + global::Timers.SendMessageTimer + >.Iter(); + + public IEnumerable Query( + System.Linq.Expressions.Expression< + Func + > predicate + ) => + SpacetimeDB.Internal.ITableView< + SendMessageTimer, + global::Timers.SendMessageTimer + >.Query(predicate); + + public global::Timers.SendMessageTimer Insert(global::Timers.SendMessageTimer row) => + SpacetimeDB.Internal.ITableView< + SendMessageTimer, + global::Timers.SendMessageTimer + >.Insert(row); + + public IEnumerable FilterByText(string Text) => + SpacetimeDB + .Internal.ITableView< + SendMessageTimer, + global::Timers.SendMessageTimer + >.ColEq.Where(0, Text, global::Timers.SendMessageTimer.BSATN.Text) + .Iter(); + + public IEnumerable FilterByScheduledId( + ulong ScheduledId + ) => + SpacetimeDB + .Internal.ITableView< + SendMessageTimer, + global::Timers.SendMessageTimer + >.ColEq.Where(1, ScheduledId, global::Timers.SendMessageTimer.BSATN.ScheduledId) + .Iter(); + + public global::Timers.SendMessageTimer? FindByScheduledId(ulong ScheduledId) => + FilterByScheduledId(ScheduledId) + .Cast() + .SingleOrDefault(); + + public bool DeleteByScheduledId(ulong ScheduledId) => + SpacetimeDB + .Internal.ITableView< + SendMessageTimer, + global::Timers.SendMessageTimer + >.ColEq.Where(1, ScheduledId, global::Timers.SendMessageTimer.BSATN.ScheduledId) + .Delete(); + + public bool UpdateByScheduledId( + ulong ScheduledId, + global::Timers.SendMessageTimer @this + ) => + SpacetimeDB + .Internal.ITableView< + SendMessageTimer, + global::Timers.SendMessageTimer + >.ColEq.Where(1, ScheduledId, global::Timers.SendMessageTimer.BSATN.ScheduledId) + .Update(@this); + } + } + + public sealed class Local + { + public Internal.TableHandles.PrivateTable PrivateTable => new(); + public Internal.TableHandles.PublicTable PublicTable => new(); + public Internal.TableHandles.SendMessageTimer SendMessageTimer => new(); + } +} + static class ModuleRegistration { class Init : SpacetimeDB.Internal.IReducer @@ -14,9 +381,9 @@ public SpacetimeDB.Internal.ReducerDef MakeReducerDef( SpacetimeDB.BSATN.ITypeRegistrar registrar ) => new("__init__", []); - public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) + public void Invoke(BinaryReader reader, SpacetimeDB.Internal.IReducerContext ctx) { - Timers.Init(ctx); + Timers.Init((SpacetimeDB.ReducerContext)ctx); } } @@ -28,9 +395,9 @@ public SpacetimeDB.Internal.ReducerDef MakeReducerDef( SpacetimeDB.BSATN.ITypeRegistrar registrar ) => new("InsertData", [new(nameof(data), data.GetAlgebraicType(registrar))]); - public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) + public void Invoke(BinaryReader reader, SpacetimeDB.Internal.IReducerContext ctx) { - Reducers.InsertData(data.Read(reader)); + Reducers.InsertData((SpacetimeDB.ReducerContext)ctx, data.Read(reader)); } } @@ -46,9 +413,12 @@ SpacetimeDB.BSATN.ITypeRegistrar registrar [new(nameof(data), data.GetAlgebraicType(registrar))] ); - public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) + public void Invoke(BinaryReader reader, SpacetimeDB.Internal.IReducerContext ctx) { - Test.NestingNamespaces.AndClasses.InsertData2(ctx, data.Read(reader)); + Test.NestingNamespaces.AndClasses.InsertData2( + (SpacetimeDB.ReducerContext)ctx, + data.Read(reader) + ); } } @@ -60,9 +430,9 @@ public SpacetimeDB.Internal.ReducerDef MakeReducerDef( SpacetimeDB.BSATN.ITypeRegistrar registrar ) => new("ScheduleImmediate", [new(nameof(data), data.GetAlgebraicType(registrar))]); - public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) + public void Invoke(BinaryReader reader, SpacetimeDB.Internal.IReducerContext ctx) { - Reducers.ScheduleImmediate(data.Read(reader)); + Reducers.ScheduleImmediate((SpacetimeDB.ReducerContext)ctx, data.Read(reader)); } } @@ -74,9 +444,9 @@ public SpacetimeDB.Internal.ReducerDef MakeReducerDef( SpacetimeDB.BSATN.ITypeRegistrar registrar ) => new("SendScheduledMessage", [new(nameof(arg), arg.GetAlgebraicType(registrar))]); - public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) + public void Invoke(BinaryReader reader, SpacetimeDB.Internal.IReducerContext ctx) { - Timers.SendScheduledMessage(arg.Read(reader)); + Timers.SendScheduledMessage((SpacetimeDB.ReducerContext)ctx, arg.Read(reader)); } } @@ -93,14 +463,19 @@ public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) #endif public static void Main() { + SpacetimeDB.Internal.Module.SetReducerContextConstructor( + (identity, address, random, time) => + new SpacetimeDB.ReducerContext(identity, address, random, time) + ); + SpacetimeDB.Internal.Module.RegisterReducer(); SpacetimeDB.Internal.Module.RegisterReducer(); SpacetimeDB.Internal.Module.RegisterReducer(); SpacetimeDB.Internal.Module.RegisterReducer(); SpacetimeDB.Internal.Module.RegisterReducer(); - SpacetimeDB.Internal.Module.RegisterTable(); - SpacetimeDB.Internal.Module.RegisterTable(); - SpacetimeDB.Internal.Module.RegisterTable(); + SpacetimeDB.Internal.Module.RegisterTable(); + SpacetimeDB.Internal.Module.RegisterTable(); + SpacetimeDB.Internal.Module.RegisterTable(); } // Exports only work from the main assembly, so we need to generate forwarding methods. diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#PrivateTable.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#PrivateTable.verified.cs index d3bfe384895..915464b0ef2 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#PrivateTable.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#PrivateTable.verified.cs @@ -26,37 +26,30 @@ SpacetimeDB.BSATN.ITypeRegistrar registrar )); } - void SpacetimeDB.Internal.ITable.ReadGenFields(System.IO.BinaryReader reader) { } - - static SpacetimeDB.Internal.TableDesc SpacetimeDB.Internal.ITable.MakeTableDesc( + static IEnumerable SpacetimeDB.Internal.ITable.MakeTableDesc( SpacetimeDB.BSATN.ITypeRegistrar registrar ) => - new( + [ new( - TableName: nameof(PrivateTable), - Columns: [], - Indexes: [], - Constraints: [], - Sequences: [], - // "system" | "user" - TableType: "user", - // "public" | "private" - TableAccess: "private", - Scheduled: null + new( + TableName: nameof(PrivateTable), + Columns: [], + Indexes: [], + Constraints: [], + Sequences: [], + // "system" | "user" + TableType: "user", + // "public" | "private" + TableAccess: "private", + Scheduled: null + ), + (uint) + ( + (SpacetimeDB.BSATN.AlgebraicType.Ref)new BSATN().GetAlgebraicType(registrar) + ).Ref_ ), - (uint) - ((SpacetimeDB.BSATN.AlgebraicType.Ref)new BSATN().GetAlgebraicType(registrar)).Ref_ - ); + ]; static SpacetimeDB.Internal.Filter SpacetimeDB.Internal.ITable.CreateFilter() => new([]); - - public static IEnumerable Iter() => - SpacetimeDB.Internal.ITable.Iter(); - - public static IEnumerable Query( - System.Linq.Expressions.Expression> predicate - ) => SpacetimeDB.Internal.ITable.Query(predicate); - - public void Insert() => SpacetimeDB.Internal.ITable.Insert(this); } // PrivateTable diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#PublicTable.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#PublicTable.verified.cs index 92f7325293f..7a1e03c1569 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#PublicTable.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#PublicTable.verified.cs @@ -188,91 +188,88 @@ SpacetimeDB.BSATN.ITypeRegistrar registrar )); } - void SpacetimeDB.Internal.ITable.ReadGenFields(System.IO.BinaryReader reader) - { - if (Id == default) - { - Id = BSATN.Id.Read(reader); - } - } - - static SpacetimeDB.Internal.TableDesc SpacetimeDB.Internal.ITable.MakeTableDesc( + static IEnumerable SpacetimeDB.Internal.ITable.MakeTableDesc( SpacetimeDB.BSATN.ITypeRegistrar registrar ) => - new( + [ new( - TableName: nameof(PublicTable), - Columns: - [ - new(nameof(Id), BSATN.Id.GetAlgebraicType(registrar)), - new(nameof(ByteField), BSATN.ByteField.GetAlgebraicType(registrar)), - new(nameof(UshortField), BSATN.UshortField.GetAlgebraicType(registrar)), - new(nameof(UintField), BSATN.UintField.GetAlgebraicType(registrar)), - new(nameof(UlongField), BSATN.UlongField.GetAlgebraicType(registrar)), - new(nameof(UInt128Field), BSATN.UInt128Field.GetAlgebraicType(registrar)), - new(nameof(U128Field), BSATN.U128Field.GetAlgebraicType(registrar)), - new(nameof(U256Field), BSATN.U256Field.GetAlgebraicType(registrar)), - new(nameof(SbyteField), BSATN.SbyteField.GetAlgebraicType(registrar)), - new(nameof(ShortField), BSATN.ShortField.GetAlgebraicType(registrar)), - new(nameof(IntField), BSATN.IntField.GetAlgebraicType(registrar)), - new(nameof(LongField), BSATN.LongField.GetAlgebraicType(registrar)), - new(nameof(Int128Field), BSATN.Int128Field.GetAlgebraicType(registrar)), - new(nameof(I128Field), BSATN.I128Field.GetAlgebraicType(registrar)), - new(nameof(I256Field), BSATN.I256Field.GetAlgebraicType(registrar)), - new(nameof(BoolField), BSATN.BoolField.GetAlgebraicType(registrar)), - new(nameof(FloatField), BSATN.FloatField.GetAlgebraicType(registrar)), - new(nameof(DoubleField), BSATN.DoubleField.GetAlgebraicType(registrar)), - new(nameof(StringField), BSATN.StringField.GetAlgebraicType(registrar)), - new(nameof(IdentityField), BSATN.IdentityField.GetAlgebraicType(registrar)), - new(nameof(AddressField), BSATN.AddressField.GetAlgebraicType(registrar)), - new( - nameof(CustomStructField), - BSATN.CustomStructField.GetAlgebraicType(registrar) - ), - new( - nameof(CustomClassField), - BSATN.CustomClassField.GetAlgebraicType(registrar) - ), - new(nameof(CustomEnumField), BSATN.CustomEnumField.GetAlgebraicType(registrar)), - new( - nameof(CustomTaggedEnumField), - BSATN.CustomTaggedEnumField.GetAlgebraicType(registrar) - ), - new(nameof(ListField), BSATN.ListField.GetAlgebraicType(registrar)), - new(nameof(DictionaryField), BSATN.DictionaryField.GetAlgebraicType(registrar)), - new( - nameof(NullableValueField), - BSATN.NullableValueField.GetAlgebraicType(registrar) - ), - new( - nameof(NullableReferenceField), - BSATN.NullableReferenceField.GetAlgebraicType(registrar) - ), - new( - nameof(ComplexNestedField), - BSATN.ComplexNestedField.GetAlgebraicType(registrar) - ) - ], - Indexes: [], - Constraints: - [ - new( - nameof(PublicTable), - 0, - nameof(Id), - SpacetimeDB.ColumnAttrs.PrimaryKeyIdentity - ) - ], - Sequences: [], - // "system" | "user" - TableType: "user", - // "public" | "private" - TableAccess: "private", - Scheduled: null + new( + TableName: nameof(PublicTable), + Columns: + [ + new(nameof(Id), BSATN.Id.GetAlgebraicType(registrar)), + new(nameof(ByteField), BSATN.ByteField.GetAlgebraicType(registrar)), + new(nameof(UshortField), BSATN.UshortField.GetAlgebraicType(registrar)), + new(nameof(UintField), BSATN.UintField.GetAlgebraicType(registrar)), + new(nameof(UlongField), BSATN.UlongField.GetAlgebraicType(registrar)), + new(nameof(UInt128Field), BSATN.UInt128Field.GetAlgebraicType(registrar)), + new(nameof(U128Field), BSATN.U128Field.GetAlgebraicType(registrar)), + new(nameof(U256Field), BSATN.U256Field.GetAlgebraicType(registrar)), + new(nameof(SbyteField), BSATN.SbyteField.GetAlgebraicType(registrar)), + new(nameof(ShortField), BSATN.ShortField.GetAlgebraicType(registrar)), + new(nameof(IntField), BSATN.IntField.GetAlgebraicType(registrar)), + new(nameof(LongField), BSATN.LongField.GetAlgebraicType(registrar)), + new(nameof(Int128Field), BSATN.Int128Field.GetAlgebraicType(registrar)), + new(nameof(I128Field), BSATN.I128Field.GetAlgebraicType(registrar)), + new(nameof(I256Field), BSATN.I256Field.GetAlgebraicType(registrar)), + new(nameof(BoolField), BSATN.BoolField.GetAlgebraicType(registrar)), + new(nameof(FloatField), BSATN.FloatField.GetAlgebraicType(registrar)), + new(nameof(DoubleField), BSATN.DoubleField.GetAlgebraicType(registrar)), + new(nameof(StringField), BSATN.StringField.GetAlgebraicType(registrar)), + new(nameof(IdentityField), BSATN.IdentityField.GetAlgebraicType(registrar)), + new(nameof(AddressField), BSATN.AddressField.GetAlgebraicType(registrar)), + new( + nameof(CustomStructField), + BSATN.CustomStructField.GetAlgebraicType(registrar) + ), + new( + nameof(CustomClassField), + BSATN.CustomClassField.GetAlgebraicType(registrar) + ), + new( + nameof(CustomEnumField), + BSATN.CustomEnumField.GetAlgebraicType(registrar) + ), + new( + nameof(CustomTaggedEnumField), + BSATN.CustomTaggedEnumField.GetAlgebraicType(registrar) + ), + new(nameof(ListField), BSATN.ListField.GetAlgebraicType(registrar)), + new( + nameof(DictionaryField), + BSATN.DictionaryField.GetAlgebraicType(registrar) + ), + new( + nameof(NullableValueField), + BSATN.NullableValueField.GetAlgebraicType(registrar) + ), + new( + nameof(NullableReferenceField), + BSATN.NullableReferenceField.GetAlgebraicType(registrar) + ), + new( + nameof(ComplexNestedField), + BSATN.ComplexNestedField.GetAlgebraicType(registrar) + ) + ], + Indexes: [], + Constraints: + [ + new(nameof(PublicTable), 0, nameof(Id), (SpacetimeDB.ColumnAttrs)15) + ], + Sequences: [], + // "system" | "user" + TableType: "user", + // "public" | "private" + TableAccess: "private", + Scheduled: null + ), + (uint) + ( + (SpacetimeDB.BSATN.AlgebraicType.Ref)new BSATN().GetAlgebraicType(registrar) + ).Ref_ ), - (uint) - ((SpacetimeDB.BSATN.AlgebraicType.Ref)new BSATN().GetAlgebraicType(registrar)).Ref_ - ); + ]; static SpacetimeDB.Internal.Filter SpacetimeDB.Internal.ITable.CreateFilter() => new( @@ -357,99 +354,4 @@ static SpacetimeDB.Internal.Filter SpacetimeDB.Internal.ITable.Crea ) ] ); - - public static IEnumerable Iter() => - SpacetimeDB.Internal.ITable.Iter(); - - public static IEnumerable Query( - System.Linq.Expressions.Expression> predicate - ) => SpacetimeDB.Internal.ITable.Query(predicate); - - public void Insert() => SpacetimeDB.Internal.ITable.Insert(this); - - public static IEnumerable FilterById(int Id) => - SpacetimeDB.Internal.ITable.ColEq.Where(0, Id, BSATN.Id).Iter(); - - public static PublicTable? FindById(int Id) => - FilterById(Id).Cast().SingleOrDefault(); - - public static bool DeleteById(int Id) => - SpacetimeDB.Internal.ITable.ColEq.Where(0, Id, BSATN.Id).Delete(); - - public static bool UpdateById(int Id, PublicTable @this) => - SpacetimeDB.Internal.ITable.ColEq.Where(0, Id, BSATN.Id).Update(@this); - - public static IEnumerable FilterByByteField(byte ByteField) => - SpacetimeDB.Internal.ITable.ColEq.Where(1, ByteField, BSATN.ByteField).Iter(); - - public static IEnumerable FilterByUshortField(ushort UshortField) => - SpacetimeDB - .Internal.ITable.ColEq.Where(2, UshortField, BSATN.UshortField) - .Iter(); - - public static IEnumerable FilterByUintField(uint UintField) => - SpacetimeDB.Internal.ITable.ColEq.Where(3, UintField, BSATN.UintField).Iter(); - - public static IEnumerable FilterByUlongField(ulong UlongField) => - SpacetimeDB - .Internal.ITable.ColEq.Where(4, UlongField, BSATN.UlongField) - .Iter(); - - public static IEnumerable FilterByUInt128Field(System.UInt128 UInt128Field) => - SpacetimeDB - .Internal.ITable.ColEq.Where(5, UInt128Field, BSATN.UInt128Field) - .Iter(); - - public static IEnumerable FilterByU128Field(SpacetimeDB.U128 U128Field) => - SpacetimeDB.Internal.ITable.ColEq.Where(6, U128Field, BSATN.U128Field).Iter(); - - public static IEnumerable FilterByU256Field(SpacetimeDB.U256 U256Field) => - SpacetimeDB.Internal.ITable.ColEq.Where(7, U256Field, BSATN.U256Field).Iter(); - - public static IEnumerable FilterBySbyteField(sbyte SbyteField) => - SpacetimeDB - .Internal.ITable.ColEq.Where(8, SbyteField, BSATN.SbyteField) - .Iter(); - - public static IEnumerable FilterByShortField(short ShortField) => - SpacetimeDB - .Internal.ITable.ColEq.Where(9, ShortField, BSATN.ShortField) - .Iter(); - - public static IEnumerable FilterByIntField(int IntField) => - SpacetimeDB.Internal.ITable.ColEq.Where(10, IntField, BSATN.IntField).Iter(); - - public static IEnumerable FilterByLongField(long LongField) => - SpacetimeDB.Internal.ITable.ColEq.Where(11, LongField, BSATN.LongField).Iter(); - - public static IEnumerable FilterByInt128Field(System.Int128 Int128Field) => - SpacetimeDB - .Internal.ITable.ColEq.Where(12, Int128Field, BSATN.Int128Field) - .Iter(); - - public static IEnumerable FilterByI128Field(SpacetimeDB.I128 I128Field) => - SpacetimeDB.Internal.ITable.ColEq.Where(13, I128Field, BSATN.I128Field).Iter(); - - public static IEnumerable FilterByI256Field(SpacetimeDB.I256 I256Field) => - SpacetimeDB.Internal.ITable.ColEq.Where(14, I256Field, BSATN.I256Field).Iter(); - - public static IEnumerable FilterByBoolField(bool BoolField) => - SpacetimeDB.Internal.ITable.ColEq.Where(15, BoolField, BSATN.BoolField).Iter(); - - public static IEnumerable FilterByStringField(string StringField) => - SpacetimeDB - .Internal.ITable.ColEq.Where(18, StringField, BSATN.StringField) - .Iter(); - - public static IEnumerable FilterByIdentityField( - SpacetimeDB.Identity IdentityField - ) => - SpacetimeDB - .Internal.ITable.ColEq.Where(19, IdentityField, BSATN.IdentityField) - .Iter(); - - public static IEnumerable FilterByAddressField(SpacetimeDB.Address AddressField) => - SpacetimeDB - .Internal.ITable.ColEq.Where(20, AddressField, BSATN.AddressField) - .Iter(); } // PublicTable diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#Timers.SendMessageTimer.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#Timers.SendMessageTimer.verified.cs index 646eca5b2a1..26f8b11a88c 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#Timers.SendMessageTimer.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#Timers.SendMessageTimer.verified.cs @@ -53,50 +53,43 @@ SpacetimeDB.BSATN.ITypeRegistrar registrar public ulong ScheduledId; public SpacetimeDB.ScheduleAt ScheduledAt; - void SpacetimeDB.Internal.ITable.ReadGenFields( - System.IO.BinaryReader reader - ) - { - if (ScheduledId == default) - { - ScheduledId = BSATN.ScheduledId.Read(reader); - } - } - - static SpacetimeDB.Internal.TableDesc SpacetimeDB.Internal.ITable.MakeTableDesc( + static IEnumerable SpacetimeDB.Internal.ITable.MakeTableDesc( SpacetimeDB.BSATN.ITypeRegistrar registrar ) => - new( + [ new( - TableName: nameof(SendMessageTimer), - Columns: - [ - new(nameof(Text), BSATN.Text.GetAlgebraicType(registrar)), - new(nameof(ScheduledId), BSATN.ScheduledId.GetAlgebraicType(registrar)), - new(nameof(ScheduledAt), BSATN.ScheduledAt.GetAlgebraicType(registrar)) - ], - Indexes: [], - Constraints: - [ - new( - nameof(SendMessageTimer), - 1, - nameof(ScheduledId), - SpacetimeDB.ColumnAttrs.PrimaryKeyIdentity - ) - ], - Sequences: [], - // "system" | "user" - TableType: "user", - // "public" | "private" - TableAccess: "private", - Scheduled: nameof(SendScheduledMessage) + new( + TableName: nameof(SendMessageTimer), + Columns: + [ + new(nameof(Text), BSATN.Text.GetAlgebraicType(registrar)), + new(nameof(ScheduledId), BSATN.ScheduledId.GetAlgebraicType(registrar)), + new(nameof(ScheduledAt), BSATN.ScheduledAt.GetAlgebraicType(registrar)) + ], + Indexes: [], + Constraints: + [ + new( + nameof(SendMessageTimer), + 1, + nameof(ScheduledId), + (SpacetimeDB.ColumnAttrs)15 + ) + ], + Sequences: [], + // "system" | "user" + TableType: "user", + // "public" | "private" + TableAccess: "private", + Scheduled: nameof(SendScheduledMessage) + ), + (uint) + ( + (SpacetimeDB.BSATN.AlgebraicType.Ref) + new BSATN().GetAlgebraicType(registrar) + ).Ref_ ), - (uint) - ( - (SpacetimeDB.BSATN.AlgebraicType.Ref)new BSATN().GetAlgebraicType(registrar) - ).Ref_ - ); + ]; static SpacetimeDB.Internal.Filter SpacetimeDB.Internal.ITable.CreateFilter() => new( @@ -109,35 +102,5 @@ static SpacetimeDB.Internal.Filter SpacetimeDB.Internal.ITable ) ] ); - - public static IEnumerable Iter() => - SpacetimeDB.Internal.ITable.Iter(); - - public static IEnumerable Query( - System.Linq.Expressions.Expression> predicate - ) => SpacetimeDB.Internal.ITable.Query(predicate); - - public void Insert() => SpacetimeDB.Internal.ITable.Insert(this); - - public static IEnumerable FilterByText(string Text) => - SpacetimeDB.Internal.ITable.ColEq.Where(0, Text, BSATN.Text).Iter(); - - public static IEnumerable FilterByScheduledId(ulong ScheduledId) => - SpacetimeDB - .Internal.ITable.ColEq.Where(1, ScheduledId, BSATN.ScheduledId) - .Iter(); - - public static SendMessageTimer? FindByScheduledId(ulong ScheduledId) => - FilterByScheduledId(ScheduledId).Cast().SingleOrDefault(); - - public static bool DeleteByScheduledId(ulong ScheduledId) => - SpacetimeDB - .Internal.ITable.ColEq.Where(1, ScheduledId, BSATN.ScheduledId) - .Delete(); - - public static bool UpdateByScheduledId(ulong ScheduledId, SendMessageTimer @this) => - SpacetimeDB - .Internal.ITable.ColEq.Where(1, ScheduledId, BSATN.ScheduledId) - .Update(@this); } // SendMessageTimer } // Timers diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Type#CustomClass.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Type#CustomClass.verified.cs index f1c7727c41d..1ac96f3d0dd 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Type#CustomClass.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Type#CustomClass.verified.cs @@ -2,7 +2,7 @@ // #nullable enable -partial struct CustomClass : SpacetimeDB.BSATN.IStructuralReadWrite +partial class CustomClass : SpacetimeDB.BSATN.IStructuralReadWrite { public void ReadFields(System.IO.BinaryReader reader) { diff --git a/crates/bindings-csharp/Codegen/Module.cs b/crates/bindings-csharp/Codegen/Module.cs index 5127714b891..264e0380d94 100644 --- a/crates/bindings-csharp/Codegen/Module.cs +++ b/crates/bindings-csharp/Codegen/Module.cs @@ -2,15 +2,18 @@ namespace SpacetimeDB.Codegen; using System.Collections.Immutable; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Utils; record ColumnDeclaration : MemberDeclaration { - public readonly ColumnAttrs Attrs; + public readonly EquatableArray<(string? table, ColumnAttrs mask)> Attrs; public readonly bool IsEquatable; + public readonly string FullTableName; public ColumnDeclaration( + string tableName, string name, string type, string typeInfo, @@ -19,18 +22,36 @@ bool isEquatable ) : base(name, type, typeInfo) { - Attrs = attrs; + Attrs = new(ImmutableArray.Create((default(string), attrs))); IsEquatable = isEquatable; + FullTableName = tableName; } - public ColumnDeclaration(IFieldSymbol field) + public ColumnDeclaration(string tableName, IFieldSymbol field) : base(field) { - Attrs = field - .GetAttributes() - .Where(a => a.AttributeClass?.ToString() == typeof(ColumnAttribute).FullName) - .Select(a => a.ParseAs().Type) - .SingleOrDefault(); + FullTableName = tableName; + + Attrs = new( + field + .GetAttributes() + .Select(a => + ( + table: a.NamedArguments.FirstOrDefault(a => a.Key == "Table").Value.Value + as string, + attr: a.AttributeClass?.ToString() switch + { + "SpacetimeDB.AutoIncAttribute" => ColumnAttrs.AutoInc, + "SpacetimeDB.PrimaryKeyAttribute" => ColumnAttrs.PrimaryKey, + "SpacetimeDB.UniqueAttribute" => ColumnAttrs.Unique, + "SpacetimeDB.IndexedAttribute" => ColumnAttrs.Indexed, + _ => ColumnAttrs.UnSet, + } + ) + ) + .Where(a => a.attr != ColumnAttrs.UnSet) + .ToImmutableArray() + ); var type = field.Type; @@ -54,7 +75,9 @@ or SpecialType.System_Int64 _ => false, }; - if (Attrs.HasFlag(ColumnAttrs.AutoInc) && !isInteger) + var attrs = Attrs.Aggregate(ColumnAttrs.UnSet, (xs, x) => xs | x.mask); + + if (attrs.HasFlag(ColumnAttrs.AutoInc) && !isInteger) { throw new Exception( $"{type} {Name} is not valid for AutoInc or Identity as it's not an integer." @@ -75,7 +98,7 @@ or SpecialType.System_Int64 ) && type.NullableAnnotation != NullableAnnotation.Annotated; - if (Attrs.HasFlag(ColumnAttrs.Unique) && !IsEquatable) + if (attrs.HasFlag(ColumnAttrs.Unique) && !IsEquatable) { throw new Exception( $"{type} {Name} is not valid for Identity, PrimaryKey or PrimaryKeyAuto as it's not an equatable primitive." @@ -83,6 +106,11 @@ or SpecialType.System_Int64 } } + public ColumnAttrs GetAttrs(string tableName) => + Attrs + .Where(x => x.table == null || x.table == tableName) + .Aggregate(ColumnAttrs.UnSet, (xs, x) => xs | x.mask); + // For the `TableDesc` constructor. public string GenerateColumnDef() => $"new (nameof({Name}), BSATN.{Name}.GetAlgebraicType(registrar))"; @@ -92,22 +120,52 @@ public string GenerateFilterEntry() => $"new (nameof({Name}), (w, v) => BSATN.{Name}.Write(w, ({Type}) v!))"; } -record TableDeclaration : BaseTypeDeclaration +record TableView { + public readonly string Name; public readonly bool IsPublic; public readonly string? Scheduled; - private static readonly ColumnDeclaration[] ScheduledColumns = - [ - new("ScheduledId", "ulong", "SpacetimeDB.BSATN.U64", ColumnAttrs.PrimaryKeyAuto, true), - new( - "ScheduledAt", - "SpacetimeDB.ScheduleAt", - "SpacetimeDB.ScheduleAt.BSATN", - ColumnAttrs.UnSet, - false - ), - ]; + public TableView(TableDeclaration table, AttributeData data) + { + Name = + data.NamedArguments.FirstOrDefault(x => x.Key == "Name").Value.Value as string + ?? table.ShortName; + + IsPublic = data.NamedArguments.Any(pair => pair is { Key: "Public", Value.Value: true }); + + Scheduled = data + .NamedArguments.Where(pair => pair.Key == "Scheduled") + .Select(pair => (string?)pair.Value.Value) + .SingleOrDefault(); + } +} + +record TableDeclaration : BaseTypeDeclaration +{ + public readonly Accessibility Visibility; + public readonly string? Scheduled; + public readonly EquatableArray Views; + + private static ColumnDeclaration[] ScheduledColumns(string tableName) => + [ + new( + tableName, + "ScheduledId", + "ulong", + "SpacetimeDB.BSATN.U64", + ColumnAttrs.PrimaryKeyAuto, + true + ), + new( + tableName, + "ScheduledAt", + "SpacetimeDB.ScheduleAt", + "SpacetimeDB.ScheduleAt.BSATN", + ColumnAttrs.UnSet, + false + ), + ]; public TableDeclaration(GeneratorAttributeSyntaxContext context) : base(context) @@ -117,21 +175,125 @@ public TableDeclaration(GeneratorAttributeSyntaxContext context) throw new InvalidOperationException("Tagged enums cannot be tables."); } - var attr = context.Attributes.Single().ParseAs(); + Visibility = context.TargetSymbol.DeclaredAccessibility; - IsPublic = attr.Public; - Scheduled = attr.Scheduled; + var container = context.TargetSymbol; + while (container != null) + { + switch (Visibility) + { + case Accessibility.ProtectedAndInternal: + case Accessibility.NotApplicable: + case Accessibility.Internal: + case Accessibility.Public: + break; + default: + throw new Exception( + "Table row type visibility must be public or internal, including containing types." + ); + } + ; - if (Scheduled is not null) + container = container.ContainingType; + } + + Views = new(context.Attributes.Select(a => new TableView(this, a)).ToImmutableArray()); + + var schedules = Views.Select(t => t.Scheduled).Distinct(); + if (schedules.Count() != 1) + { + throw new Exception( + "When using multiple [Table] attributes with schedule, all [Table] must have the same schedule." + ); + } + + Scheduled = schedules.First(); + if (Scheduled != null) { // For scheduled tables, we append extra fields early in the pipeline, // both to the type itself and to the BSATN information, as if they // were part of the original declaration. - Members = new(Members.Concat(ScheduledColumns).ToImmutableArray()); + Members = new(Members.Concat(ScheduledColumns(FullName)).ToImmutableArray()); } } - protected override ColumnDeclaration ConvertMember(IFieldSymbol field) => new(field); + protected override ColumnDeclaration ConvertMember(IFieldSymbol field) => new(FullName, field); + + public IEnumerable GenerateViewFilters(string viewName, string iTable) + { + foreach ( + var (f, i) in Members + .Select((field, i) => (field, i)) + .Where(pair => pair.field.IsEquatable) + ) + { + var globalName = $"global::{FullName}"; + var colEqWhere = $"{iTable}.ColEq.Where({i}, {f.Name}, {globalName}.BSATN.{f.Name})"; + + yield return $""" + public IEnumerable<{globalName}> FilterBy{f.Name}({f.Type} {f.Name}) => + {colEqWhere}.Iter(); + """; + + if (f.GetAttrs(viewName).HasFlag(ColumnAttrs.Unique)) + { + yield return $""" + public {globalName}? FindBy{f.Name}({f.Type} {f.Name}) => + FilterBy{f.Name}({f.Name}) + .Cast<{globalName}?>() + .SingleOrDefault(); + + public bool DeleteBy{f.Name}({f.Type} {f.Name}) => + {colEqWhere}.Delete(); + + public bool UpdateBy{f.Name}({f.Type} {f.Name}, {globalName} @this) => + {colEqWhere}.Update(@this); + """; + } + } + } + + public record struct View(string viewName, string tableName, string view, string getter); + + public IEnumerable GenerateViews() + { + foreach (var v in Views) + { + var autoIncFields = Members + .Where(f => f.GetAttrs(v.Name).HasFlag(ColumnAttrs.AutoInc)) + .Select(f => f.Name); + + var globalName = $"global::{FullName}"; + var iTable = $"SpacetimeDB.Internal.ITableView<{v.Name}, {globalName}>"; + yield return new( + v.Name, + globalName, + $$""" + {{SyntaxFacts.GetText(Visibility)}} readonly struct {{v.Name}} : {{iTable}} { + static {{globalName}} {{iTable}}.ReadGenFields(System.IO.BinaryReader reader, {{globalName}} row) { + {{string.Join( + "\n", + autoIncFields.Select(name => + $$""" + if (row.{{name}} == default) + { + row.{{name}} = {{globalName}}.BSATN.{{name}}.Read(reader); + } + """ + ) + )}} + return row; + } + public IEnumerable<{{globalName}}> Iter() => {{iTable}}.Iter(); + public IEnumerable<{{globalName}}> Query(System.Linq.Expressions.Expression> predicate) => {{iTable}}.Query(predicate); + public {{globalName}} Insert({{globalName}} row) => {{iTable}}.Insert(row); + {{string.Join("\n", GenerateViewFilters(v.Name, iTable))}} + } + """, + $"{SyntaxFacts.GetText(Visibility)} Internal.TableHandles.{v.Name} {v.Name} => new();" + ); + } + } public override Scope.Extensions ToExtensions() { @@ -155,10 +317,6 @@ public override Scope.Extensions ToExtensions() ); } - var autoIncFields = Members - .Where(f => f.Attrs.HasFlag(ColumnAttrs.AutoInc)) - .Select(f => f.Name); - var iTable = $"SpacetimeDB.Internal.ITable<{ShortName}>"; // ITable inherits IStructuralReadWrite, so we can replace the base type instead of appending another one. @@ -167,21 +325,9 @@ public override Scope.Extensions ToExtensions() extensions.Contents.Append( $$""" - void {{iTable}}.ReadGenFields(System.IO.BinaryReader reader) { - {{string.Join( - "\n", - autoIncFields.Select(name => - $$""" - if ({{name}} == default) - { - {{name}} = BSATN.{{name}}.Read(reader); - } - """ - ) - )}} - } - - static SpacetimeDB.Internal.TableDesc {{iTable}}.MakeTableDesc(SpacetimeDB.BSATN.ITypeRegistrar registrar) => new ( + static IEnumerable {{iTable}}.MakeTableDesc(SpacetimeDB.BSATN.ITypeRegistrar registrar) => [ + {{string.Join("\n", Views.Select(v => $$""" + new ( new ( TableName: nameof({{ShortName}}), Columns: [ @@ -193,15 +339,15 @@ public override Scope.Extensions ToExtensions() ",\n", Members // Important: the position must be stored here, before filtering. - .Select((col, pos) => (col, pos)) - .Where(pair => pair.col.Attrs != ColumnAttrs.UnSet) - .Select(pair => + .Select((col, pos) => (col, pos, attr: col.GetAttrs(v.Name))) + .Where(tuple => tuple.attr != ColumnAttrs.UnSet) + .Select(tuple => $$""" new ( nameof({{ShortName}}), - {{pair.pos}}, - nameof({{pair.col.Name}}), - SpacetimeDB.ColumnAttrs.{{pair.col.Attrs}} + {{tuple.pos}}, + nameof({{tuple.col.Name}}), + (SpacetimeDB.ColumnAttrs){{(int)tuple.attr}} ) """ ) @@ -211,77 +357,30 @@ public override Scope.Extensions ToExtensions() // "system" | "user" TableType: "user", // "public" | "private" - TableAccess: "{{(IsPublic ? "public" : "private")}}", - Scheduled: {{(Scheduled is not null ? $"nameof({Scheduled})" : "null")}} + TableAccess: "{{(v.IsPublic ? "public" : "private")}}", + Scheduled: {{(v.Scheduled is not null ? $"nameof({v.Scheduled})" : "null")}} ), (uint) ((SpacetimeDB.BSATN.AlgebraicType.Ref) new BSATN().GetAlgebraicType(registrar)).Ref_ - ); + ), + """))}} + ]; static SpacetimeDB.Internal.Filter {{iTable}}.CreateFilter() => new([ {{string.Join(",\n", Members.Select(f => f.GenerateFilterEntry()))}} ]); - - public static IEnumerable<{{ShortName}}> Iter() => {{iTable}}.Iter(); - public static IEnumerable<{{ShortName}}> Query(System.Linq.Expressions.Expression> predicate) => {{iTable}}.Query(predicate); - public void Insert() => {{iTable}}.Insert(this); """ ); - foreach ( - var (f, i) in Members - .Select((field, i) => (field, i)) - .Where(pair => pair.field.IsEquatable) - ) - { - var colEqWhere = $"{iTable}.ColEq.Where({i}, {f.Name}, BSATN.{f.Name})"; - - extensions.Contents.Append( - $""" - public static IEnumerable<{ShortName}> FilterBy{f.Name}({f.Type} {f.Name}) => - {colEqWhere}.Iter(); - """ - ); - - if (f.Attrs.HasFlag(ColumnAttrs.Unique)) - { - extensions.Contents.Append( - $""" - public static {ShortName}? FindBy{f.Name}({f.Type} {f.Name}) => - FilterBy{f.Name}({f.Name}) - .Cast<{ShortName}?>() - .SingleOrDefault(); - - public static bool DeleteBy{f.Name}({f.Type} {f.Name}) => - {colEqWhere}.Delete(); - - public static bool UpdateBy{f.Name}({f.Type} {f.Name}, {ShortName} @this) => - {colEqWhere}.Update(@this); - """ - ); - } - } - return extensions; } } -record ReducerParamDeclaration : MemberDeclaration -{ - public readonly bool IsContextArg; - - public ReducerParamDeclaration(IParameterSymbol param) - : base(param.Name, param.Type) - { - IsContextArg = Type == "SpacetimeDB.ReducerContext"; - } -} - record ReducerDeclaration { public readonly string Name; public readonly string ExportName; public readonly string FullName; - public readonly EquatableArray Args; + public readonly EquatableArray Args; public readonly Scope Scope; public ReducerDeclaration(GeneratorAttributeSyntaxContext context) @@ -294,36 +393,45 @@ public ReducerDeclaration(GeneratorAttributeSyntaxContext context) { throw new Exception($"Reducer {method} must return void"); } + if ( + method.Parameters.FirstOrDefault()?.Type is not INamedTypeSymbol namedType + || namedType.Name != "ReducerContext" + ) + { + throw new Exception( + $"Reducer {method} must have a first argument of type ReducerContext" + ); + } Name = method.Name; ExportName = attr.Name ?? Name; FullName = SymbolToName(method); Args = new( - method.Parameters.Select(p => new ReducerParamDeclaration(p)).ToImmutableArray() + method + .Parameters.Skip(1) + .Select(p => new MemberDeclaration(p.Name, p.Type)) + .ToImmutableArray() ); Scope = new Scope(methodSyntax.Parent as MemberDeclarationSyntax); } - private IEnumerable NonContextArgs => Args.Where(a => !a.IsContextArg); - public KeyValuePair GenerateClass() { + var args = string.Join( + ", ", + Args.Select(a => $"{a.Name}.Read(reader)").Prepend("(SpacetimeDB.ReducerContext)ctx") + ); var class_ = $$""" class {{Name}}: SpacetimeDB.Internal.IReducer { - {{MemberDeclaration.GenerateBsatnFields(Accessibility.Private, NonContextArgs)}} + {{MemberDeclaration.GenerateBsatnFields(Accessibility.Private, Args)}} public SpacetimeDB.Internal.ReducerDef MakeReducerDef(SpacetimeDB.BSATN.ITypeRegistrar registrar) => new ( "{{ExportName}}", - [{{MemberDeclaration.GenerateDefs(NonContextArgs)}}] + [{{MemberDeclaration.GenerateDefs(Args)}}] ); - public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) { - {{FullName}}( - {{string.Join( - ", ", - Args.Select(a => a.IsContextArg ? "ctx" : $"{a.Name}.Read(reader)") - )}} - ); + public void Invoke(BinaryReader reader, SpacetimeDB.Internal.IReducerContext ctx) { + {{FullName}}({{args}}); } } """; @@ -339,13 +447,13 @@ public Scope.Extensions GenerateSchedule() $$""" public static void VolatileNonatomicScheduleImmediate{{Name}}({{string.Join( ", ", - NonContextArgs.Select(a => $"{a.Type} {a.Name}") + Args.Select(a => $"{a.Type} {a.Name}") )}}) { using var stream = new MemoryStream(); using var writer = new BinaryWriter(stream); {{string.Join( "\n", - NonContextArgs.Select(a => $"new {a.TypeInfo}().Write(writer, {a.Name});") + Args.Select(a => $"new {a.TypeInfo}().Write(writer, {a.Name});") )}} SpacetimeDB.Internal.IReducer.VolatileNonatomicScheduleImmediate("{{ExportName}}", stream); } @@ -374,8 +482,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .WithTrackingName("SpacetimeDB.Table.GenerateExtensions") .RegisterSourceOutputs(context); - var tableNames = tables.Select((t, ct) => t.FullName).Collect(); - var reducers = context .SyntaxProvider.ForAttributeWithMetadataName( fullyQualifiedMetadataName: typeof(ReducerAttribute).FullName, @@ -394,18 +500,23 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .WithTrackingName("SpacetimeDB.Reducer.GenerateClass") .Collect(); + var tableViews = tables + .SelectMany((t, ct) => t.GenerateViews()) + .WithTrackingName("SpacetimeDB.Table.GenerateViews") + .Collect(); + context.RegisterSourceOutput( - tableNames.Combine(addReducers), + tableViews.Combine(addReducers), (context, tuple) => { // Sort tables and reducers by name to match Rust behaviour. // Not really important outside of testing, but for testing // it matters because we commit module-bindings // so they need to match 1:1 between different langs. - var tableNames = tuple.Left.Sort(); + var tableViews = tuple.Left.Sort((a, b) => a.viewName.CompareTo(b.viewName)); var addReducers = tuple.Right.Sort((a, b) => a.Key.CompareTo(b.Key)); // Don't generate the FFI boilerplate if there are no tables or reducers. - if (tableNames.IsEmpty && addReducers.IsEmpty) + if (tableViews.IsEmpty && addReducers.IsEmpty) { return; } @@ -419,6 +530,30 @@ public void Initialize(IncrementalGeneratorInitializationContext context) using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + namespace SpacetimeDB { + public sealed record ReducerContext : DbContext, Internal.IReducerContext { + public readonly Identity Sender; + public readonly Address? Address; + public readonly Random Random; + public readonly DateTimeOffset Time; + + internal ReducerContext(Identity sender, Address? address, Random random, DateTimeOffset time) { + Sender = sender; + Address = address; + Random = random; + Time = time; + } + } + + namespace Internal.TableHandles { + {{string.Join("\n", tableViews.Select(v => v.view))}} + } + + public sealed class Local { + {{string.Join("\n", tableViews.Select(v => v.getter))}} + } + } + static class ModuleRegistration { {{string.Join("\n", addReducers.Select(r => r.Value))}} @@ -431,6 +566,8 @@ static class ModuleRegistration { [DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, typeof(SpacetimeDB.Internal.Module))] #endif public static void Main() { + SpacetimeDB.Internal.Module.SetReducerContextConstructor((identity, address, random, time) => new SpacetimeDB.ReducerContext(identity, address, random, time)); + {{string.Join( "\n", addReducers.Select(r => @@ -439,7 +576,7 @@ public static void Main() { )}} {{string.Join( "\n", - tableNames.Select(t => $"SpacetimeDB.Internal.Module.RegisterTable<{t}>();") + tableViews.Select(t => $"SpacetimeDB.Internal.Module.RegisterTable<{t.tableName}>();") )}} } diff --git a/crates/bindings-csharp/Runtime/Attrs.cs b/crates/bindings-csharp/Runtime/Attrs.cs index 648c925dbc9..578d0293d93 100644 --- a/crates/bindings-csharp/Runtime/Attrs.cs +++ b/crates/bindings-csharp/Runtime/Attrs.cs @@ -1,30 +1,5 @@ namespace SpacetimeDB; -public static class ReducerKind -{ - public const string Init = "__init__"; - public const string Update = "__update__"; - public const string Connect = "__identity_connected__"; - public const string Disconnect = "__identity_disconnected__"; -} - -[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] -public sealed class ReducerAttribute(string? name = null) : Attribute -{ - public string? Name => name; -} - -[AttributeUsage( - AttributeTargets.Struct | AttributeTargets.Class, - Inherited = false, - AllowMultiple = false -)] -public sealed class TableAttribute : Attribute -{ - public bool Public { get; init; } - public string? Scheduled { get; init; } -} - [Flags] public enum ColumnAttrs : byte { @@ -40,8 +15,59 @@ public enum ColumnAttrs : byte PrimaryKeyIdentity = PrimaryKeyAuto, } -[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] -public sealed class ColumnAttribute(ColumnAttrs type) : Attribute +/// +/// Registers a type as the row structure of a SpacetimeDB table, enabling codegen for it. +/// +/// +/// Multiple [Table] attributes per type are supported. This is useful to reuse row types. +/// Each attribute instance must have a unique name and will create a SpacetimeDB table. +/// +/// +[AttributeUsage(AttributeTargets.Struct | AttributeTargets.Class, AllowMultiple = true)] +public sealed class TableAttribute : Attribute +{ + /// + /// This identifier is used to name the SpacetimeDB table on the host as well as the + /// table handle structures generated to access the table from within a reducer call. + /// + /// Defaults to the nameof of the target type. + /// + public string? Name { get; init; } + + /// + /// Set to true to make the table visible to everyone. + /// + /// Defaults to the table only being visible to its owner. + /// + public bool Public { get; init; } = false; + + public string? Scheduled { get; init; } +} + +[AttributeUsage(AttributeTargets.Field)] +public abstract class ColumnAttribute : Attribute { - public ColumnAttrs Type => type; + public string? Table { get; init; } +} + +public sealed class AutoIncAttribute : ColumnAttribute { } + +public sealed class PrimaryKeyAttribute : ColumnAttribute { } + +public sealed class UniqueAttribute : ColumnAttribute { } + +public sealed class IndexedAttribute : ColumnAttribute { } + +public static class ReducerKind +{ + public const string Init = "__init__"; + public const string Update = "__update__"; + public const string Connect = "__identity_connected__"; + public const string Disconnect = "__identity_disconnected__"; +} + +[AttributeUsage(AttributeTargets.Method, Inherited = false)] +public sealed class ReducerAttribute(string? name = null) : Attribute +{ + public string? Name => name; } diff --git a/crates/bindings-csharp/Runtime/Internal/IReducer.cs b/crates/bindings-csharp/Runtime/Internal/IReducer.cs index bb5c8e1fa95..3624c06f6e8 100644 --- a/crates/bindings-csharp/Runtime/Internal/IReducer.cs +++ b/crates/bindings-csharp/Runtime/Internal/IReducer.cs @@ -3,12 +3,14 @@ namespace SpacetimeDB.Internal; using System.Text; using SpacetimeDB.BSATN; +public interface IReducerContext { } + public interface IReducer { ReducerDef MakeReducerDef(ITypeRegistrar registrar); // This one is not static because we need to be able to store IReducer in a list. - void Invoke(BinaryReader reader, ReducerContext args); + void Invoke(BinaryReader reader, IReducerContext args); public static void VolatileNonatomicScheduleImmediate(string name, MemoryStream args) { diff --git a/crates/bindings-csharp/Runtime/Internal/ITable.cs b/crates/bindings-csharp/Runtime/Internal/ITable.cs index 2233e04db84..f5d820ee347 100644 --- a/crates/bindings-csharp/Runtime/Internal/ITable.cs +++ b/crates/bindings-csharp/Runtime/Internal/ITable.cs @@ -7,12 +7,18 @@ public interface ITable : IStructuralReadWrite where T : ITable, new() { // These are the methods that codegen needs to implement. - void ReadGenFields(BinaryReader reader); - static abstract TableDesc MakeTableDesc(ITypeRegistrar registrar); + static abstract IEnumerable MakeTableDesc(ITypeRegistrar registrar); + static abstract Filter CreateFilter(); +} - // These are static helpers that codegen can use. +public interface ITableView + where View : ITableView + where T : ITable, new() +{ + static abstract T ReadGenFields(BinaryReader reader, T row); + // These are static helpers that codegen can use. private abstract class RawTableIterBase { public class Enumerator(FFI.RowIter handle) : IDisposable @@ -105,7 +111,7 @@ public IEnumerable Parse() using var reader = new BinaryReader(stream); while (stream.Position < stream.Length) { - yield return Read(reader); + yield return IStructuralReadWrite.Read(reader); } } } @@ -134,7 +140,7 @@ protected override void IterStart(out FFI.RowIter handle) => private static readonly Lazy tableId_ = new(() => { - var name_bytes = System.Text.Encoding.UTF8.GetBytes(typeof(T).Name); + var name_bytes = System.Text.Encoding.UTF8.GetBytes(typeof(View).Name); FFI._table_id_from_name(name_bytes, (uint)name_bytes.Length, out var out_); return out_; }); @@ -148,17 +154,17 @@ protected override void IterStart(out FFI.RowIter handle) => public static IEnumerable Query(Expression> query) => new RawTableIterFiltered(tableId, filter.Value.Compile(query)).Parse(); - protected static void Insert(T row) + protected static T Insert(T row) { // Insert the row. - var bytes = ToBytes(row); + var bytes = IStructuralReadWrite.ToBytes(row); var bytes_len = (uint)bytes.Length; FFI._datastore_insert_bsatn(tableId, bytes, ref bytes_len); // Write back any generated column values. using var stream = new MemoryStream(bytes, 0, (int)bytes_len); using var reader = new BinaryReader(stream); - row.ReadGenFields(reader); + return View.ReadGenFields(reader, row); } protected readonly ref struct ColEq @@ -175,7 +181,7 @@ private ColEq(FFI.ColId colId, byte[] value) public static ColEq Where(ushort colId, TCol colValue, TColRW rw) where TColRW : IReadWrite { - return new(new FFI.ColId(colId), ToBytes(rw, colValue)); + return new(new FFI.ColId(colId), IStructuralReadWrite.ToBytes(rw, colValue)); } // Note: do not inline FindBy from the Codegen as a helper API here. diff --git a/crates/bindings-csharp/Runtime/Internal/Module.cs b/crates/bindings-csharp/Runtime/Internal/Module.cs index 425331c015b..5a4452fe60c 100644 --- a/crates/bindings-csharp/Runtime/Internal/Module.cs +++ b/crates/bindings-csharp/Runtime/Internal/Module.cs @@ -53,6 +53,13 @@ public static class Module private static readonly RawModuleDefV8 moduleDef = new(); private static readonly List reducers = []; + private static Func? newContext = + null; + + public static void SetReducerContextConstructor( + Func ctor + ) => newContext = ctor; + readonly struct TypeRegistrar() : ITypeRegistrar { private readonly Dictionary types = []; @@ -95,7 +102,10 @@ public static void RegisterReducer() public static void RegisterTable() where T : ITable, new() { - moduleDef.RegisterTable(T.MakeTableDesc(typeRegistrar)); + foreach (var t in T.MakeTableDesc(typeRegistrar)) + { + moduleDef.RegisterTable(t); + } } private static byte[] Consume(this BytesSource source) @@ -182,20 +192,22 @@ public static Errno __call_reducer__( BytesSink error ) { - // Piece together the sender identity. - var sender = Identity.From( - MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]).ToArray() - ); - - // Piece together the sender address. - var address = Address.From(MemoryMarshal.AsBytes([address_0, address_1]).ToArray()); - try { + var senderIdentity = Identity.From( + MemoryMarshal.AsBytes([sender_0, sender_1, sender_2, sender_3]).ToArray() + ); + var senderAddress = Address.From( + MemoryMarshal.AsBytes([address_0, address_1]).ToArray() + ); + var random = new Random((int)timestamp.MicrosecondsSinceEpoch); + var time = timestamp.ToStd(); + + var ctx = newContext!(senderIdentity, senderAddress, random, time); + using var stream = new MemoryStream(args.Consume()); using var reader = new BinaryReader(stream); - var context = new ReducerContext(sender, address, timestamp); - reducers[(int)id].Invoke(reader, context); + reducers[(int)id].Invoke(reader, ctx); if (stream.Position != stream.Length) { throw new Exception("Unrecognised extra bytes in the reducer arguments"); diff --git a/crates/bindings-csharp/Runtime/LogStopwatch.cs b/crates/bindings-csharp/Runtime/LogStopwatch.cs index 7965b987823..effb2d57852 100644 --- a/crates/bindings-csharp/Runtime/LogStopwatch.cs +++ b/crates/bindings-csharp/Runtime/LogStopwatch.cs @@ -1,7 +1,7 @@ -using System.Text; -using SpacetimeDB.Internal; +namespace SpacetimeDB; -namespace SpacetimeDB; +using System.Text; +using SpacetimeDB.Internal; public sealed class LogStopwatch : IDisposable { diff --git a/crates/bindings-csharp/Runtime/Runtime.cs b/crates/bindings-csharp/Runtime/Runtime.cs index 054b0fbbb5a..badb81a053c 100644 --- a/crates/bindings-csharp/Runtime/Runtime.cs +++ b/crates/bindings-csharp/Runtime/Runtime.cs @@ -4,30 +4,6 @@ namespace SpacetimeDB; using SpacetimeDB.BSATN; using SpacetimeDB.Internal; -public class ReducerContext -{ - public readonly Identity Sender; - public readonly DateTimeOffset Time; - public readonly Address? Address; - - /// - /// A reducer-specific instance of `System.Random` that is seeded by current reducer's timestamp. This object is unchanged throught the entire reducer call - /// - public readonly Random Rng; - - internal ReducerContext( - Identity senderIdentity, - Address? senderAddress, - DateTimeOffsetRepr timestamp - ) - { - Sender = senderIdentity; - Address = senderAddress; - Time = timestamp.ToStd(); - Rng = new Random((int)timestamp.MicrosecondsSinceEpoch); - } -} - // [SpacetimeDB.Type] - we have custom representation of time in microseconds, so implementing BSATN manually public abstract partial record ScheduleAt : SpacetimeDB.TaggedEnum<(DateTimeOffset Time, TimeSpan Interval)> diff --git a/crates/bindings-csharp/SpacetimeSharpSATS.sln b/crates/bindings-csharp/SpacetimeSharpSATS.sln index f76a5c6ce3a..0cc2abf8a52 100644 --- a/crates/bindings-csharp/SpacetimeSharpSATS.sln +++ b/crates/bindings-csharp/SpacetimeSharpSATS.sln @@ -18,6 +18,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{12907A .editorconfig = .editorconfig EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sdk-test-connect-disconnect-cs", "..\..\modules\sdk-test-connect-disconnect-cs\sdk-test-connect-disconnect-cs.csproj", "{5393711C-44B0-4752-B8D0-852C73D6866F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sdk-test-cs", "..\..\modules\sdk-test-cs\sdk-test-cs.csproj", "{40F1C615-EDD9-463F-A012-B232F6710FA5}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "spacetimedb-quickstart-server-cs", "..\..\modules\spacetimedb-quickstart-cs\spacetimedb-quickstart-server-cs.csproj", "{FDACD960-168E-44F9-B036-2E29EA391BE7}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "sdk-test-multi-cs", "..\..\modules\sdk-test-multi-cs\sdk-test-multi-cs.csproj", "{960384A9-D78F-4C07-986A-1D1F3846AEBE}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -44,6 +52,22 @@ Global {2C282EBD-8E37-4F4C-8EE1-E91E21E75FEE}.Debug|Any CPU.Build.0 = Debug|Any CPU {2C282EBD-8E37-4F4C-8EE1-E91E21E75FEE}.Release|Any CPU.ActiveCfg = Release|Any CPU {2C282EBD-8E37-4F4C-8EE1-E91E21E75FEE}.Release|Any CPU.Build.0 = Release|Any CPU + {5393711C-44B0-4752-B8D0-852C73D6866F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5393711C-44B0-4752-B8D0-852C73D6866F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5393711C-44B0-4752-B8D0-852C73D6866F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5393711C-44B0-4752-B8D0-852C73D6866F}.Release|Any CPU.Build.0 = Release|Any CPU + {40F1C615-EDD9-463F-A012-B232F6710FA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {40F1C615-EDD9-463F-A012-B232F6710FA5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {40F1C615-EDD9-463F-A012-B232F6710FA5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {40F1C615-EDD9-463F-A012-B232F6710FA5}.Release|Any CPU.Build.0 = Release|Any CPU + {FDACD960-168E-44F9-B036-2E29EA391BE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FDACD960-168E-44F9-B036-2E29EA391BE7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FDACD960-168E-44F9-B036-2E29EA391BE7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FDACD960-168E-44F9-B036-2E29EA391BE7}.Release|Any CPU.Build.0 = Release|Any CPU + {960384A9-D78F-4C07-986A-1D1F3846AEBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {960384A9-D78F-4C07-986A-1D1F3846AEBE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {960384A9-D78F-4C07-986A-1D1F3846AEBE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {960384A9-D78F-4C07-986A-1D1F3846AEBE}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/crates/cli/src/subcommands/project/csharp/Lib._cs b/crates/cli/src/subcommands/project/csharp/Lib._cs index 83f83490efe..98a85f15a4d 100644 --- a/crates/cli/src/subcommands/project/csharp/Lib._cs +++ b/crates/cli/src/subcommands/project/csharp/Lib._cs @@ -1,28 +1,28 @@ using SpacetimeDB; -static partial class Module +public static partial class Module { [SpacetimeDB.Table] public partial struct Person { - [SpacetimeDB.Column(ColumnAttrs.Unique | ColumnAttrs.AutoInc)] + [SpacetimeDB.AutoInc] + [SpacetimeDB.PrimaryKey] public int Id; public string Name; public int Age; } [SpacetimeDB.Reducer] - public static void Add(string name, int age) + public static void Add(ReducerContext ctx, string name, int age) { - var person = new Person { Name = name, Age = age }; - person.Insert(); + var person = ctx.Db.Person.Insert(new Person { Name = name, Age = age }); Log.Info($"Inserted {person.Name} under #{person.Id}"); } [SpacetimeDB.Reducer] - public static void SayHello() + public static void SayHello(ReducerContext ctx) { - foreach (var person in Person.Iter()) + foreach (var person in ctx.Db.Person.Iter()) { Log.Info($"Hello, {person.Name}!"); } diff --git a/modules/sdk-test-connect-disconnect-cs/Lib.cs b/modules/sdk-test-connect-disconnect-cs/Lib.cs index 4688033cb46..542780c6538 100644 --- a/modules/sdk-test-connect-disconnect-cs/Lib.cs +++ b/modules/sdk-test-connect-disconnect-cs/Lib.cs @@ -1,28 +1,30 @@ +namespace SpacetimeDB.Sdk.Test.ConnectDisconnect; + using SpacetimeDB; -static partial class Module +[SpacetimeDB.Table(Public = true)] +public partial struct Connected { - [SpacetimeDB.Table(Public = true)] - public partial struct Connected - { - public Identity identity; - } + public Identity identity; +} - [SpacetimeDB.Table(Public = true)] - public partial struct Disconnected - { - public Identity identity; - } +[SpacetimeDB.Table(Public = true)] +public partial struct Disconnected +{ + public Identity identity; +} +static partial class Module +{ [SpacetimeDB.Reducer(ReducerKind.Connect)] - public static void OnConnect(ReducerContext e) + public static void OnConnect(ReducerContext ctx) { - new Connected { identity = e.Sender }.Insert(); + ctx.Db.Connected.Insert(new Connected { identity = ctx.Sender }); } [SpacetimeDB.Reducer(ReducerKind.Disconnect)] - public static void OnDisconnect(ReducerContext e) + public static void OnDisconnect(ReducerContext ctx) { - new Disconnected { identity = e.Sender }.Insert(); + ctx.Db.Disconnected.Insert(new Disconnected { identity = ctx.Sender }); } } diff --git a/modules/sdk-test-connect-disconnect-cs/StdbModule.csproj b/modules/sdk-test-connect-disconnect-cs/sdk-test-connect-disconnect-cs.csproj similarity index 94% rename from modules/sdk-test-connect-disconnect-cs/StdbModule.csproj rename to modules/sdk-test-connect-disconnect-cs/sdk-test-connect-disconnect-cs.csproj index 7773fe10c08..805df19a725 100644 --- a/modules/sdk-test-connect-disconnect-cs/StdbModule.csproj +++ b/modules/sdk-test-connect-disconnect-cs/sdk-test-connect-disconnect-cs.csproj @@ -1,6 +1,7 @@ + StdbModule net8.0 wasi-wasm enable diff --git a/modules/sdk-test-cs/Lib.cs b/modules/sdk-test-cs/Lib.cs index e77fe6890dd..bc5e3495266 100644 --- a/modules/sdk-test-cs/Lib.cs +++ b/modules/sdk-test-cs/Lib.cs @@ -1,6 +1,8 @@ +namespace SpacetimeDB.Sdk.Test; + using SpacetimeDB; -static partial class Module +public static partial class Module { [SpacetimeDB.Type] public enum SimpleEnum @@ -99,9 +101,9 @@ public partial struct OneU8 } [SpacetimeDB.Reducer] - public static void insert_one_u8(byte n) + public static void insert_one_u8(ReducerContext ctx, byte n) { - new OneU8 { n = n }.Insert(); + ctx.Db.OneU8.Insert(new OneU8 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -111,9 +113,9 @@ public partial struct OneU16 } [SpacetimeDB.Reducer] - public static void insert_one_u16(ushort n) + public static void insert_one_u16(ReducerContext ctx, ushort n) { - new OneU16 { n = n }.Insert(); + ctx.Db.OneU16.Insert(new OneU16 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -123,9 +125,9 @@ public partial struct OneU32 } [SpacetimeDB.Reducer] - public static void insert_one_u32(uint n) + public static void insert_one_u32(ReducerContext ctx, uint n) { - new OneU32 { n = n }.Insert(); + ctx.Db.OneU32.Insert(new OneU32 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -135,9 +137,9 @@ public partial struct OneU64 } [SpacetimeDB.Reducer] - public static void insert_one_u64(ulong n) + public static void insert_one_u64(ReducerContext ctx, ulong n) { - new OneU64 { n = n }.Insert(); + ctx.Db.OneU64.Insert(new OneU64 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -147,9 +149,9 @@ public partial struct OneU128 } [SpacetimeDB.Reducer] - public static void insert_one_u128(U128 n) + public static void insert_one_u128(ReducerContext ctx, U128 n) { - new OneU128 { n = n }.Insert(); + ctx.Db.OneU128.Insert(new OneU128 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -159,9 +161,9 @@ public partial struct OneU256 } [SpacetimeDB.Reducer] - public static void insert_one_u256(U256 n) + public static void insert_one_u256(ReducerContext ctx, U256 n) { - new OneU256 { n = n }.Insert(); + ctx.Db.OneU256.Insert(new OneU256 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -171,9 +173,9 @@ public partial struct OneI8 } [SpacetimeDB.Reducer] - public static void insert_one_i8(sbyte n) + public static void insert_one_i8(ReducerContext ctx, sbyte n) { - new OneI8 { n = n }.Insert(); + ctx.Db.OneI8.Insert(new OneI8 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -183,9 +185,9 @@ public partial struct OneI16 } [SpacetimeDB.Reducer] - public static void insert_one_i16(short n) + public static void insert_one_i16(ReducerContext ctx, short n) { - new OneI16 { n = n }.Insert(); + ctx.Db.OneI16.Insert(new OneI16 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -195,9 +197,9 @@ public partial struct OneI32 } [SpacetimeDB.Reducer] - public static void insert_one_i32(int n) + public static void insert_one_i32(ReducerContext ctx, int n) { - new OneI32 { n = n }.Insert(); + ctx.Db.OneI32.Insert(new OneI32 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -207,9 +209,9 @@ public partial struct OneI64 } [SpacetimeDB.Reducer] - public static void insert_one_i64(long n) + public static void insert_one_i64(ReducerContext ctx, long n) { - new OneI64 { n = n }.Insert(); + ctx.Db.OneI64.Insert(new OneI64 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -219,9 +221,9 @@ public partial struct OneI128 } [SpacetimeDB.Reducer] - public static void insert_one_i128(I128 n) + public static void insert_one_i128(ReducerContext ctx, I128 n) { - new OneI128 { n = n }.Insert(); + ctx.Db.OneI128.Insert(new OneI128 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -231,9 +233,9 @@ public partial struct OneI256 } [SpacetimeDB.Reducer] - public static void insert_one_i256(I256 n) + public static void insert_one_i256(ReducerContext ctx, I256 n) { - new OneI256 { n = n }.Insert(); + ctx.Db.OneI256.Insert(new OneI256 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -243,9 +245,9 @@ public partial struct OneBool } [SpacetimeDB.Reducer] - public static void insert_one_bool(bool b) + public static void insert_one_bool(ReducerContext ctx, bool b) { - new OneBool { b = b }.Insert(); + ctx.Db.OneBool.Insert(new OneBool { b = b }); } [SpacetimeDB.Table(Public = true)] @@ -255,9 +257,9 @@ public partial struct OneF32 } [SpacetimeDB.Reducer] - public static void insert_one_f32(float f) + public static void insert_one_f32(ReducerContext ctx, float f) { - new OneF32 { f = f }.Insert(); + ctx.Db.OneF32.Insert(new OneF32 { f = f }); } [SpacetimeDB.Table(Public = true)] @@ -267,9 +269,9 @@ public partial struct OneF64 } [SpacetimeDB.Reducer] - public static void insert_one_f64(double f) + public static void insert_one_f64(ReducerContext ctx, double f) { - new OneF64 { f = f }.Insert(); + ctx.Db.OneF64.Insert(new OneF64 { f = f }); } [SpacetimeDB.Table(Public = true)] @@ -279,9 +281,9 @@ public partial struct OneString } [SpacetimeDB.Reducer] - public static void insert_one_string(string s) + public static void insert_one_string(ReducerContext ctx, string s) { - new OneString { s = s }.Insert(); + ctx.Db.OneString.Insert(new OneString { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -291,9 +293,9 @@ public partial struct OneIdentity } [SpacetimeDB.Reducer] - public static void insert_one_identity(Identity i) + public static void insert_one_identity(ReducerContext ctx, Identity i) { - new OneIdentity { i = i }.Insert(); + ctx.Db.OneIdentity.Insert(new OneIdentity { i = i }); } [SpacetimeDB.Table(Public = true)] @@ -303,9 +305,9 @@ public partial struct OneAddress } [SpacetimeDB.Reducer] - public static void insert_one_address(Address a) + public static void insert_one_address(ReducerContext ctx, Address a) { - new OneAddress { a = a }.Insert(); + ctx.Db.OneAddress.Insert(new OneAddress { a = a }); } [SpacetimeDB.Table(Public = true)] @@ -315,9 +317,9 @@ public partial struct OneSimpleEnum } [SpacetimeDB.Reducer] - public static void insert_one_simple_enum(SimpleEnum e) + public static void insert_one_simple_enum(ReducerContext ctx, SimpleEnum e) { - new OneSimpleEnum { e = e }.Insert(); + ctx.Db.OneSimpleEnum.Insert(new OneSimpleEnum { e = e }); } [SpacetimeDB.Table(Public = true)] @@ -327,9 +329,9 @@ public partial struct OneEnumWithPayload } [SpacetimeDB.Reducer] - public static void insert_one_enum_with_payload(EnumWithPayload e) + public static void insert_one_enum_with_payload(ReducerContext ctx, EnumWithPayload e) { - new OneEnumWithPayload { e = e }.Insert(); + ctx.Db.OneEnumWithPayload.Insert(new OneEnumWithPayload { e = e }); } [SpacetimeDB.Table(Public = true)] @@ -339,9 +341,9 @@ public partial struct OneUnitStruct } [SpacetimeDB.Reducer] - public static void insert_one_unit_struct(UnitStruct s) + public static void insert_one_unit_struct(ReducerContext ctx, UnitStruct s) { - new OneUnitStruct { s = s }.Insert(); + ctx.Db.OneUnitStruct.Insert(new OneUnitStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -351,9 +353,9 @@ public partial struct OneByteStruct } [SpacetimeDB.Reducer] - public static void insert_one_byte_struct(ByteStruct s) + public static void insert_one_byte_struct(ReducerContext ctx, ByteStruct s) { - new OneByteStruct { s = s }.Insert(); + ctx.Db.OneByteStruct.Insert(new OneByteStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -363,9 +365,9 @@ public partial struct OneEveryPrimitiveStruct } [SpacetimeDB.Reducer] - public static void insert_one_every_primitive_struct(EveryPrimitiveStruct s) + public static void insert_one_every_primitive_struct(ReducerContext ctx, EveryPrimitiveStruct s) { - new OneEveryPrimitiveStruct { s = s }.Insert(); + ctx.Db.OneEveryPrimitiveStruct.Insert(new OneEveryPrimitiveStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -375,9 +377,9 @@ public partial struct OneEveryVecStruct } [SpacetimeDB.Reducer] - public static void insert_one_every_vec_struct(EveryVecStruct s) + public static void insert_one_every_vec_struct(ReducerContext ctx, EveryVecStruct s) { - new OneEveryVecStruct { s = s }.Insert(); + ctx.Db.OneEveryVecStruct.Insert(new OneEveryVecStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -387,9 +389,9 @@ public partial struct VecU8 } [SpacetimeDB.Reducer] - public static void insert_vec_u8(List n) + public static void insert_vec_u8(ReducerContext ctx, List n) { - new VecU8 { n = n }.Insert(); + ctx.Db.VecU8.Insert(new VecU8 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -399,9 +401,9 @@ public partial struct VecU16 } [SpacetimeDB.Reducer] - public static void insert_vec_u16(List n) + public static void insert_vec_u16(ReducerContext ctx, List n) { - new VecU16 { n = n }.Insert(); + ctx.Db.VecU16.Insert(new VecU16 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -411,9 +413,9 @@ public partial struct VecU32 } [SpacetimeDB.Reducer] - public static void insert_vec_u32(List n) + public static void insert_vec_u32(ReducerContext ctx, List n) { - new VecU32 { n = n }.Insert(); + ctx.Db.VecU32.Insert(new VecU32 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -423,9 +425,9 @@ public partial struct VecU64 } [SpacetimeDB.Reducer] - public static void insert_vec_u64(List n) + public static void insert_vec_u64(ReducerContext ctx, List n) { - new VecU64 { n = n }.Insert(); + ctx.Db.VecU64.Insert(new VecU64 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -435,9 +437,9 @@ public partial struct VecU128 } [SpacetimeDB.Reducer] - public static void insert_vec_u128(List n) + public static void insert_vec_u128(ReducerContext ctx, List n) { - new VecU128 { n = n }.Insert(); + ctx.Db.VecU128.Insert(new VecU128 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -447,9 +449,9 @@ public partial struct VecU256 } [SpacetimeDB.Reducer] - public static void insert_vec_u256(List n) + public static void insert_vec_u256(ReducerContext ctx, List n) { - new VecU256 { n = n }.Insert(); + ctx.Db.VecU256.Insert(new VecU256 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -459,9 +461,9 @@ public partial struct VecI8 } [SpacetimeDB.Reducer] - public static void insert_vec_i8(List n) + public static void insert_vec_i8(ReducerContext ctx, List n) { - new VecI8 { n = n }.Insert(); + ctx.Db.VecI8.Insert(new VecI8 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -471,9 +473,9 @@ public partial struct VecI16 } [SpacetimeDB.Reducer] - public static void insert_vec_i16(List n) + public static void insert_vec_i16(ReducerContext ctx, List n) { - new VecI16 { n = n }.Insert(); + ctx.Db.VecI16.Insert(new VecI16 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -483,9 +485,9 @@ public partial struct VecI32 } [SpacetimeDB.Reducer] - public static void insert_vec_i32(List n) + public static void insert_vec_i32(ReducerContext ctx, List n) { - new VecI32 { n = n }.Insert(); + ctx.Db.VecI32.Insert(new VecI32 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -495,9 +497,9 @@ public partial struct VecI64 } [SpacetimeDB.Reducer] - public static void insert_vec_i64(List n) + public static void insert_vec_i64(ReducerContext ctx, List n) { - new VecI64 { n = n }.Insert(); + ctx.Db.VecI64.Insert(new VecI64 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -507,9 +509,9 @@ public partial struct VecI128 } [SpacetimeDB.Reducer] - public static void insert_vec_i128(List n) + public static void insert_vec_i128(ReducerContext ctx, List n) { - new VecI128 { n = n }.Insert(); + ctx.Db.VecI128.Insert(new VecI128 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -519,9 +521,9 @@ public partial struct VecI256 } [SpacetimeDB.Reducer] - public static void insert_vec_i256(List n) + public static void insert_vec_i256(ReducerContext ctx, List n) { - new VecI256 { n = n }.Insert(); + ctx.Db.VecI256.Insert(new VecI256 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -531,9 +533,9 @@ public partial struct VecBool } [SpacetimeDB.Reducer] - public static void insert_vec_bool(List b) + public static void insert_vec_bool(ReducerContext ctx, List b) { - new VecBool { b = b }.Insert(); + ctx.Db.VecBool.Insert(new VecBool { b = b }); } [SpacetimeDB.Table(Public = true)] @@ -543,9 +545,9 @@ public partial struct VecF32 } [SpacetimeDB.Reducer] - public static void insert_vec_f32(List f) + public static void insert_vec_f32(ReducerContext ctx, List f) { - new VecF32 { f = f }.Insert(); + ctx.Db.VecF32.Insert(new VecF32 { f = f }); } [SpacetimeDB.Table(Public = true)] @@ -555,9 +557,9 @@ public partial struct VecF64 } [SpacetimeDB.Reducer] - public static void insert_vec_f64(List f) + public static void insert_vec_f64(ReducerContext ctx, List f) { - new VecF64 { f = f }.Insert(); + ctx.Db.VecF64.Insert(new VecF64 { f = f }); } [SpacetimeDB.Table(Public = true)] @@ -567,9 +569,9 @@ public partial struct VecString } [SpacetimeDB.Reducer] - public static void insert_vec_string(List s) + public static void insert_vec_string(ReducerContext ctx, List s) { - new VecString { s = s }.Insert(); + ctx.Db.VecString.Insert(new VecString { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -579,9 +581,9 @@ public partial struct VecIdentity } [SpacetimeDB.Reducer] - public static void insert_vec_identity(List i) + public static void insert_vec_identity(ReducerContext ctx, List i) { - new VecIdentity { i = i }.Insert(); + ctx.Db.VecIdentity.Insert(new VecIdentity { i = i }); } [SpacetimeDB.Table(Public = true)] @@ -591,9 +593,9 @@ public partial struct VecAddress } [SpacetimeDB.Reducer] - public static void insert_vec_address(List
a) + public static void insert_vec_address(ReducerContext ctx, List
a) { - new VecAddress { a = a }.Insert(); + ctx.Db.VecAddress.Insert(new VecAddress { a = a }); } [SpacetimeDB.Table(Public = true)] @@ -603,9 +605,9 @@ public partial struct VecSimpleEnum } [SpacetimeDB.Reducer] - public static void insert_vec_simple_enum(List e) + public static void insert_vec_simple_enum(ReducerContext ctx, List e) { - new VecSimpleEnum { e = e }.Insert(); + ctx.Db.VecSimpleEnum.Insert(new VecSimpleEnum { e = e }); } [SpacetimeDB.Table(Public = true)] @@ -615,9 +617,9 @@ public partial struct VecEnumWithPayload } [SpacetimeDB.Reducer] - public static void insert_vec_enum_with_payload(List e) + public static void insert_vec_enum_with_payload(ReducerContext ctx, List e) { - new VecEnumWithPayload { e = e }.Insert(); + ctx.Db.VecEnumWithPayload.Insert(new VecEnumWithPayload { e = e }); } [SpacetimeDB.Table(Public = true)] @@ -627,9 +629,9 @@ public partial struct VecUnitStruct } [SpacetimeDB.Reducer] - public static void insert_vec_unit_struct(List s) + public static void insert_vec_unit_struct(ReducerContext ctx, List s) { - new VecUnitStruct { s = s }.Insert(); + ctx.Db.VecUnitStruct.Insert(new VecUnitStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -639,9 +641,9 @@ public partial struct VecByteStruct } [SpacetimeDB.Reducer] - public static void insert_vec_byte_struct(List s) + public static void insert_vec_byte_struct(ReducerContext ctx, List s) { - new VecByteStruct { s = s }.Insert(); + ctx.Db.VecByteStruct.Insert(new VecByteStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -651,9 +653,9 @@ public partial struct VecEveryPrimitiveStruct } [SpacetimeDB.Reducer] - public static void insert_vec_every_primitive_struct(List s) + public static void insert_vec_every_primitive_struct(ReducerContext ctx, List s) { - new VecEveryPrimitiveStruct { s = s }.Insert(); + ctx.Db.VecEveryPrimitiveStruct.Insert(new VecEveryPrimitiveStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -663,9 +665,9 @@ public partial struct VecEveryVecStruct } [SpacetimeDB.Reducer] - public static void insert_vec_every_vec_struct(List s) + public static void insert_vec_every_vec_struct(ReducerContext ctx, List s) { - new VecEveryVecStruct { s = s }.Insert(); + ctx.Db.VecEveryVecStruct.Insert(new VecEveryVecStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -675,9 +677,9 @@ public partial struct OptionI32 } [SpacetimeDB.Reducer] - public static void insert_option_i32(int? n) + public static void insert_option_i32(ReducerContext ctx, int? n) { - new OptionI32 { n = n }.Insert(); + ctx.Db.OptionI32.Insert(new OptionI32 { n = n }); } [SpacetimeDB.Table(Public = true)] @@ -687,9 +689,9 @@ public partial struct OptionString } [SpacetimeDB.Reducer] - public static void insert_option_string(string? s) + public static void insert_option_string(ReducerContext ctx, string? s) { - new OptionString { s = s }.Insert(); + ctx.Db.OptionString.Insert(new OptionString { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -699,9 +701,9 @@ public partial struct OptionIdentity } [SpacetimeDB.Reducer] - public static void insert_option_identity(Identity? i) + public static void insert_option_identity(ReducerContext ctx, Identity? i) { - new OptionIdentity { i = i }.Insert(); + ctx.Db.OptionIdentity.Insert(new OptionIdentity { i = i }); } [SpacetimeDB.Table(Public = true)] @@ -711,9 +713,9 @@ public partial struct OptionSimpleEnum } [SpacetimeDB.Reducer] - public static void insert_option_simple_enum(SimpleEnum? e) + public static void insert_option_simple_enum(ReducerContext ctx, SimpleEnum? e) { - new OptionSimpleEnum { e = e }.Insert(); + ctx.Db.OptionSimpleEnum.Insert(new OptionSimpleEnum { e = e }); } [SpacetimeDB.Table(Public = true)] @@ -723,9 +725,9 @@ public partial struct OptionEveryPrimitiveStruct } [SpacetimeDB.Reducer] - public static void insert_option_every_primitive_struct(EveryPrimitiveStruct? s) + public static void insert_option_every_primitive_struct(ReducerContext ctx, EveryPrimitiveStruct? s) { - new OptionEveryPrimitiveStruct { s = s }.Insert(); + ctx.Db.OptionEveryPrimitiveStruct.Insert(new OptionEveryPrimitiveStruct { s = s }); } [SpacetimeDB.Table(Public = true)] @@ -735,903 +737,903 @@ public partial struct OptionVecOptionI32 } [SpacetimeDB.Reducer] - public static void insert_option_vec_option_i32(List? v) + public static void insert_option_vec_option_i32(ReducerContext ctx, List? v) { - new OptionVecOptionI32 { v = v }.Insert(); + ctx.Db.OptionVecOptionI32.Insert(new OptionVecOptionI32 { v = v }); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueU8 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public byte n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_u8(byte n, int data) + public static void insert_unique_u8(ReducerContext ctx, byte n, int data) { - new UniqueU8 { n = n, data = data }.Insert(); + ctx.Db.UniqueU8.Insert(new UniqueU8 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_u8(byte n, int data) + public static void update_unique_u8(ReducerContext ctx, byte n, int data) { var key = n; - UniqueU8.UpdateByn(key, new UniqueU8 { n = n, data = data }); + ctx.Db.UniqueU8.UpdateByn(key, new UniqueU8 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_u8(byte n) + public static void delete_unique_u8(ReducerContext ctx, byte n) { - UniqueU8.DeleteByn(n); + ctx.Db.UniqueU8.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueU16 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public ushort n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_u16(ushort n, int data) + public static void insert_unique_u16(ReducerContext ctx, ushort n, int data) { - new UniqueU16 { n = n, data = data }.Insert(); + ctx.Db.UniqueU16.Insert(new UniqueU16 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_u16(ushort n, int data) + public static void update_unique_u16(ReducerContext ctx, ushort n, int data) { var key = n; - UniqueU16.UpdateByn(key, new UniqueU16 { n = n, data = data }); + ctx.Db.UniqueU16.UpdateByn(key, new UniqueU16 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_u16(ushort n) + public static void delete_unique_u16(ReducerContext ctx, ushort n) { - UniqueU16.DeleteByn(n); + ctx.Db.UniqueU16.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueU32 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public uint n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_u32(uint n, int data) + public static void insert_unique_u32(ReducerContext ctx, uint n, int data) { - new UniqueU32 { n = n, data = data }.Insert(); + ctx.Db.UniqueU32.Insert(new UniqueU32 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_u32(uint n, int data) + public static void update_unique_u32(ReducerContext ctx, uint n, int data) { var key = n; - UniqueU32.UpdateByn(key, new UniqueU32 { n = n, data = data }); + ctx.Db.UniqueU32.UpdateByn(key, new UniqueU32 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_u32(uint n) + public static void delete_unique_u32(ReducerContext ctx, uint n) { - UniqueU32.DeleteByn(n); + ctx.Db.UniqueU32.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueU64 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public ulong n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_u64(ulong n, int data) + public static void insert_unique_u64(ReducerContext ctx, ulong n, int data) { - new UniqueU64 { n = n, data = data }.Insert(); + ctx.Db.UniqueU64.Insert(new UniqueU64 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_u64(ulong n, int data) + public static void update_unique_u64(ReducerContext ctx, ulong n, int data) { var key = n; - UniqueU64.UpdateByn(key, new UniqueU64 { n = n, data = data }); + ctx.Db.UniqueU64.UpdateByn(key, new UniqueU64 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_u64(ulong n) + public static void delete_unique_u64(ReducerContext ctx, ulong n) { - UniqueU64.DeleteByn(n); + ctx.Db.UniqueU64.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueU128 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public U128 n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_u128(U128 n, int data) + public static void insert_unique_u128(ReducerContext ctx, U128 n, int data) { - new UniqueU128 { n = n, data = data }.Insert(); + ctx.Db.UniqueU128.Insert(new UniqueU128 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_u128(U128 n, int data) + public static void update_unique_u128(ReducerContext ctx, U128 n, int data) { var key = n; - UniqueU128.UpdateByn(key, new UniqueU128 { n = n, data = data }); + ctx.Db.UniqueU128.UpdateByn(key, new UniqueU128 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_u128(U128 n) + public static void delete_unique_u128(ReducerContext ctx, U128 n) { - UniqueU128.DeleteByn(n); + ctx.Db.UniqueU128.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueU256 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public U256 n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_u256(U256 n, int data) + public static void insert_unique_u256(ReducerContext ctx, U256 n, int data) { - new UniqueU256 { n = n, data = data }.Insert(); + ctx.Db.UniqueU256.Insert(new UniqueU256 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_u256(U256 n, int data) + public static void update_unique_u256(ReducerContext ctx, U256 n, int data) { var key = n; - UniqueU256.UpdateByn(key, new UniqueU256 { n = n, data = data }); + ctx.Db.UniqueU256.UpdateByn(key, new UniqueU256 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_u256(U256 n) + public static void delete_unique_u256(ReducerContext ctx, U256 n) { - UniqueU256.DeleteByn(n); + ctx.Db.UniqueU256.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueI8 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public sbyte n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_i8(sbyte n, int data) + public static void insert_unique_i8(ReducerContext ctx, sbyte n, int data) { - new UniqueI8 { n = n, data = data }.Insert(); + ctx.Db.UniqueI8.Insert(new UniqueI8 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_i8(sbyte n, int data) + public static void update_unique_i8(ReducerContext ctx, sbyte n, int data) { var key = n; - UniqueI8.UpdateByn(key, new UniqueI8 { n = n, data = data }); + ctx.Db.UniqueI8.UpdateByn(key, new UniqueI8 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_i8(sbyte n) + public static void delete_unique_i8(ReducerContext ctx, sbyte n) { - UniqueI8.DeleteByn(n); + ctx.Db.UniqueI8.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueI16 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public short n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_i16(short n, int data) + public static void insert_unique_i16(ReducerContext ctx, short n, int data) { - new UniqueI16 { n = n, data = data }.Insert(); + ctx.Db.UniqueI16.Insert(new UniqueI16 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_i16(short n, int data) + public static void update_unique_i16(ReducerContext ctx, short n, int data) { var key = n; - UniqueI16.UpdateByn(key, new UniqueI16 { n = n, data = data }); + ctx.Db.UniqueI16.UpdateByn(key, new UniqueI16 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_i16(short n) + public static void delete_unique_i16(ReducerContext ctx, short n) { - UniqueI16.DeleteByn(n); + ctx.Db.UniqueI16.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueI32 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public int n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_i32(int n, int data) + public static void insert_unique_i32(ReducerContext ctx, int n, int data) { - new UniqueI32 { n = n, data = data }.Insert(); + ctx.Db.UniqueI32.Insert(new UniqueI32 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_i32(int n, int data) + public static void update_unique_i32(ReducerContext ctx, int n, int data) { var key = n; - UniqueI32.UpdateByn(key, new UniqueI32 { n = n, data = data }); + ctx.Db.UniqueI32.UpdateByn(key, new UniqueI32 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_i32(int n) + public static void delete_unique_i32(ReducerContext ctx, int n) { - UniqueI32.DeleteByn(n); + ctx.Db.UniqueI32.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueI64 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public long n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_i64(long n, int data) + public static void insert_unique_i64(ReducerContext ctx, long n, int data) { - new UniqueI64 { n = n, data = data }.Insert(); + ctx.Db.UniqueI64.Insert(new UniqueI64 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_i64(long n, int data) + public static void update_unique_i64(ReducerContext ctx, long n, int data) { var key = n; - UniqueI64.UpdateByn(key, new UniqueI64 { n = n, data = data }); + ctx.Db.UniqueI64.UpdateByn(key, new UniqueI64 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_i64(long n) + public static void delete_unique_i64(ReducerContext ctx, long n) { - UniqueI64.DeleteByn(n); + ctx.Db.UniqueI64.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueI128 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public I128 n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_i128(I128 n, int data) + public static void insert_unique_i128(ReducerContext ctx, I128 n, int data) { - new UniqueI128 { n = n, data = data }.Insert(); + ctx.Db.UniqueI128.Insert(new UniqueI128 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_i128(I128 n, int data) + public static void update_unique_i128(ReducerContext ctx, I128 n, int data) { var key = n; - UniqueI128.UpdateByn(key, new UniqueI128 { n = n, data = data }); + ctx.Db.UniqueI128.UpdateByn(key, new UniqueI128 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_i128(I128 n) + public static void delete_unique_i128(ReducerContext ctx, I128 n) { - UniqueI128.DeleteByn(n); + ctx.Db.UniqueI128.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueI256 { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public I256 n; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_i256(I256 n, int data) + public static void insert_unique_i256(ReducerContext ctx, I256 n, int data) { - new UniqueI256 { n = n, data = data }.Insert(); + ctx.Db.UniqueI256.Insert(new UniqueI256 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_i256(I256 n, int data) + public static void update_unique_i256(ReducerContext ctx, I256 n, int data) { var key = n; - UniqueI256.UpdateByn(key, new UniqueI256 { n = n, data = data }); + ctx.Db.UniqueI256.UpdateByn(key, new UniqueI256 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_i256(I256 n) + public static void delete_unique_i256(ReducerContext ctx, I256 n) { - UniqueI256.DeleteByn(n); + ctx.Db.UniqueI256.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueBool { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public bool b; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_bool(bool b, int data) + public static void insert_unique_bool(ReducerContext ctx, bool b, int data) { - new UniqueBool { b = b, data = data }.Insert(); + ctx.Db.UniqueBool.Insert(new UniqueBool { b = b, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_bool(bool b, int data) + public static void update_unique_bool(ReducerContext ctx, bool b, int data) { var key = b; - UniqueBool.UpdateByb(key, new UniqueBool { b = b, data = data }); + ctx.Db.UniqueBool.UpdateByb(key, new UniqueBool { b = b, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_bool(bool b) + public static void delete_unique_bool(ReducerContext ctx, bool b) { - UniqueBool.DeleteByb(b); + ctx.Db.UniqueBool.DeleteByb(b); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueString { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public string s; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_string(string s, int data) + public static void insert_unique_string(ReducerContext ctx, string s, int data) { - new UniqueString { s = s, data = data }.Insert(); + ctx.Db.UniqueString.Insert(new UniqueString { s = s, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_string(string s, int data) + public static void update_unique_string(ReducerContext ctx, string s, int data) { var key = s; - UniqueString.UpdateBys(key, new UniqueString { s = s, data = data }); + ctx.Db.UniqueString.UpdateBys(key, new UniqueString { s = s, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_string(string s) + public static void delete_unique_string(ReducerContext ctx, string s) { - UniqueString.DeleteBys(s); + ctx.Db.UniqueString.DeleteBys(s); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueIdentity { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public Identity i; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_identity(Identity i, int data) + public static void insert_unique_identity(ReducerContext ctx, Identity i, int data) { - new UniqueIdentity { i = i, data = data }.Insert(); + ctx.Db.UniqueIdentity.Insert(new UniqueIdentity { i = i, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_identity(Identity i, int data) + public static void update_unique_identity(ReducerContext ctx, Identity i, int data) { var key = i; - UniqueIdentity.UpdateByi(key, new UniqueIdentity { i = i, data = data }); + ctx.Db.UniqueIdentity.UpdateByi(key, new UniqueIdentity { i = i, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_identity(Identity i) + public static void delete_unique_identity(ReducerContext ctx, Identity i) { - UniqueIdentity.DeleteByi(i); + ctx.Db.UniqueIdentity.DeleteByi(i); } [SpacetimeDB.Table(Public = true)] public partial struct UniqueAddress { - [SpacetimeDB.Column(ColumnAttrs.Unique)] + [SpacetimeDB.Unique] public Address a; public int data; } [SpacetimeDB.Reducer] - public static void insert_unique_address(Address a, int data) + public static void insert_unique_address(ReducerContext ctx, Address a, int data) { - new UniqueAddress { a = a, data = data }.Insert(); + ctx.Db.UniqueAddress.Insert(new UniqueAddress { a = a, data = data }); } [SpacetimeDB.Reducer] - public static void update_unique_address(Address a, int data) + public static void update_unique_address(ReducerContext ctx, Address a, int data) { var key = a; - UniqueAddress.UpdateBya(key, new UniqueAddress { a = a, data = data }); + ctx.Db.UniqueAddress.UpdateBya(key, new UniqueAddress { a = a, data = data }); } [SpacetimeDB.Reducer] - public static void delete_unique_address(Address a) + public static void delete_unique_address(ReducerContext ctx, Address a) { - UniqueAddress.DeleteBya(a); + ctx.Db.UniqueAddress.DeleteBya(a); } [SpacetimeDB.Table(Public = true)] public partial struct PkU8 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public byte n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_u8(byte n, int data) + public static void insert_pk_u8(ReducerContext ctx, byte n, int data) { - new PkU8 { n = n, data = data }.Insert(); + ctx.Db.PkU8.Insert(new PkU8 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_u8(byte n, int data) + public static void update_pk_u8(ReducerContext ctx, byte n, int data) { var key = n; - PkU8.UpdateByn(key, new PkU8 { n = n, data = data }); + ctx.Db.PkU8.UpdateByn(key, new PkU8 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_u8(byte n) + public static void delete_pk_u8(ReducerContext ctx, byte n) { - PkU8.DeleteByn(n); + ctx.Db.PkU8.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkU16 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public ushort n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_u16(ushort n, int data) + public static void insert_pk_u16(ReducerContext ctx, ushort n, int data) { - new PkU16 { n = n, data = data }.Insert(); + ctx.Db.PkU16.Insert(new PkU16 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_u16(ushort n, int data) + public static void update_pk_u16(ReducerContext ctx, ushort n, int data) { var key = n; - PkU16.UpdateByn(key, new PkU16 { n = n, data = data }); + ctx.Db.PkU16.UpdateByn(key, new PkU16 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_u16(ushort n) + public static void delete_pk_u16(ReducerContext ctx, ushort n) { - PkU16.DeleteByn(n); + ctx.Db.PkU16.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkU32 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public uint n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_u32(uint n, int data) + public static void insert_pk_u32(ReducerContext ctx, uint n, int data) { - new PkU32 { n = n, data = data }.Insert(); + ctx.Db.PkU32.Insert(new PkU32 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_u32(uint n, int data) + public static void update_pk_u32(ReducerContext ctx, uint n, int data) { var key = n; - PkU32.UpdateByn(key, new PkU32 { n = n, data = data }); + ctx.Db.PkU32.UpdateByn(key, new PkU32 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_u32(uint n) + public static void delete_pk_u32(ReducerContext ctx, uint n) { - PkU32.DeleteByn(n); + ctx.Db.PkU32.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkU64 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public ulong n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_u64(ulong n, int data) + public static void insert_pk_u64(ReducerContext ctx, ulong n, int data) { - new PkU64 { n = n, data = data }.Insert(); + ctx.Db.PkU64.Insert(new PkU64 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_u64(ulong n, int data) + public static void update_pk_u64(ReducerContext ctx, ulong n, int data) { var key = n; - PkU64.UpdateByn(key, new PkU64 { n = n, data = data }); + ctx.Db.PkU64.UpdateByn(key, new PkU64 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_u64(ulong n) + public static void delete_pk_u64(ReducerContext ctx, ulong n) { - PkU64.DeleteByn(n); + ctx.Db.PkU64.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkU128 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public U128 n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_u128(U128 n, int data) + public static void insert_pk_u128(ReducerContext ctx, U128 n, int data) { - new PkU128 { n = n, data = data }.Insert(); + ctx.Db.PkU128.Insert(new PkU128 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_u128(U128 n, int data) + public static void update_pk_u128(ReducerContext ctx, U128 n, int data) { var key = n; - PkU128.UpdateByn(key, new PkU128 { n = n, data = data }); + ctx.Db.PkU128.UpdateByn(key, new PkU128 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_u128(U128 n) + public static void delete_pk_u128(ReducerContext ctx, U128 n) { - PkU128.DeleteByn(n); + ctx.Db.PkU128.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkU256 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public U256 n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_u256(U256 n, int data) + public static void insert_pk_u256(ReducerContext ctx, U256 n, int data) { - new PkU256 { n = n, data = data }.Insert(); + ctx.Db.PkU256.Insert(new PkU256 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_u256(U256 n, int data) + public static void update_pk_u256(ReducerContext ctx, U256 n, int data) { var key = n; - PkU256.UpdateByn(key, new PkU256 { n = n, data = data }); + ctx.Db.PkU256.UpdateByn(key, new PkU256 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_u256(U256 n) + public static void delete_pk_u256(ReducerContext ctx, U256 n) { - PkU256.DeleteByn(n); + ctx.Db.PkU256.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkI8 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public sbyte n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_i8(sbyte n, int data) + public static void insert_pk_i8(ReducerContext ctx, sbyte n, int data) { - new PkI8 { n = n, data = data }.Insert(); + ctx.Db.PkI8.Insert(new PkI8 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_i8(sbyte n, int data) + public static void update_pk_i8(ReducerContext ctx, sbyte n, int data) { var key = n; - PkI8.UpdateByn(key, new PkI8 { n = n, data = data }); + ctx.Db.PkI8.UpdateByn(key, new PkI8 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_i8(sbyte n) + public static void delete_pk_i8(ReducerContext ctx, sbyte n) { - PkI8.DeleteByn(n); + ctx.Db.PkI8.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkI16 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public short n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_i16(short n, int data) + public static void insert_pk_i16(ReducerContext ctx, short n, int data) { - new PkI16 { n = n, data = data }.Insert(); + ctx.Db.PkI16.Insert(new PkI16 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_i16(short n, int data) + public static void update_pk_i16(ReducerContext ctx, short n, int data) { var key = n; - PkI16.UpdateByn(key, new PkI16 { n = n, data = data }); + ctx.Db.PkI16.UpdateByn(key, new PkI16 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_i16(short n) + public static void delete_pk_i16(ReducerContext ctx, short n) { - PkI16.DeleteByn(n); + ctx.Db.PkI16.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkI32 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public int n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_i32(int n, int data) + public static void insert_pk_i32(ReducerContext ctx, int n, int data) { - new PkI32 { n = n, data = data }.Insert(); + ctx.Db.PkI32.Insert(new PkI32 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_i32(int n, int data) + public static void update_pk_i32(ReducerContext ctx, int n, int data) { var key = n; - PkI32.UpdateByn(key, new PkI32 { n = n, data = data }); + ctx.Db.PkI32.UpdateByn(key, new PkI32 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_i32(int n) + public static void delete_pk_i32(ReducerContext ctx, int n) { - PkI32.DeleteByn(n); + ctx.Db.PkI32.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkI64 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public long n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_i64(long n, int data) + public static void insert_pk_i64(ReducerContext ctx, long n, int data) { - new PkI64 { n = n, data = data }.Insert(); + ctx.Db.PkI64.Insert(new PkI64 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_i64(long n, int data) + public static void update_pk_i64(ReducerContext ctx, long n, int data) { var key = n; - PkI64.UpdateByn(key, new PkI64 { n = n, data = data }); + ctx.Db.PkI64.UpdateByn(key, new PkI64 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_i64(long n) + public static void delete_pk_i64(ReducerContext ctx, long n) { - PkI64.DeleteByn(n); + ctx.Db.PkI64.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkI128 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public I128 n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_i128(I128 n, int data) + public static void insert_pk_i128(ReducerContext ctx, I128 n, int data) { - new PkI128 { n = n, data = data }.Insert(); + ctx.Db.PkI128.Insert(new PkI128 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_i128(I128 n, int data) + public static void update_pk_i128(ReducerContext ctx, I128 n, int data) { var key = n; - PkI128.UpdateByn(key, new PkI128 { n = n, data = data }); + ctx.Db.PkI128.UpdateByn(key, new PkI128 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_i128(I128 n) + public static void delete_pk_i128(ReducerContext ctx, I128 n) { - PkI128.DeleteByn(n); + ctx.Db.PkI128.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkI256 { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public I256 n; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_i256(I256 n, int data) + public static void insert_pk_i256(ReducerContext ctx, I256 n, int data) { - new PkI256 { n = n, data = data }.Insert(); + ctx.Db.PkI256.Insert(new PkI256 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_i256(I256 n, int data) + public static void update_pk_i256(ReducerContext ctx, I256 n, int data) { var key = n; - PkI256.UpdateByn(key, new PkI256 { n = n, data = data }); + ctx.Db.PkI256.UpdateByn(key, new PkI256 { n = n, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_i256(I256 n) + public static void delete_pk_i256(ReducerContext ctx, I256 n) { - PkI256.DeleteByn(n); + ctx.Db.PkI256.DeleteByn(n); } [SpacetimeDB.Table(Public = true)] public partial struct PkBool { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public bool b; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_bool(bool b, int data) + public static void insert_pk_bool(ReducerContext ctx, bool b, int data) { - new PkBool { b = b, data = data }.Insert(); + ctx.Db.PkBool.Insert(new PkBool { b = b, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_bool(bool b, int data) + public static void update_pk_bool(ReducerContext ctx, bool b, int data) { var key = b; - PkBool.UpdateByb(key, new PkBool { b = b, data = data }); + ctx.Db.PkBool.UpdateByb(key, new PkBool { b = b, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_bool(bool b) + public static void delete_pk_bool(ReducerContext ctx, bool b) { - PkBool.DeleteByb(b); + ctx.Db.PkBool.DeleteByb(b); } [SpacetimeDB.Table(Public = true)] public partial struct PkString { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public string s; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_string(string s, int data) + public static void insert_pk_string(ReducerContext ctx, string s, int data) { - new PkString { s = s, data = data }.Insert(); + ctx.Db.PkString.Insert(new PkString { s = s, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_string(string s, int data) + public static void update_pk_string(ReducerContext ctx, string s, int data) { var key = s; - PkString.UpdateBys(key, new PkString { s = s, data = data }); + ctx.Db.PkString.UpdateBys(key, new PkString { s = s, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_string(string s) + public static void delete_pk_string(ReducerContext ctx, string s) { - PkString.DeleteBys(s); + ctx.Db.PkString.DeleteBys(s); } [SpacetimeDB.Table(Public = true)] public partial struct PkIdentity { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public Identity i; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_identity(Identity i, int data) + public static void insert_pk_identity(ReducerContext ctx, Identity i, int data) { - new PkIdentity { i = i, data = data }.Insert(); + ctx.Db.PkIdentity.Insert(new PkIdentity { i = i, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_identity(Identity i, int data) + public static void update_pk_identity(ReducerContext ctx, Identity i, int data) { var key = i; - PkIdentity.UpdateByi(key, new PkIdentity { i = i, data = data }); + ctx.Db.PkIdentity.UpdateByi(key, new PkIdentity { i = i, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_identity(Identity i) + public static void delete_pk_identity(ReducerContext ctx, Identity i) { - PkIdentity.DeleteByi(i); + ctx.Db.PkIdentity.DeleteByi(i); } [SpacetimeDB.Table(Public = true)] public partial struct PkAddress { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKey)] + [SpacetimeDB.PrimaryKey] public Address a; public int data; } [SpacetimeDB.Reducer] - public static void insert_pk_address(Address a, int data) + public static void insert_pk_address(ReducerContext ctx, Address a, int data) { - new PkAddress { a = a, data = data }.Insert(); + ctx.Db.PkAddress.Insert(new PkAddress { a = a, data = data }); } [SpacetimeDB.Reducer] - public static void update_pk_address(Address a, int data) + public static void update_pk_address(ReducerContext ctx, Address a, int data) { var key = a; - PkAddress.UpdateBya(key, new PkAddress { a = a, data = data }); + ctx.Db.PkAddress.UpdateBya(key, new PkAddress { a = a, data = data }); } [SpacetimeDB.Reducer] - public static void delete_pk_address(Address a) + public static void delete_pk_address(ReducerContext ctx, Address a) { - PkAddress.DeleteBya(a); + ctx.Db.PkAddress.DeleteBya(a); } [SpacetimeDB.Reducer] public static void insert_caller_one_identity(ReducerContext ctx) { - new OneIdentity { i = ctx.Sender }.Insert(); + ctx.Db.OneIdentity.Insert(new OneIdentity { i = ctx.Sender }); } [SpacetimeDB.Reducer] public static void insert_caller_vec_identity(ReducerContext ctx) { - new VecIdentity { i = new List { ctx.Sender } }.Insert(); + ctx.Db.VecIdentity.Insert(new VecIdentity { i = new List { ctx.Sender } }); } [SpacetimeDB.Reducer] public static void insert_caller_unique_identity(ReducerContext ctx, int data) { - new UniqueIdentity { i = ctx.Sender, data = data }.Insert(); + ctx.Db.UniqueIdentity.Insert(new UniqueIdentity { i = ctx.Sender, data = data }); } [SpacetimeDB.Reducer] public static void insert_caller_pk_identity(ReducerContext ctx, int data) { - new PkIdentity { i = ctx.Sender, data = data }.Insert(); + ctx.Db.PkIdentity.Insert(new PkIdentity { i = ctx.Sender, data = data }); } [SpacetimeDB.Reducer] public static void insert_caller_one_address(ReducerContext ctx) { - new OneAddress { a = (Address)ctx.Address!, }.Insert(); + ctx.Db.OneAddress.Insert(new OneAddress { a = (Address)ctx.Address!, }); } [SpacetimeDB.Reducer] @@ -1648,13 +1650,13 @@ public static void insert_caller_vec_address(ReducerContext ctx) [SpacetimeDB.Reducer] public static void insert_caller_unique_address(ReducerContext ctx, int data) { - new UniqueAddress { a = (Address)ctx.Address!, data = data }.Insert(); + ctx.Db.UniqueAddress.Insert(new UniqueAddress { a = (Address)ctx.Address!, data = data }); } [SpacetimeDB.Reducer] public static void insert_caller_pk_address(ReducerContext ctx, int data) { - new PkAddress { a = (Address)ctx.Address!, data = data }.Insert(); + ctx.Db.PkAddress.Insert(new PkAddress { a = (Address)ctx.Address!, data = data }); } [SpacetimeDB.Table(Public = true)] @@ -1686,6 +1688,7 @@ public partial struct LargeTable [SpacetimeDB.Reducer] public static void insert_large_table( + ReducerContext ctx, byte a, ushort b, uint c, @@ -1710,8 +1713,7 @@ public static void insert_large_table( EveryVecStruct v ) { - new LargeTable - { + ctx.Db.LargeTable.Insert(new LargeTable { a = a, b = b, c = c, @@ -1734,19 +1736,18 @@ EveryVecStruct v t = t, u = u, v = v, - }.Insert(); + }); } [SpacetimeDB.Reducer] - public static void insert_primitives_as_strings(EveryPrimitiveStruct s) + public static void insert_primitives_as_strings(ReducerContext ctx, EveryPrimitiveStruct s) { - new VecString - { + ctx.Db.VecString.Insert(new VecString { s = typeof(EveryPrimitiveStruct) .GetFields() .Select(f => f.GetValue(s)!.ToString()!.ToLowerInvariant()) .ToList() - }.Insert(); + }); } [SpacetimeDB.Table(Public = true)] @@ -1757,11 +1758,11 @@ public partial struct TableHoldsTable } [SpacetimeDB.Reducer] - public static void insert_table_holds_table(OneU8 a, VecU8 b) + public static void insert_table_holds_table(ReducerContext ctx, OneU8 a, VecU8 b) { - new TableHoldsTable { a = a, b = b }.Insert(); + ctx.Db.TableHoldsTable.Insert(new TableHoldsTable { a = a, b = b }); } [SpacetimeDB.Reducer] - public static void no_op_succeeds() { } + public static void no_op_succeeds(ReducerContext ctx) { } } diff --git a/modules/sdk-test-cs/StdbModule.csproj b/modules/sdk-test-cs/sdk-test-cs.csproj similarity index 95% rename from modules/sdk-test-cs/StdbModule.csproj rename to modules/sdk-test-cs/sdk-test-cs.csproj index a4ec4b0f832..2b0ba60adf7 100644 --- a/modules/sdk-test-cs/StdbModule.csproj +++ b/modules/sdk-test-cs/sdk-test-cs.csproj @@ -1,6 +1,7 @@ + StdbModule net8.0 wasi-wasm enable diff --git a/modules/sdk-test-multi-cs/.gitignore b/modules/sdk-test-multi-cs/.gitignore new file mode 100644 index 00000000000..1746e3269ed --- /dev/null +++ b/modules/sdk-test-multi-cs/.gitignore @@ -0,0 +1,2 @@ +bin +obj diff --git a/modules/sdk-test-multi-cs/Lib.cs b/modules/sdk-test-multi-cs/Lib.cs new file mode 100644 index 00000000000..5162256f727 --- /dev/null +++ b/modules/sdk-test-multi-cs/Lib.cs @@ -0,0 +1,50 @@ +namespace SpacetimeDB.Sdk.Test.Multi; + +using SpacetimeDB; + +[Table(Public = true)] +partial struct User { + [AutoInc] + [PrimaryKey] + public ulong Id; + [Unique] + public Identity Owner; + public string Name; +} + +[Table(Name = "MyTable1", Public = true)] +[Table(Name = "MyTable2")] +partial struct MyTable { + public string Name; + + [AutoInc] + [PrimaryKey] + public uint Foo; + + [Unique(Table = "MyTable2")] + public uint Bar; +} + +static partial class Module +{ + [Reducer] + public static void AddUser(ReducerContext ctx, string name) { + Log.Info($"Hello, {name}"); + + ctx.Db.User.Insert(new User() { + Id = ulong.MaxValue, + Owner = ctx.Sender, + Name = name, + }); + } + + [Reducer] + public static void GreetAllUsers(ReducerContext ctx) + { + Log.Info("Hello All"); + foreach (var user in ctx.Db.User.Iter()) + { + Log.Info($"Hello, {user.Name}!"); + } + } +} \ No newline at end of file diff --git a/modules/sdk-test-multi-cs/README.md b/modules/sdk-test-multi-cs/README.md new file mode 100644 index 00000000000..fff94dde76c --- /dev/null +++ b/modules/sdk-test-multi-cs/README.md @@ -0,0 +1,3 @@ +This C# source code is manually derived from `../sdk-test-connect-disconnect/src/lib.rs` and is supposed to be functionally equivalent. + +Do not add new types or functionality here that is not present in the Rust version, because they're compared against each other. diff --git a/modules/spacetimedb-quickstart-cs/StdbModule.csproj b/modules/sdk-test-multi-cs/sdk-test-multi-cs.csproj similarity index 90% rename from modules/spacetimedb-quickstart-cs/StdbModule.csproj rename to modules/sdk-test-multi-cs/sdk-test-multi-cs.csproj index 7773fe10c08..8f459333e30 100644 --- a/modules/spacetimedb-quickstart-cs/StdbModule.csproj +++ b/modules/sdk-test-multi-cs/sdk-test-multi-cs.csproj @@ -1,6 +1,7 @@ - + + StdbModule net8.0 wasi-wasm enable diff --git a/modules/spacetimedb-quickstart-cs/Lib.cs b/modules/spacetimedb-quickstart-cs/Lib.cs index dabc9ab9775..e5efa37090c 100644 --- a/modules/spacetimedb-quickstart-cs/Lib.cs +++ b/modules/spacetimedb-quickstart-cs/Lib.cs @@ -1,36 +1,38 @@ +namespace SpacetimeDB.Examples.QuickStart.Server; + using SpacetimeDB; +[Table(Public = true)] +public partial struct Person { + [AutoInc] + [PrimaryKey] + public uint Id; + public string Name; + public byte Age; +} + static partial class Module { - [SpacetimeDB.Table(Public = true)] - public partial struct Person - { - [SpacetimeDB.Column(ColumnAttrs.PrimaryKeyAuto)] - public uint Id; - public string Name; - public byte Age; - } - - [SpacetimeDB.Reducer("add")] - public static void Add(string name, byte age) + [Reducer("add")] + public static void Add(ReducerContext ctx, string name, byte age) { - new Person { Name = name, Age = age }.Insert(); + ctx.Db.Person.Insert(new Person { Name = name, Age = age }); } - [SpacetimeDB.Reducer("say_hello")] - public static void SayHello() + [Reducer("say_hello")] + public static void SayHello(ReducerContext ctx) { - foreach (var person in Person.Iter()) + foreach (var person in ctx.Db.Person.Iter()) { Log.Info($"Hello, {person.Name}!"); } Log.Info("Hello, World!"); } - [SpacetimeDB.Reducer("list_over_age")] - public static void ListOverAge(byte age) + [Reducer("list_over_age")] + public static void ListOverAge(ReducerContext ctx, byte age) { - foreach (var person in Person.Query(person => person.Age >= age)) + foreach (var person in ctx.Db.Person.Query(person => person.Age >= age)) { Log.Info($"{person.Name} has age {person.Age} >= {age}"); } diff --git a/modules/spacetimedb-quickstart-cs/spacetimedb-quickstart-server-cs.csproj b/modules/spacetimedb-quickstart-cs/spacetimedb-quickstart-server-cs.csproj new file mode 100644 index 00000000000..8f459333e30 --- /dev/null +++ b/modules/spacetimedb-quickstart-cs/spacetimedb-quickstart-server-cs.csproj @@ -0,0 +1,21 @@ + + + + StdbModule + net8.0 + wasi-wasm + enable + enable + + + + + + + + +