diff --git a/examples~/quickstart/client/module_bindings/SendMessageReducer.cs b/examples~/quickstart/client/module_bindings/SendMessageReducer.cs deleted file mode 100644 index 15106671..00000000 --- a/examples~/quickstart/client/module_bindings/SendMessageReducer.cs +++ /dev/null @@ -1,19 +0,0 @@ -// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE -// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. -// - -#nullable enable - -using System; -using SpacetimeDB; - -namespace SpacetimeDB.Types -{ - [SpacetimeDB.Type] - public partial class SendMessage : IReducerArgs - { - string IReducerArgs.ReducerName => "send_message"; - - public string Text = ""; - } -} diff --git a/examples~/quickstart/client/module_bindings/SetNameReducer.cs b/examples~/quickstart/client/module_bindings/SetNameReducer.cs deleted file mode 100644 index 83a0b728..00000000 --- a/examples~/quickstart/client/module_bindings/SetNameReducer.cs +++ /dev/null @@ -1,19 +0,0 @@ -// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE -// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD. -// - -#nullable enable - -using System; -using SpacetimeDB; - -namespace SpacetimeDB.Types -{ - [SpacetimeDB.Type] - public partial class SetName : IReducerArgs - { - string IReducerArgs.ReducerName => "set_name"; - - public string Name = ""; - } -} diff --git a/examples~/quickstart/client/module_bindings/_Globals/SpacetimeDBClient.cs b/examples~/quickstart/client/module_bindings/_Globals/SpacetimeDBClient.cs index 726f56d0..031c02f0 100644 --- a/examples~/quickstart/client/module_bindings/_Globals/SpacetimeDBClient.cs +++ b/examples~/quickstart/client/module_bindings/_Globals/SpacetimeDBClient.cs @@ -8,6 +8,7 @@ using SpacetimeDB; using SpacetimeDB.ClientApi; using System.Collections.Generic; +using System.Runtime.Serialization; namespace SpacetimeDB.Types { @@ -25,30 +26,30 @@ internal MessageHandle() public class UserHandle : RemoteTableHandle { - private static Dictionary Identity_Index = new(16); public override void InternalInvokeValueInserted(IDatabaseRow row) { var value = (User)row; - Identity_Index[value.Identity] = value; + Identity.Cache[value.Identity] = value; } public override void InternalInvokeValueDeleted(IDatabaseRow row) { - Identity_Index.Remove(((User)row).Identity); + Identity.Cache.Remove(((User)row).Identity); } - public readonly ref struct IdentityUniqueIndex + public class IdentityUniqueIndex { + internal readonly Dictionary Cache = new(16); public User? Find(SpacetimeDB.Identity value) { - Identity_Index.TryGetValue(value, out var r); + Cache.TryGetValue(value, out var r); return r; } } - public IdentityUniqueIndex Identity => new(); + public IdentityUniqueIndex Identity = new(); internal UserHandle() { @@ -70,10 +71,10 @@ public sealed class RemoteReducers : RemoteBase public void SendMessage(string text) { - conn.InternalCallReducer(new SendMessage { Text = text }, this.SetCallReducerFlags.SendMessageFlags); + conn.InternalCallReducer(new Reducer.SendMessage(text), this.SetCallReducerFlags.SendMessageFlags); } - public bool InvokeSendMessage(EventContext ctx, SendMessage args) + public bool InvokeSendMessage(EventContext ctx, Reducer.SendMessage args) { if (OnSendMessage == null) return false; OnSendMessage( @@ -87,10 +88,10 @@ public bool InvokeSendMessage(EventContext ctx, SendMessage args) public void SetName(string name) { - conn.InternalCallReducer(new SetName { Name = name }, this.SetCallReducerFlags.SetNameFlags); + conn.InternalCallReducer(new Reducer.SetName(name), this.SetCallReducerFlags.SetNameFlags); } - public bool InvokeSetName(EventContext ctx, SetName args) + public bool InvokeSetName(EventContext ctx, Reducer.SetName args) { if (OnSetName == null) return false; OnSetName( @@ -124,14 +125,55 @@ internal EventContext(DbConnection conn, Event reducerEvent) : base(con } } - [Type] - public partial record Reducer : TaggedEnum<( - SendMessage SendMessage, - SetName SetName, - Unit StdbNone, - Unit StdbIdentityConnected, - Unit StdbIdentityDisconnected - )>; + public abstract partial class Reducer + { + private Reducer() { } + + [SpacetimeDB.Type] + [DataContract] + public partial class SendMessage : Reducer, IReducerArgs + { + [DataMember(Name = "text")] + public string Text; + + public SendMessage(string Text) + { + this.Text = Text; + } + + public SendMessage() + { + this.Text = ""; + } + + string IReducerArgs.ReducerName => "send_message"; + } + + [SpacetimeDB.Type] + [DataContract] + public partial class SetName : Reducer, IReducerArgs + { + [DataMember(Name = "name")] + public string Name; + + public SetName(string Name) + { + this.Name = Name; + } + + public SetName() + { + this.Name = ""; + } + + string IReducerArgs.ReducerName => "set_name"; + } + + public class StdbNone : Reducer {} + public class StdbIdentityConnected : Reducer {} + public class StdbIdentityDisconnected : Reducer {} + } + public class DbConnection : DbConnectionBase { public readonly RemoteTables Db = new(); @@ -150,14 +192,13 @@ public DbConnection() protected override Reducer ToReducer(TransactionUpdate update) { var encodedArgs = update.ReducerCall.Args; - return update.ReducerCall.ReducerName switch - { - "send_message" => new Reducer.SendMessage(BSATNHelpers.Decode(encodedArgs)), - "set_name" => new Reducer.SetName(BSATNHelpers.Decode(encodedArgs)), - "" => new Reducer.StdbNone(default), - "__identity_connected__" => new Reducer.StdbIdentityConnected(default), - "__identity_disconnected__" => new Reducer.StdbIdentityDisconnected(default), - "" => new Reducer.StdbNone(default), + return update.ReducerCall.ReducerName switch { + "send_message" => BSATNHelpers.Decode(encodedArgs), + "set_name" => BSATNHelpers.Decode(encodedArgs), + "" => new Reducer.StdbNone(), + "__identity_connected__" => new Reducer.StdbIdentityConnected(), + "__identity_disconnected__" => new Reducer.StdbIdentityDisconnected(), + "" => new Reducer.StdbNone(), var reducer => throw new ArgumentOutOfRangeException("Reducer", $"Unknown reducer {reducer}") }; } @@ -168,10 +209,9 @@ protected override IEventContext ToEventContext(Event reducerEvent) => protected override bool Dispatch(IEventContext context, Reducer reducer) { var eventContext = (EventContext)context; - return reducer switch - { - Reducer.SendMessage(var args) => Reducers.InvokeSendMessage(eventContext, args), - Reducer.SetName(var args) => Reducers.InvokeSetName(eventContext, args), + return reducer switch { + Reducer.SendMessage args => Reducers.InvokeSendMessage(eventContext, args), + Reducer.SetName args => Reducers.InvokeSetName(eventContext, args), Reducer.StdbNone or Reducer.StdbIdentityConnected or Reducer.StdbIdentityDisconnected => true, diff --git a/tests~/SnapshotTests.VerifyAllTablesParsed.verified.txt b/tests~/SnapshotTests.VerifyAllTablesParsed.verified.txt index e3ae0cdc..2ab0a7db 100644 --- a/tests~/SnapshotTests.VerifyAllTablesParsed.verified.txt +++ b/tests~/SnapshotTests.VerifyAllTablesParsed.verified.txt @@ -9,7 +9,9 @@ eventContext: { Reducers: {Scrubbed}, SetReducerFlags: {}, - Event: {}, + Event: { + $type: Event.SubscribeApplied + }, Db: {Scrubbed} }, user: { @@ -23,13 +25,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_1, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_2, CallerAddress: Guid_1, EnergyConsumed: {}, - Reducer: {} + Reducer: { + $type: Reducer.StdbIdentityConnected + } } }, Db: {Scrubbed} @@ -44,16 +51,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_2, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_1, CallerAddress: Guid_2, EnergyConsumed: {}, Reducer: { - SetName_: { - Name: A - } + $type: Reducer.SetName, + name: A } } }, @@ -73,16 +82,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_2, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_1, CallerAddress: Guid_2, EnergyConsumed: {}, Reducer: { - SetName_: { - Name: A - } + $type: Reducer.SetName, + name: A } } }, @@ -93,16 +104,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_3, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_2, CallerAddress: Guid_1, EnergyConsumed: {}, Reducer: { - SendMessage_: { - Text: Hello, A! - } + $type: Reducer.SendMessage, + text: Hello, A! } } }, @@ -118,16 +131,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_3, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_2, CallerAddress: Guid_1, EnergyConsumed: {}, Reducer: { - SendMessage_: { - Text: Hello, A! - } + $type: Reducer.SendMessage, + text: Hello, A! } } }, @@ -138,16 +153,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_4, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_2, CallerAddress: Guid_1, EnergyConsumed: {}, Reducer: { - SetName_: { - Name: B - } + $type: Reducer.SetName, + name: B } } }, @@ -167,16 +184,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_4, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_2, CallerAddress: Guid_1, EnergyConsumed: {}, Reducer: { - SetName_: { - Name: B - } + $type: Reducer.SetName, + name: B } } }, @@ -187,16 +206,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_5, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_1, CallerAddress: Guid_2, EnergyConsumed: {}, Reducer: { - SendMessage_: { - Text: Hello, B! - } + $type: Reducer.SendMessage, + text: Hello, B! } } }, @@ -212,16 +233,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_5, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_1, CallerAddress: Guid_2, EnergyConsumed: {}, Reducer: { - SendMessage_: { - Text: Hello, B! - } + $type: Reducer.SendMessage, + text: Hello, B! } } }, @@ -232,16 +255,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_6, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_2, CallerAddress: Guid_1, EnergyConsumed: {}, Reducer: { - SendMessage_: { - Text: Goodbye! - } + $type: Reducer.SendMessage, + text: Goodbye! } } }, @@ -257,16 +282,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_6, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_2, CallerAddress: Guid_1, EnergyConsumed: {}, Reducer: { - SendMessage_: { - Text: Goodbye! - } + $type: Reducer.SendMessage, + text: Goodbye! } } }, @@ -277,13 +304,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_7, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_2, CallerAddress: Guid_1, EnergyConsumed: {}, - Reducer: {} + Reducer: { + $type: Reducer.StdbIdentityDisconnected + } } }, Db: {Scrubbed} @@ -304,16 +336,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_8, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_1, CallerAddress: Guid_2, EnergyConsumed: {}, Reducer: { - SendMessage_: { - Text: Goodbye! - } + $type: Reducer.SendMessage, + text: Goodbye! } } }, @@ -329,16 +363,18 @@ Reducers: {Scrubbed}, SetReducerFlags: {}, Event: { + $type: Event.Reducer, ReducerEvent: { Timestamp: DateTimeOffset_8, - Status: {}, + Status: { + $type: Status.Committed + }, CallerIdentity: Identity_1, CallerAddress: Guid_2, EnergyConsumed: {}, Reducer: { - SendMessage_: { - Text: Goodbye! - } + $type: Reducer.SendMessage, + text: Goodbye! } } }, @@ -405,4 +441,4 @@ Max: type=InitialSubscription } } -} +} \ No newline at end of file diff --git a/tests~/SnapshotTests.cs b/tests~/SnapshotTests.cs index d293c126..d57379b5 100644 --- a/tests~/SnapshotTests.cs +++ b/tests~/SnapshotTests.cs @@ -238,27 +238,27 @@ private static ServerMessage[] SampleDump() => [ SampleTransactionUpdate( 1718487768057579, "j5DMlKmWjfbSl7qmZQOok7HDSwsAJopRSJjdlUsNogs=", "Vd4dFzcEzhLHJ6uNL8VXFg==", 1, "set_name", 4345615, 70, [SampleUserUpdate("j5DMlKmWjfbSl7qmZQOok7HDSwsAJopRSJjdlUsNogs=", null, "A", true, true)], - Encode(new SetName { Name = "A" }) + Encode(new Reducer.SetName { Name = "A" }) ), SampleTransactionUpdate( 1718487775346381, "l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=", "Kwmeu5riP20rvCTNbBipLA==", 1, "send_message", 2779615, 57, [SampleMessage("l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=", 1718487775346381, "Hello, A!")], - Encode(new SendMessage { Text = "Hello, A!" }) + Encode(new Reducer.SendMessage { Text = "Hello, A!" }) ), SampleTransactionUpdate( 1718487777307855, "l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=", "Kwmeu5riP20rvCTNbBipLA==", 2, "set_name", 4268615, 98, [SampleUserUpdate("l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=", null, "B", true, true)], - Encode(new SetName { Name = "B" }) + Encode(new Reducer.SetName { Name = "B" }) ), SampleTransactionUpdate( 1718487783175083, "j5DMlKmWjfbSl7qmZQOok7HDSwsAJopRSJjdlUsNogs=", "Vd4dFzcEzhLHJ6uNL8VXFg==", 2, "send_message", 2677615, 40, [SampleMessage("j5DMlKmWjfbSl7qmZQOok7HDSwsAJopRSJjdlUsNogs=", 1718487783175083, "Hello, B!")], - Encode(new SendMessage { Text = "Hello, B!" }) + Encode(new Reducer.SendMessage { Text = "Hello, B!" }) ), SampleTransactionUpdate( 1718487787645364, "l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=", "Kwmeu5riP20rvCTNbBipLA==", 3, "send_message", 2636615, 28, [SampleMessage("l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=", 1718487787645364, "Goodbye!")], - Encode(new SendMessage { Text = "Goodbye!" }) + Encode(new Reducer.SendMessage { Text = "Goodbye!" }) ), SampleTransactionUpdate( 1718487791901504, "l0qzG1GPRtC1mwr+54q98tv0325gozLc6cNzq4vrzqY=", "Kwmeu5riP20rvCTNbBipLA==", @@ -268,7 +268,7 @@ private static ServerMessage[] SampleDump() => [ SampleTransactionUpdate( 1718487794937841, "j5DMlKmWjfbSl7qmZQOok7HDSwsAJopRSJjdlUsNogs=", "Vd4dFzcEzhLHJ6uNL8VXFg==", 3, "send_message", 2636615, 34, [SampleMessage("j5DMlKmWjfbSl7qmZQOok7HDSwsAJopRSJjdlUsNogs=", 1718487794937841, "Goodbye!")], - Encode(new SendMessage { Text = "Goodbye!" }) + Encode(new Reducer.SendMessage { Text = "Goodbye!" }) ), ]; diff --git a/tests~/VerifyInit.cs b/tests~/VerifyInit.cs index 58d37d65..9d8249d9 100644 --- a/tests~/VerifyInit.cs +++ b/tests~/VerifyInit.cs @@ -1,6 +1,7 @@ namespace SpacetimeDB.Tests; using System.Runtime.CompilerServices; +using Argon; using SpacetimeDB.Types; static class VerifyInit @@ -73,13 +74,15 @@ public static void Init() Environment.SetEnvironmentVariable("DiffEngine_TargetOnLeft", "true"); VerifierSettings.AddExtraSettings(settings => + { settings.Converters.AddRange( [ new IdentityConverter(), new AddressConverter(), new NetworkRequestTrackerConverter() ] - ) - ); + ); + settings.TypeNameHandling = TypeNameHandling.Auto; + }); } }