Skip to content

Commit

Permalink
WIP new codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed May 15, 2024
1 parent 3854c08 commit 36bdc13
Show file tree
Hide file tree
Showing 32 changed files with 554 additions and 1,910 deletions.
2 changes: 1 addition & 1 deletion SpacetimeDB.ClientSDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.23.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<ProjectReference Include="../SpacetimeDB/crates/bindings-csharp/BSATN.Runtime/BSATN.Runtime.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions examples/quickstart/client/client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>10</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Protobuf" Version="3.23.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<ProjectReference Include="../../../SpacetimeDB.ClientSDK.csproj" />
<ProjectReference Include="../../../../SpacetimeDB/crates/bindings-csharp/BSATN.Codegen/BSATN.Codegen.csproj" OutputItemType="Analyzer" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 12 additions & 0 deletions examples/quickstart/client/client.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "client", "client.csproj", "
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpacetimeDB.ClientSDK", "..\..\..\SpacetimeDB.ClientSDK.csproj", "{242A8146-A58D-43E9-A2BD-31FFC6851AA6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codegen", "..\..\..\..\SpacetimeDB\crates\bindings-csharp\BSATN.Codegen\BSATN.Codegen.csproj", "{C7C0ED26-79E4-478B-8B15-71E2B078541D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Runtime", "..\..\..\..\SpacetimeDB\crates\bindings-csharp\BSATN.Runtime\BSATN.Runtime.csproj", "{BD1F3C9B-CEA0-4447-B754-4BF55D6C509F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +25,14 @@ Global
{242A8146-A58D-43E9-A2BD-31FFC6851AA6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{242A8146-A58D-43E9-A2BD-31FFC6851AA6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{242A8146-A58D-43E9-A2BD-31FFC6851AA6}.Release|Any CPU.Build.0 = Release|Any CPU
{C7C0ED26-79E4-478B-8B15-71E2B078541D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C7C0ED26-79E4-478B-8B15-71E2B078541D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C7C0ED26-79E4-478B-8B15-71E2B078541D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C7C0ED26-79E4-478B-8B15-71E2B078541D}.Release|Any CPU.Build.0 = Release|Any CPU
{BD1F3C9B-CEA0-4447-B754-4BF55D6C509F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BD1F3C9B-CEA0-4447-B754-4BF55D6C509F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BD1F3C9B-CEA0-4447-B754-4BF55D6C509F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BD1F3C9B-CEA0-4447-B754-4BF55D6C509F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
128 changes: 20 additions & 108 deletions examples/quickstart/client/module_bindings/Message.cs
Original file line number Diff line number Diff line change
@@ -1,131 +1,43 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
// <auto-generated />

#nullable enable

using System;
using System.Collections.Generic;
using SpacetimeDB;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;

namespace SpacetimeDB.Types
{
[Newtonsoft.Json.JsonObject(Newtonsoft.Json.MemberSerialization.OptIn)]
public partial class Message : IDatabaseTable
[SpacetimeDB.Type]
[DataContract]
public partial class Message : SpacetimeDB.DatabaseTable<Message, SpacetimeDB.Types.ReducerEvent>
{
[Newtonsoft.Json.JsonProperty("sender")]
public SpacetimeDB.Identity Sender;
[Newtonsoft.Json.JsonProperty("sent")]
[DataMember(Name = "sender")]
public SpacetimeDB.Identity Sender = new();
[DataMember(Name = "sent")]
public ulong Sent;
[Newtonsoft.Json.JsonProperty("text")]
public string Text;


private static void InternalOnValueInserted(object insertedValue)
{
var val = (Message)insertedValue;
}

private static void InternalOnValueDeleted(object deletedValue)
{
var val = (Message)deletedValue;
}
[DataMember(Name = "text")]
public string Text = "";

public static SpacetimeDB.SATS.AlgebraicType GetAlgebraicType()
public static IEnumerable<Message> FilterBySender(SpacetimeDB.Identity value)
{
return SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
{
new SpacetimeDB.SATS.ProductTypeElement("sender", SpacetimeDB.SATS.AlgebraicType.CreateProductType(new SpacetimeDB.SATS.ProductTypeElement[]
{
new SpacetimeDB.SATS.ProductTypeElement("__identity_bytes", SpacetimeDB.SATS.AlgebraicType.CreateArrayType(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U8))),
})),
new SpacetimeDB.SATS.ProductTypeElement("sent", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.U64)),
new SpacetimeDB.SATS.ProductTypeElement("text", SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String)),
});
return Query(x => x.Sender == value);
}

public static explicit operator Message(SpacetimeDB.SATS.AlgebraicValue value)
public static IEnumerable<Message> FilterBySent(ulong value)
{
if (value == null) {
return null;
}
var productValue = value.AsProductValue();
return new Message
{
Sender = SpacetimeDB.Identity.From(productValue.elements[0].AsProductValue().elements[0].AsBytes()),
Sent = productValue.elements[1].AsU64(),
Text = productValue.elements[2].AsString(),
};
return Query(x => x.Sent == value);
}

public static System.Collections.Generic.IEnumerable<Message> Iter()
{
foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Message"))
{
yield return (Message)entry.Item2;
}
}
public static int Count()
public static IEnumerable<Message> FilterByText(string value)
{
return SpacetimeDBClient.clientDB.Count("Message");
}
public static System.Collections.Generic.IEnumerable<Message> FilterBySender(SpacetimeDB.Identity value)
{
foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Message"))
{
var productValue = entry.Item1.AsProductValue();
var compareValue = Identity.From(productValue.elements[0].AsProductValue().elements[0].AsBytes());
if (compareValue == value) {
yield return (Message)entry.Item2;
}
}
return Query(x => x.Text == value);
}

public static System.Collections.Generic.IEnumerable<Message> FilterBySent(ulong value)
{
foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Message"))
{
var productValue = entry.Item1.AsProductValue();
var compareValue = (ulong)productValue.elements[1].AsU64();
if (compareValue == value) {
yield return (Message)entry.Item2;
}
}
}

public static System.Collections.Generic.IEnumerable<Message> FilterByText(string value)
{
foreach(var entry in SpacetimeDBClient.clientDB.GetEntries("Message"))
{
var productValue = entry.Item1.AsProductValue();
var compareValue = (string)productValue.elements[2].AsString();
if (compareValue == value) {
yield return (Message)entry.Item2;
}
}
}

public static bool ComparePrimaryKey(SpacetimeDB.SATS.AlgebraicType t, SpacetimeDB.SATS.AlgebraicValue _v1, SpacetimeDB.SATS.AlgebraicValue _v2)
{
return false;
}

public delegate void InsertEventHandler(Message insertedValue, SpacetimeDB.Types.ReducerEvent dbEvent);
public delegate void DeleteEventHandler(Message deletedValue, SpacetimeDB.Types.ReducerEvent dbEvent);
public static event InsertEventHandler OnInsert;
public static event DeleteEventHandler OnBeforeDelete;
public static event DeleteEventHandler OnDelete;

public static void OnInsertEvent(object newValue, ClientApi.Event dbEvent)
{
OnInsert?.Invoke((Message)newValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo);
}

public static void OnBeforeDeleteEvent(object oldValue, ClientApi.Event dbEvent)
{
OnBeforeDelete?.Invoke((Message)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo);
}

public static void OnDeleteEvent(object oldValue, ClientApi.Event dbEvent)
{
OnDelete?.Invoke((Message)oldValue,(ReducerEvent)dbEvent?.FunctionCall.CallInfo);
}
}
}
67 changes: 0 additions & 67 deletions examples/quickstart/client/module_bindings/ReducerEvent.cs

This file was deleted.

14 changes: 0 additions & 14 deletions examples/quickstart/client/module_bindings/ReducerJsonSettings.cs

This file was deleted.

62 changes: 22 additions & 40 deletions examples/quickstart/client/module_bindings/SendMessageReducer.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,43 @@
// THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE
// WILL NOT BE SAVED. MODIFY TABLES IN RUST INSTEAD.
// <auto-generated />

#nullable enable

using System;
using ClientApi;
using Newtonsoft.Json.Linq;
using SpacetimeDB;

namespace SpacetimeDB.Types
{
[SpacetimeDB.Type]
public partial class SendMessageArgsStruct : IReducerArgs
{
ReducerType IReducerArgs.ReducerType => ReducerType.SendMessage;
string IReducerArgsBase.ReducerName => "send_message";
bool IReducerArgs.InvokeHandler(ReducerEvent reducerEvent) => Reducer.OnSendMessage(reducerEvent, this);

public string Text = "";
}

public static partial class Reducer
{
public delegate void SendMessageHandler(ReducerEvent reducerEvent, string text);
public static event SendMessageHandler OnSendMessageEvent;
public static event SendMessageHandler? OnSendMessageEvent;

public static void SendMessage(string text)
{
var _argArray = new object[] {text};
var _message = new SpacetimeDBClient.ReducerCallRequest {
fn = "send_message",
args = _argArray,
};
SpacetimeDBClient.instance.InternalCallReducer(Newtonsoft.Json.JsonConvert.SerializeObject(_message, _settings));
SpacetimeDBClient.instance.InternalCallReducer(new SendMessageArgsStruct { Text = text });
}

[ReducerCallback(FunctionName = "send_message")]
public static bool OnSendMessage(ClientApi.Event dbEvent)
public static bool OnSendMessage(ReducerEvent reducerEvent, SendMessageArgsStruct args)
{
if(OnSendMessageEvent != null)
{
var args = ((ReducerEvent)dbEvent.FunctionCall.CallInfo).SendMessageArgs;
OnSendMessageEvent((ReducerEvent)dbEvent.FunctionCall.CallInfo
,(string)args.Text
);
return true;
}
return false;
if (OnSendMessageEvent == null) return false;
OnSendMessageEvent(
reducerEvent,
args.Text
);
return true;
}

[DeserializeEvent(FunctionName = "send_message")]
public static void SendMessageDeserializeEventArgs(ClientApi.Event dbEvent)
{
var args = new SendMessageArgsStruct();
var bsatnBytes = dbEvent.FunctionCall.ArgBytes;
using var ms = new System.IO.MemoryStream();
ms.SetLength(bsatnBytes.Length);
bsatnBytes.CopyTo(ms.GetBuffer(), 0);
ms.Position = 0;
using var reader = new System.IO.BinaryReader(ms);
var args_0_value = SpacetimeDB.SATS.AlgebraicValue.Deserialize(SpacetimeDB.SATS.AlgebraicType.CreatePrimitiveType(SpacetimeDB.SATS.BuiltinType.Type.String), reader);
args.Text = args_0_value.AsString();
dbEvent.FunctionCall.CallInfo = new ReducerEvent(ReducerType.SendMessage, "send_message", dbEvent.Timestamp, Identity.From(dbEvent.CallerIdentity.ToByteArray()), Address.From(dbEvent.CallerAddress.ToByteArray()), dbEvent.Message, dbEvent.Status, args);
}
}

public partial class SendMessageArgsStruct
{
public string Text;
}

}
Loading

0 comments on commit 36bdc13

Please sign in to comment.