From 90ec74f564b3574c40549c3528127b4bde9262ac Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Wed, 4 Oct 2023 15:50:19 +0100 Subject: [PATCH] Update C# bindings --- crates/bindings-csharp/Codegen/Utils.cs | 38 ++++---- .../bindings-csharp/Runtime/AlgebraicType.cs | 90 ++++++++----------- crates/bindings-csharp/Runtime/Runtime.cs | 12 +-- 3 files changed, 64 insertions(+), 76 deletions(-) diff --git a/crates/bindings-csharp/Codegen/Utils.cs b/crates/bindings-csharp/Codegen/Utils.cs index 424fbb287b..134b9ed2fb 100644 --- a/crates/bindings-csharp/Codegen/Utils.cs +++ b/crates/bindings-csharp/Codegen/Utils.cs @@ -60,23 +60,23 @@ public static string GetTypeInfo(ITypeSymbol type) INamedTypeSymbol namedType => type.SpecialType switch { - SpecialType.System_Boolean => "SpacetimeDB.SATS.BuiltinType.BoolTypeInfo", - SpecialType.System_SByte => "SpacetimeDB.SATS.BuiltinType.I8TypeInfo", - SpecialType.System_Byte => "SpacetimeDB.SATS.BuiltinType.U8TypeInfo", - SpecialType.System_Int16 => "SpacetimeDB.SATS.BuiltinType.I16TypeInfo", - SpecialType.System_UInt16 => "SpacetimeDB.SATS.BuiltinType.U16TypeInfo", - SpecialType.System_Int32 => "SpacetimeDB.SATS.BuiltinType.I32TypeInfo", - SpecialType.System_UInt32 => "SpacetimeDB.SATS.BuiltinType.U32TypeInfo", - SpecialType.System_Int64 => "SpacetimeDB.SATS.BuiltinType.I64TypeInfo", - SpecialType.System_UInt64 => "SpacetimeDB.SATS.BuiltinType.U64TypeInfo", + SpecialType.System_Boolean => "SpacetimeDB.SATS.AlgebraicType.BoolTypeInfo", + SpecialType.System_SByte => "SpacetimeDB.SATS.AlgebraicType.I8TypeInfo", + SpecialType.System_Byte => "SpacetimeDB.SATS.AlgebraicType.U8TypeInfo", + SpecialType.System_Int16 => "SpacetimeDB.SATS.AlgebraicType.I16TypeInfo", + SpecialType.System_UInt16 => "SpacetimeDB.SATS.AlgebraicType.U16TypeInfo", + SpecialType.System_Int32 => "SpacetimeDB.SATS.AlgebraicType.I32TypeInfo", + SpecialType.System_UInt32 => "SpacetimeDB.SATS.AlgebraicType.U32TypeInfo", + SpecialType.System_Int64 => "SpacetimeDB.SATS.AlgebraicType.I64TypeInfo", + SpecialType.System_UInt64 => "SpacetimeDB.SATS.AlgebraicType.U64TypeInfo", SpecialType.System_Single - => "SpacetimeDB.SATS.BuiltinType.F32TypeInfo", - SpecialType.System_Double => "SpacetimeDB.SATS.BuiltinType.F64TypeInfo", - SpecialType.System_String => "SpacetimeDB.SATS.BuiltinType.StringTypeInfo", + => "SpacetimeDB.SATS.AlgebraicType.F32TypeInfo", + SpecialType.System_Double => "SpacetimeDB.SATS.AlgebraicType.F64TypeInfo", + SpecialType.System_String => "SpacetimeDB.SATS.AlgebraicType.StringTypeInfo", SpecialType.None when type.ToString() == "System.Int128" - => "SpacetimeDB.SATS.BuiltinType.I128TypeInfo", + => "SpacetimeDB.SATS.AlgebraicType.I128TypeInfo", SpecialType.None when type.ToString() == "System.UInt128" - => "SpacetimeDB.SATS.BuiltinType.U128TypeInfo", + => "SpacetimeDB.SATS.AlgebraicType.U128TypeInfo", SpecialType.None when namedType.EnumUnderlyingType is not null // check that enums also have [SpacetimeDB.Type] @@ -88,12 +88,12 @@ when namedType.EnumUnderlyingType is not null a.AttributeClass?.ToDisplayString() == "SpacetimeDB.TypeAttribute" ) - => $"SpacetimeDB.SATS.BuiltinType.MakeEnum<{type}>()", + => $"SpacetimeDB.SATS.AlgebraicType.MakeEnum<{type}>()", SpecialType.None => $"{type.OriginalDefinition.ToString() switch { - "System.Collections.Generic.List" => "SpacetimeDB.SATS.BuiltinType.MakeList", - "System.Collections.Generic.Dictionary" => "SpacetimeDB.SATS.BuiltinType.MakeMap", + "System.Collections.Generic.List" => "SpacetimeDB.SATS.AlgebraicType.MakeList", + "System.Collections.Generic.Dictionary" => "SpacetimeDB.SATS.AlgebraicType.MakeMap", // If we're here, then this is nullable value type like `int?`. "System.Nullable" => $"SpacetimeDB.SATS.SumType.MakeValueOption", var name when name.StartsWith("System.") => throw new InvalidOperationException( @@ -109,8 +109,8 @@ when namedType.EnumUnderlyingType is not null IArrayTypeSymbol arrayType => arrayType.ElementType is INamedTypeSymbol namedType && namedType.SpecialType == SpecialType.System_Byte - ? "SpacetimeDB.SATS.BuiltinType.BytesTypeInfo" - : $"SpacetimeDB.SATS.BuiltinType.MakeArray({GetTypeInfo(arrayType.ElementType)})", + ? "SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo" + : $"SpacetimeDB.SATS.AlgebraicType.MakeArray({GetTypeInfo(arrayType.ElementType)})", _ => throw new InvalidOperationException($"Unsupported type {type}") }; } diff --git a/crates/bindings-csharp/Runtime/AlgebraicType.cs b/crates/bindings-csharp/Runtime/AlgebraicType.cs index e1cba56f09..e774e32027 100644 --- a/crates/bindings-csharp/Runtime/AlgebraicType.cs +++ b/crates/bindings-csharp/Runtime/AlgebraicType.cs @@ -193,8 +193,13 @@ public MapType(AlgebraicType key, AlgebraicType value) } [SpacetimeDB.Type] - public partial struct BuiltinType + public partial struct AlgebraicType : SpacetimeDB.TaggedEnum<( + AlgebraicTypeRef TypeRef, + SumType Sum, + ProductType Product, + AlgebraicType Array, + MapType Map, Unit Bool, Unit I8, Unit U8, @@ -208,68 +213,81 @@ public partial struct BuiltinType Unit U128, Unit F32, Unit F64, - Unit String, - AlgebraicType Array, - MapType Map + Unit String )> { + public static implicit operator AlgebraicType(AlgebraicTypeRef typeRef) + { + return new AlgebraicType { TypeRef = typeRef }; + } + + public static implicit operator AlgebraicType(SumType sum) + { + return new AlgebraicType { Sum = sum }; + } + + public static implicit operator AlgebraicType(ProductType product) + { + return new AlgebraicType { Product = product }; + } + public static readonly TypeInfo BoolTypeInfo = new TypeInfo( - new BuiltinType { Bool = default }, + new AlgebraicType { Bool = default }, (reader) => reader.ReadBoolean(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo I8TypeInfo = new TypeInfo( - new BuiltinType { I8 = default }, + new AlgebraicType { I8 = default }, (reader) => reader.ReadSByte(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo U8TypeInfo = new TypeInfo( - new BuiltinType { U8 = default }, + new AlgebraicType { U8 = default }, (reader) => reader.ReadByte(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo I16TypeInfo = new TypeInfo( - new BuiltinType { I16 = default }, + new AlgebraicType { I16 = default }, (reader) => reader.ReadInt16(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo U16TypeInfo = new TypeInfo( - new BuiltinType { U16 = default }, + new AlgebraicType { U16 = default }, (reader) => reader.ReadUInt16(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo I32TypeInfo = new TypeInfo( - new BuiltinType { I32 = default }, + new AlgebraicType { I32 = default }, (reader) => reader.ReadInt32(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo U32TypeInfo = new TypeInfo( - new BuiltinType { U32 = default }, + new AlgebraicType { U32 = default }, (reader) => reader.ReadUInt32(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo I64TypeInfo = new TypeInfo( - new BuiltinType { I64 = default }, + new AlgebraicType { I64 = default }, (reader) => reader.ReadInt64(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo U64TypeInfo = new TypeInfo( - new BuiltinType { U64 = default }, + new AlgebraicType { U64 = default }, (reader) => reader.ReadUInt64(), (writer, value) => writer.Write(value) ); #if NET7_0_OR_GREATER public static readonly TypeInfo I128TypeInfo = new TypeInfo( - new BuiltinType { I128 = default }, + new AlgebraicType { I128 = default }, (reader) => new Int128(reader.ReadUInt64(), reader.ReadUInt64()), (writer, value) => { @@ -279,7 +297,7 @@ MapType Map ); public static readonly TypeInfo U128TypeInfo = new TypeInfo( - new BuiltinType { U128 = default }, + new AlgebraicType { U128 = default }, (reader) => new UInt128(reader.ReadUInt64(), reader.ReadUInt64()), (writer, value) => { @@ -290,19 +308,19 @@ MapType Map #endif public static readonly TypeInfo F32TypeInfo = new TypeInfo( - new BuiltinType { F32 = default }, + new AlgebraicType { F32 = default }, (reader) => reader.ReadSingle(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo F64TypeInfo = new TypeInfo( - new BuiltinType { F64 = default }, + new AlgebraicType { F64 = default }, (reader) => reader.ReadDouble(), (writer, value) => writer.Write(value) ); public static readonly TypeInfo BytesTypeInfo = new TypeInfo( - new BuiltinType { Array = U8TypeInfo.AlgebraicType }, + new AlgebraicType { Array = U8TypeInfo.AlgebraicType }, (reader) => { var length = reader.ReadInt32(); @@ -316,7 +334,7 @@ MapType Map ); public static readonly TypeInfo StringTypeInfo = new TypeInfo( - new BuiltinType { String = default }, + new AlgebraicType { String = default }, (reader) => Encoding.UTF8.GetString(BytesTypeInfo.Read(reader)), (writer, value) => BytesTypeInfo.Write(writer, Encoding.UTF8.GetBytes(value)) ); @@ -350,7 +368,7 @@ TypeInfo elementTypeInfo where A : ICollection { return new TypeInfo( - new BuiltinType { Array = elementTypeInfo.AlgebraicType }, + new AlgebraicType { Array = elementTypeInfo.AlgebraicType }, (reader) => create(ReadEnumerable(reader, elementTypeInfo.Read)), (writer, array) => WriteEnumerable(writer, array, elementTypeInfo.Write) ); @@ -366,7 +384,7 @@ public static TypeInfo> MakeMap(TypeInfo key, TypeInfo where K : notnull { return new TypeInfo>( - new BuiltinType { Map = new MapType(key.AlgebraicType, value.AlgebraicType) }, + new AlgebraicType { Map = new MapType(key.AlgebraicType, value.AlgebraicType) }, (reader) => ReadEnumerable( reader, @@ -422,36 +440,6 @@ public static TypeInfo MakeEnum() } } - [SpacetimeDB.Type] - public partial struct AlgebraicType - : SpacetimeDB.TaggedEnum<( - SumType Sum, - ProductType Product, - BuiltinType Builtin, - AlgebraicTypeRef TypeRef - )> - { - public static implicit operator AlgebraicType(SumType sum) - { - return new AlgebraicType { Sum = sum }; - } - - public static implicit operator AlgebraicType(ProductType product) - { - return new AlgebraicType { Product = product }; - } - - public static implicit operator AlgebraicType(BuiltinType builtin) - { - return new AlgebraicType { Builtin = builtin }; - } - - public static implicit operator AlgebraicType(AlgebraicTypeRef typeRef) - { - return new AlgebraicType { TypeRef = typeRef }; - } - } - [SpacetimeDB.Type] public partial struct AlgebraicTypeRef { diff --git a/crates/bindings-csharp/Runtime/Runtime.cs b/crates/bindings-csharp/Runtime/Runtime.cs index c434fbdcaa..3230712607 100644 --- a/crates/bindings-csharp/Runtime/Runtime.cs +++ b/crates/bindings-csharp/Runtime/Runtime.cs @@ -206,11 +206,11 @@ public override int GetHashCode() => // We need to set type info to inlined identity type as `generate` CLI currently can't recognise type references for built-ins. new SpacetimeDB.SATS.ProductType { - { "__identity_bytes", SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.AlgebraicType } + { "__identity_bytes", SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.AlgebraicType } }, - reader => new(SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.Read(reader)), + reader => new(SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.Read(reader)), (writer, value) => - SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.Write(writer, value.bytes) + SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.Write(writer, value.bytes) ); public static SpacetimeDB.SATS.TypeInfo GetSatsTypeInfo() => satsTypeInfo; @@ -243,7 +243,7 @@ public override int GetHashCode() => // We need to set type info to inlined address type as `generate` CLI currently can't recognise type references for built-ins. new SpacetimeDB.SATS.ProductType { - { "__address_bytes", SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.AlgebraicType } + { "__address_bytes", SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.AlgebraicType } }, // Concern: We use this "packed" representation (as Bytes) // in the caller_id field of reducer arguments, @@ -252,9 +252,9 @@ public override int GetHashCode() => // It's possible that these happen to be identical // because BSATN is minimally self-describing, // but that doesn't seem like something we should count on. - reader => new(SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.Read(reader)), + reader => new(SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.Read(reader)), (writer, value) => - SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.Write(writer, value.bytes) + SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.Write(writer, value.bytes) ); public static SpacetimeDB.SATS.TypeInfo
GetSatsTypeInfo() => satsTypeInfo;