From 732dd2872b501ef3d41060ac9da06cb8d7f07226 Mon Sep 17 00:00:00 2001 From: Zeke Foppa <196249+bfops@users.noreply.github.com> Date: Thu, 29 Aug 2024 09:21:48 -0700 Subject: [PATCH] Revert `master` to a stable state (#123) ## Description of Changes Revert `master` to the original state of this PR: https://github.com/clockworklabs/com.clockworklabs.spacetimedbsdk/issues/53. We should prevent merging into `master` from now on, since the repositories are now merged. ## API - [ ] This is an API breaking change to the SDK *If the API is breaking, please state below what will break* ## Requires SpacetimeDB PRs *List any PRs here that are required for this SDK change to work* ## Testing I loaded a bitcraft world and ran around. I used the versions listed here: https://github.com/clockworklabs/com.clockworklabs.spacetimedbsdk-archive/pull/57 --------- Co-authored-by: Ingvar Stepanyan Co-authored-by: Zeke Foppa --- DEVELOP.md | 17 +++- SpacetimeDB.ClientSDK.csproj | 5 +- src/Primitives.cs | 83 +++++++++++++++++++ src/SpacetimeDB/ClientApi/CallReducer.cs | 5 +- src/SpacetimeDB/ClientApi/ClientMessage.cs | 1 - src/SpacetimeDB/ClientApi/DatabaseUpdate.cs | 3 +- src/SpacetimeDB/ClientApi/EnergyQuanta.cs | 3 +- src/SpacetimeDB/ClientApi/IdentityToken.cs | 5 +- .../ClientApi/InitialSubscription.cs | 5 +- src/SpacetimeDB/ClientApi/OneOffQuery.cs | 4 +- .../ClientApi/OneOffQueryResponse.cs | 8 +- src/SpacetimeDB/ClientApi/OneOffTable.cs | 6 +- src/SpacetimeDB/ClientApi/ReducerCallInfo.cs | 6 +- src/SpacetimeDB/ClientApi/ServerMessage.cs | 1 - src/SpacetimeDB/ClientApi/Subscribe.cs | 4 +- src/SpacetimeDB/ClientApi/TableUpdate.cs | 10 ++- .../ClientApi/TransactionUpdate.cs | 7 +- tests~/SnapshotTests.cs | 16 +++- tools/gen-client-api.bat | 20 ----- tools/gen-client-api.sh | 15 ---- 20 files changed, 151 insertions(+), 73 deletions(-) create mode 100644 src/Primitives.cs delete mode 100644 tools/gen-client-api.bat delete mode 100644 tools/gen-client-api.sh diff --git a/DEVELOP.md b/DEVELOP.md index 4f24f4d6..de08bc70 100644 --- a/DEVELOP.md +++ b/DEVELOP.md @@ -1,6 +1,17 @@ # Notes for maintainers -## `SpacetimeDB.ClientApi` +## `ClientApi.cs` -To regenerate this namespace, run the `tools/gen-client-api.sh` or the -`tools/gen-client-api.bat` script. +The file `ClientApi.cs` is generated by [ProtoBuf](https://protobuf.dev/) +from [the SpacetimeDB client-api-messages proto definition](https://github.com/clockworklabs/SpacetimeDB/blob/master/crates/client-api-messages/protobuf/client_api.proto). +This is not automated. +Whenever the `client_api.proto` changes, you'll have to manually re-run `protoc` to re-generate the definitions. + +```sh +cd ~/clockworklabs/SpacetimeDB/crates/client-api-messages/protobuf +protoc --csharp_out=/absolute/path/to/spacetimedb-csharp-sdk/src \ + ./client_api.proto +``` + +Note that `protoc` cannot understand paths that start with `~`; +you must write the absolute path starting from `/`. diff --git a/SpacetimeDB.ClientSDK.csproj b/SpacetimeDB.ClientSDK.csproj index 1c2158b2..72a34580 100644 --- a/SpacetimeDB.ClientSDK.csproj +++ b/SpacetimeDB.ClientSDK.csproj @@ -5,6 +5,7 @@ 9 disable enable + True SpacetimeDB.ClientSDK SpacetimeDB SDK jdetter @@ -16,7 +17,7 @@ logo.png README.md https://github.com/clockworklabs/spacetimedb-csharp-sdk - 0.12.0 + 0.11.0 $(AssemblyVersion) $(DefaultItemExcludes);*~/** @@ -24,7 +25,7 @@ - + diff --git a/src/Primitives.cs b/src/Primitives.cs new file mode 100644 index 00000000..9466cca5 --- /dev/null +++ b/src/Primitives.cs @@ -0,0 +1,83 @@ +using SpacetimeDB.BSATN; + +using System; +using System.IO; + +namespace SpacetimeDB +{ + + public struct I128 : IEquatable + { + public long hi; + public ulong lo; + + public I128(long hi, ulong lo) + { + this.hi = hi; + this.lo = lo; + } + + public readonly bool Equals(I128 x) => hi == x.hi && lo == x.lo; + + public override readonly bool Equals(object? o) => o is I128 x && Equals(x); + + public static bool operator ==(I128 a, I128 b) => a.Equals(b); + public static bool operator !=(I128 a, I128 b) => !a.Equals(b); + + public override readonly int GetHashCode() => hi.GetHashCode() ^ lo.GetHashCode(); + + public override readonly string ToString() => $"I128({hi},{lo})"; + + public readonly struct BSATN : IReadWrite + { + public I128 Read(BinaryReader reader) => new(reader.ReadInt64(), reader.ReadUInt64()); + + public void Write(BinaryWriter writer, I128 value) + { + writer.Write(value.hi); + writer.Write(value.lo); + } + + public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) => + new AlgebraicType.Builtin(new BuiltinType.I128(new Unit())); + } + } + + public struct U128 : IEquatable + { + public ulong hi; + public ulong lo; + + public U128(ulong hi, ulong lo) + { + this.lo = lo; + this.hi = hi; + } + + public readonly bool Equals(U128 x) => hi == x.hi && lo == x.lo; + + public override readonly bool Equals(object? o) => o is U128 x && Equals(x); + + public static bool operator ==(U128 a, U128 b) => a.Equals(b); + public static bool operator !=(U128 a, U128 b) => !a.Equals(b); + + public override readonly int GetHashCode() => hi.GetHashCode() ^ lo.GetHashCode(); + + public override readonly string ToString() => $"U128({hi},{lo})"; + + public readonly struct BSATN : IReadWrite + { + public U128 Read(BinaryReader reader) => new(reader.ReadUInt64(), reader.ReadUInt64()); + + public void Write(BinaryWriter writer, U128 value) + { + writer.Write(value.hi); + writer.Write(value.lo); + } + + public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) => + new AlgebraicType.Builtin(new BuiltinType.U128(new Unit())); + } + } + +} diff --git a/src/SpacetimeDB/ClientApi/CallReducer.cs b/src/SpacetimeDB/ClientApi/CallReducer.cs index d3f56f38..0d065d40 100644 --- a/src/SpacetimeDB/ClientApi/CallReducer.cs +++ b/src/SpacetimeDB/ClientApi/CallReducer.cs @@ -12,16 +12,17 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class CallReducer { [DataMember(Name = "reducer")] public string Reducer = ""; + [DataMember(Name = "args")] public SpacetimeDB.ClientApi.EncodedValue Args = null!; + [DataMember(Name = "request_id")] public uint RequestId; - } } diff --git a/src/SpacetimeDB/ClientApi/ClientMessage.cs b/src/SpacetimeDB/ClientApi/ClientMessage.cs index 9cb1c4c7..4badf680 100644 --- a/src/SpacetimeDB/ClientApi/ClientMessage.cs +++ b/src/SpacetimeDB/ClientApi/ClientMessage.cs @@ -5,7 +5,6 @@ #nullable enable using System; -using SpacetimeDB; namespace SpacetimeDB.ClientApi { diff --git a/src/SpacetimeDB/ClientApi/DatabaseUpdate.cs b/src/SpacetimeDB/ClientApi/DatabaseUpdate.cs index 245117ad..0cf2567e 100644 --- a/src/SpacetimeDB/ClientApi/DatabaseUpdate.cs +++ b/src/SpacetimeDB/ClientApi/DatabaseUpdate.cs @@ -12,12 +12,11 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class DatabaseUpdate { [DataMember(Name = "tables")] public System.Collections.Generic.List Tables = new(); - } } diff --git a/src/SpacetimeDB/ClientApi/EnergyQuanta.cs b/src/SpacetimeDB/ClientApi/EnergyQuanta.cs index ba142cb3..7cd07b36 100644 --- a/src/SpacetimeDB/ClientApi/EnergyQuanta.cs +++ b/src/SpacetimeDB/ClientApi/EnergyQuanta.cs @@ -12,12 +12,11 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class EnergyQuanta { [DataMember(Name = "quanta")] public U128 Quanta; - } } diff --git a/src/SpacetimeDB/ClientApi/IdentityToken.cs b/src/SpacetimeDB/ClientApi/IdentityToken.cs index 344cbbba..b1bbd183 100644 --- a/src/SpacetimeDB/ClientApi/IdentityToken.cs +++ b/src/SpacetimeDB/ClientApi/IdentityToken.cs @@ -12,16 +12,17 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class IdentityToken { [DataMember(Name = "identity")] public SpacetimeDB.Identity Identity = new(); + [DataMember(Name = "token")] public string Token = ""; + [DataMember(Name = "address")] public SpacetimeDB.Address Address = new(); - } } diff --git a/src/SpacetimeDB/ClientApi/InitialSubscription.cs b/src/SpacetimeDB/ClientApi/InitialSubscription.cs index 06ae7ba8..ed704238 100644 --- a/src/SpacetimeDB/ClientApi/InitialSubscription.cs +++ b/src/SpacetimeDB/ClientApi/InitialSubscription.cs @@ -12,16 +12,17 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class InitialSubscription { [DataMember(Name = "database_update")] public SpacetimeDB.ClientApi.DatabaseUpdate DatabaseUpdate = new(); + [DataMember(Name = "request_id")] public uint RequestId; + [DataMember(Name = "total_host_execution_duration_micros")] public ulong TotalHostExecutionDurationMicros; - } } diff --git a/src/SpacetimeDB/ClientApi/OneOffQuery.cs b/src/SpacetimeDB/ClientApi/OneOffQuery.cs index c103c829..c4625fe5 100644 --- a/src/SpacetimeDB/ClientApi/OneOffQuery.cs +++ b/src/SpacetimeDB/ClientApi/OneOffQuery.cs @@ -12,14 +12,14 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class OneOffQuery { [DataMember(Name = "message_id")] public byte[] MessageId = Array.Empty(); + [DataMember(Name = "query_string")] public string QueryString = ""; - } } diff --git a/src/SpacetimeDB/ClientApi/OneOffQueryResponse.cs b/src/SpacetimeDB/ClientApi/OneOffQueryResponse.cs index 35c1d0eb..4e98289f 100644 --- a/src/SpacetimeDB/ClientApi/OneOffQueryResponse.cs +++ b/src/SpacetimeDB/ClientApi/OneOffQueryResponse.cs @@ -12,18 +12,20 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class OneOffQueryResponse { [DataMember(Name = "message_id")] public byte[] MessageId = Array.Empty(); + [DataMember(Name = "error")] public string? Error; + [DataMember(Name = "tables")] - public System.Collections.Generic.List Tables = new(); + public List Tables = new(); + [DataMember(Name = "total_host_execution_duration_micros")] public ulong TotalHostExecutionDurationMicros; - } } diff --git a/src/SpacetimeDB/ClientApi/OneOffTable.cs b/src/SpacetimeDB/ClientApi/OneOffTable.cs index 12f7e0f9..84fa81bf 100644 --- a/src/SpacetimeDB/ClientApi/OneOffTable.cs +++ b/src/SpacetimeDB/ClientApi/OneOffTable.cs @@ -12,14 +12,14 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class OneOffTable { [DataMember(Name = "table_name")] public string TableName = ""; - [DataMember(Name = "rows")] - public System.Collections.Generic.List Rows = new(); + [DataMember(Name = "rows")] + public List Rows = new(); } } diff --git a/src/SpacetimeDB/ClientApi/ReducerCallInfo.cs b/src/SpacetimeDB/ClientApi/ReducerCallInfo.cs index 44bc8865..86a67416 100644 --- a/src/SpacetimeDB/ClientApi/ReducerCallInfo.cs +++ b/src/SpacetimeDB/ClientApi/ReducerCallInfo.cs @@ -12,18 +12,20 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class ReducerCallInfo { [DataMember(Name = "reducer_name")] public string ReducerName = ""; + [DataMember(Name = "reducer_id")] public uint ReducerId; + [DataMember(Name = "args")] public SpacetimeDB.ClientApi.EncodedValue Args = null!; + [DataMember(Name = "request_id")] public uint RequestId; - } } diff --git a/src/SpacetimeDB/ClientApi/ServerMessage.cs b/src/SpacetimeDB/ClientApi/ServerMessage.cs index 46b5be3a..7f4bf714 100644 --- a/src/SpacetimeDB/ClientApi/ServerMessage.cs +++ b/src/SpacetimeDB/ClientApi/ServerMessage.cs @@ -5,7 +5,6 @@ #nullable enable using System; -using SpacetimeDB; namespace SpacetimeDB.ClientApi { diff --git a/src/SpacetimeDB/ClientApi/Subscribe.cs b/src/SpacetimeDB/ClientApi/Subscribe.cs index d5eaba81..e844a347 100644 --- a/src/SpacetimeDB/ClientApi/Subscribe.cs +++ b/src/SpacetimeDB/ClientApi/Subscribe.cs @@ -12,14 +12,14 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class Subscribe { [DataMember(Name = "query_strings")] public System.Collections.Generic.List QueryStrings = new(); + [DataMember(Name = "request_id")] public uint RequestId; - } } diff --git a/src/SpacetimeDB/ClientApi/TableUpdate.cs b/src/SpacetimeDB/ClientApi/TableUpdate.cs index b9b47ea5..63400ee6 100644 --- a/src/SpacetimeDB/ClientApi/TableUpdate.cs +++ b/src/SpacetimeDB/ClientApi/TableUpdate.cs @@ -12,18 +12,20 @@ namespace SpacetimeDB.ClientApi { - [SpacetimeDB.Type] [DataContract] + [SpacetimeDB.Type] public partial class TableUpdate { [DataMember(Name = "table_id")] public uint TableId; + [DataMember(Name = "table_name")] public string TableName = ""; + [DataMember(Name = "deletes")] - public System.Collections.Generic.List Deletes = new(); - [DataMember(Name = "inserts")] - public System.Collections.Generic.List Inserts = new(); + public List Deletes = new(); + [DataMember(Name = "inserts")] + public List Inserts = new(); } } diff --git a/src/SpacetimeDB/ClientApi/TransactionUpdate.cs b/src/SpacetimeDB/ClientApi/TransactionUpdate.cs index fb71dd9e..5861e9ee 100644 --- a/src/SpacetimeDB/ClientApi/TransactionUpdate.cs +++ b/src/SpacetimeDB/ClientApi/TransactionUpdate.cs @@ -18,18 +18,23 @@ public partial class TransactionUpdate { [DataMember(Name = "status")] public SpacetimeDB.ClientApi.UpdateStatus Status = null!; + [DataMember(Name = "timestamp")] public SpacetimeDB.ClientApi.Timestamp Timestamp = new(); + [DataMember(Name = "caller_identity")] public SpacetimeDB.Identity CallerIdentity = new(); + [DataMember(Name = "caller_address")] public SpacetimeDB.Address CallerAddress = new(); + [DataMember(Name = "reducer_call")] public SpacetimeDB.ClientApi.ReducerCallInfo ReducerCall = new(); + [DataMember(Name = "energy_quanta_used")] public SpacetimeDB.ClientApi.EnergyQuanta EnergyQuantaUsed = new(); + [DataMember(Name = "host_execution_duration_micros")] public ulong HostExecutionDurationMicros; - } } diff --git a/tests~/SnapshotTests.cs b/tests~/SnapshotTests.cs index af4364c1..8f722b3c 100644 --- a/tests~/SnapshotTests.cs +++ b/tests~/SnapshotTests.cs @@ -43,15 +43,23 @@ class EnergyQuantaConverter : WriteOnlyJsonConverter { public override void Write(VerifyJsonWriter writer, EnergyQuanta value) { - writer.WriteRawValueIfNoStrict(value.Quanta.ToString()); + Assert.Equal(0uL, value.Quanta.hi); + writer.WriteValue(value.Quanta.lo); } } - class EncodedValueConverter : WriteOnlyJsonConverter + class EncodedValueConverter : WriteOnlyJsonConverter { - public override void Write(VerifyJsonWriter writer, EncodedValue.Binary value) + public override void Write(VerifyJsonWriter writer, EncodedValue value) { - writer.WriteValue(value.Binary_); + if (value is EncodedValue.Binary(var bytes)) + { + writer.WriteValue(bytes); + } + else + { + throw new InvalidOperationException(); + } } } diff --git a/tools/gen-client-api.bat b/tools/gen-client-api.bat deleted file mode 100644 index 9eb66c49..00000000 --- a/tools/gen-client-api.bat +++ /dev/null @@ -1,20 +0,0 @@ -@echo off -setlocal - -if "%CL_HOME%"=="" ( - echo "Variable CL_HOME not set" - exit /b 1 -) - -cd %CL_HOME%\SpacetimeDB\crates\client-api-messages -cargo run --example get_ws_schema > %CL_HOME%/schema.json - -cd %CL_HOME%\SpacetimeDB\crates\cli -cargo run -- generate -l csharp -n SpacetimeDB.ClientApi ^ - --json-module %CL_HOME%\schema.json ^ - -o %CL_HOME%\spacetimedb-csharp-sdk\src\SpacetimeDB\ClientApi - -cd %CL_HOME%\spacetimedb-csharp-sdk\src\SpacetimeDB\ClientApi -del /q _Globals - -del %CL_HOME%\schema.json diff --git a/tools/gen-client-api.sh b/tools/gen-client-api.sh deleted file mode 100644 index 91fbe3da..00000000 --- a/tools/gen-client-api.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -eu -: $CL_HOME - -cd $CL_HOME/SpacetimeDB/crates/client-api-messages -cargo run --example get_ws_schema > $CL_HOME/schema.json - -cd $CL_HOME/SpacetimeDB/crates/cli -cargo run -- generate -l csharp -n SpacetimeDB.ClientApi \ - --json-module $CL_HOME/schema.json \ - -o $CL_HOME/spacetimedb-csharp-sdk/src/SpacetimeDB/ClientApi - -cd $CL_HOME/spacetimedb-csharp-sdk/src/SpacetimeDB/ClientApi -rm -rf _Globals - -rm -f $CL_HOME/schema.json