Skip to content

Commit

Permalink
excise AlgebraicType/Value::Map
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Oct 15, 2024
1 parent c4e637e commit 4a596fb
Show file tree
Hide file tree
Showing 45 changed files with 34 additions and 925 deletions.
2 changes: 0 additions & 2 deletions crates/bindings-csharp/BSATN.Codegen/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,6 @@ static string GetTypeInfoForNamedType(INamedTypeSymbol type)
"SpacetimeDB.I256" => "SpacetimeDB.BSATN.I256",
"SpacetimeDB.U256" => "SpacetimeDB.BSATN.U256",
"System.Collections.Generic.List<T>" => $"SpacetimeDB.BSATN.List",
"System.Collections.Generic.Dictionary<TKey, TValue>" =>
$"SpacetimeDB.BSATN.Dictionary",
// If we're here, then this is nullable *value* type like `int?`.
"System.Nullable<T>" => $"SpacetimeDB.BSATN.ValueOption",
var name when name.StartsWith("System.") => throw new InvalidOperationException(
Expand Down
8 changes: 0 additions & 8 deletions crates/bindings-csharp/BSATN.Runtime/BSATN/AlgebraicType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,13 @@ public partial struct AggregateElement(string? name, AlgebraicType algebraicType
public AlgebraicType AlgebraicType = algebraicType;
}

[SpacetimeDB.Type]
public partial struct MapElement(AlgebraicType key, AlgebraicType value)
{
public AlgebraicType Key = key;
public AlgebraicType Value = value;
}

[SpacetimeDB.Type]
public partial record AlgebraicType
: SpacetimeDB.TaggedEnum<(
int Ref,
AggregateElement[] Sum,
AggregateElement[] Product,
AlgebraicType Array,
MapElement Map,
Unit String,
Unit Bool,
Unit I8,
Expand Down
35 changes: 0 additions & 35 deletions crates/bindings-csharp/BSATN.Runtime/BSATN/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,38 +438,3 @@ public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
enumerable.GetAlgebraicType(registrar);
}

public readonly struct Dictionary<Key, Value, KeyRW, ValueRW> : IReadWrite<Dictionary<Key, Value>>
where Key : notnull
where KeyRW : IReadWrite<Key>, new()
where ValueRW : IReadWrite<Value>, new()
{
private static readonly KeyRW keyRW = new();
private static readonly ValueRW valueRW = new();

public Dictionary<Key, Value> Read(BinaryReader reader)
{
var count = reader.ReadInt32();
var result = new Dictionary<Key, Value>(count);
for (var i = 0; i < count; i++)
{
result.Add(keyRW.Read(reader), valueRW.Read(reader));
}
return result;
}

public void Write(BinaryWriter writer, Dictionary<Key, Value> value)
{
writer.Write(value.Count);
foreach (var (key, val) in value)
{
keyRW.Write(writer, key);
valueRW.Write(writer, val);
}
}

public AlgebraicType GetAlgebraicType(ITypeRegistrar registrar) =>
new AlgebraicType.Map(
new(keyRW.GetAlgebraicType(registrar), valueRW.GetAlgebraicType(registrar))
);
}
2 changes: 0 additions & 2 deletions crates/bindings-csharp/Codegen.Tests/fixtures/client/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public partial struct PublicTable
public CustomEnum CustomEnumField;
public CustomTaggedEnum CustomTaggedEnumField;
public List<int> ListField;
public Dictionary<string, int> DictionaryField;
public int? NullableValueField;
public string? NullableReferenceField;
public Dictionary<CustomEnum, List<int?>?>? ComplexNestedField;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ public void ReadFields(System.IO.BinaryReader reader)
CustomEnumField = BSATN.CustomEnumField.Read(reader);
CustomTaggedEnumField = BSATN.CustomTaggedEnumField.Read(reader);
ListField = BSATN.ListField.Read(reader);
DictionaryField = BSATN.DictionaryField.Read(reader);
NullableValueField = BSATN.NullableValueField.Read(reader);
NullableReferenceField = BSATN.NullableReferenceField.Read(reader);
ComplexNestedField = BSATN.ComplexNestedField.Read(reader);
}

public void WriteFields(System.IO.BinaryWriter writer)
Expand Down Expand Up @@ -60,10 +58,8 @@ public void WriteFields(System.IO.BinaryWriter writer)
BSATN.CustomEnumField.Write(writer, CustomEnumField);
BSATN.CustomTaggedEnumField.Write(writer, CustomTaggedEnumField);
BSATN.ListField.Write(writer, ListField);
BSATN.DictionaryField.Write(writer, DictionaryField);
BSATN.NullableValueField.Write(writer, NullableValueField);
BSATN.NullableReferenceField.Write(writer, NullableReferenceField);
BSATN.ComplexNestedField.Write(writer, ComplexNestedField);
}

public readonly partial struct BSATN : SpacetimeDB.BSATN.IReadWrite<PublicTable>
Expand Down Expand Up @@ -92,12 +88,6 @@ public void WriteFields(System.IO.BinaryWriter writer)
internal static readonly CustomTaggedEnum.BSATN CustomTaggedEnumField = new();
internal static readonly SpacetimeDB.BSATN.List<int, SpacetimeDB.BSATN.I32> ListField =
new();
internal static readonly SpacetimeDB.BSATN.Dictionary<
string,
int,
SpacetimeDB.BSATN.String,
SpacetimeDB.BSATN.I32
> DictionaryField = new();
internal static readonly SpacetimeDB.BSATN.ValueOption<
int,
SpacetimeDB.BSATN.I32
Expand All @@ -106,24 +96,6 @@ internal static readonly SpacetimeDB.BSATN.RefOption<
string,
SpacetimeDB.BSATN.String
> NullableReferenceField = new();
internal static readonly SpacetimeDB.BSATN.RefOption<
System.Collections.Generic.Dictionary<
CustomEnum,
System.Collections.Generic.List<int?>?
>,
SpacetimeDB.BSATN.Dictionary<
CustomEnum,
System.Collections.Generic.List<int?>?,
SpacetimeDB.BSATN.Enum<CustomEnum>,
SpacetimeDB.BSATN.RefOption<
System.Collections.Generic.List<int?>,
SpacetimeDB.BSATN.List<
int?,
SpacetimeDB.BSATN.ValueOption<int, SpacetimeDB.BSATN.I32>
>
>
>
> ComplexNestedField = new();

public PublicTable Read(System.IO.BinaryReader reader) =>
SpacetimeDB.BSATN.IStructuralReadWrite.Read<PublicTable>(reader);
Expand Down Expand Up @@ -165,13 +137,11 @@ SpacetimeDB.BSATN.ITypeRegistrar registrar
CustomTaggedEnumField.GetAlgebraicType(registrar)
),
new(nameof(ListField), ListField.GetAlgebraicType(registrar)),
new(nameof(DictionaryField), DictionaryField.GetAlgebraicType(registrar)),
new(nameof(NullableValueField), NullableValueField.GetAlgebraicType(registrar)),
new(
nameof(NullableReferenceField),
NullableReferenceField.GetAlgebraicType(registrar)
),
new(nameof(ComplexNestedField), ComplexNestedField.GetAlgebraicType(registrar))
)
}
));
}
Expand Down
2 changes: 0 additions & 2 deletions crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,8 @@ public partial struct PublicTable
public CustomEnum CustomEnumField;
public CustomTaggedEnum CustomTaggedEnumField;
public List<int> ListField;
public Dictionary<string, int> DictionaryField;
public int? NullableValueField;
public string? NullableReferenceField;
public Dictionary<CustomEnum, List<int?>?>? ComplexNestedField;
}

public static partial class Reducers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ public void ReadFields(System.IO.BinaryReader reader)
CustomEnumField = BSATN.CustomEnumField.Read(reader);
CustomTaggedEnumField = BSATN.CustomTaggedEnumField.Read(reader);
ListField = BSATN.ListField.Read(reader);
DictionaryField = BSATN.DictionaryField.Read(reader);
NullableValueField = BSATN.NullableValueField.Read(reader);
NullableReferenceField = BSATN.NullableReferenceField.Read(reader);
ComplexNestedField = BSATN.ComplexNestedField.Read(reader);
}

public void WriteFields(System.IO.BinaryWriter writer)
Expand Down Expand Up @@ -66,10 +64,8 @@ public void WriteFields(System.IO.BinaryWriter writer)
BSATN.CustomEnumField.Write(writer, CustomEnumField);
BSATN.CustomTaggedEnumField.Write(writer, CustomTaggedEnumField);
BSATN.ListField.Write(writer, ListField);
BSATN.DictionaryField.Write(writer, DictionaryField);
BSATN.NullableValueField.Write(writer, NullableValueField);
BSATN.NullableReferenceField.Write(writer, NullableReferenceField);
BSATN.ComplexNestedField.Write(writer, ComplexNestedField);
}

public readonly partial struct BSATN : SpacetimeDB.BSATN.IReadWrite<PublicTable>
Expand Down Expand Up @@ -101,12 +97,6 @@ public void WriteFields(System.IO.BinaryWriter writer)
internal static readonly CustomTaggedEnum.BSATN CustomTaggedEnumField = new();
internal static readonly SpacetimeDB.BSATN.List<int, SpacetimeDB.BSATN.I32> ListField =
new();
internal static readonly SpacetimeDB.BSATN.Dictionary<
string,
int,
SpacetimeDB.BSATN.String,
SpacetimeDB.BSATN.I32
> DictionaryField = new();
internal static readonly SpacetimeDB.BSATN.ValueOption<
int,
SpacetimeDB.BSATN.I32
Expand All @@ -115,24 +105,6 @@ internal static readonly SpacetimeDB.BSATN.RefOption<
string,
SpacetimeDB.BSATN.String
> NullableReferenceField = new();
internal static readonly SpacetimeDB.BSATN.RefOption<
System.Collections.Generic.Dictionary<
CustomEnum,
System.Collections.Generic.List<int?>?
>,
SpacetimeDB.BSATN.Dictionary<
CustomEnum,
System.Collections.Generic.List<int?>?,
SpacetimeDB.BSATN.Enum<CustomEnum>,
SpacetimeDB.BSATN.RefOption<
System.Collections.Generic.List<int?>,
SpacetimeDB.BSATN.List<
int?,
SpacetimeDB.BSATN.ValueOption<int, SpacetimeDB.BSATN.I32>
>
>
>
> ComplexNestedField = new();

public PublicTable Read(System.IO.BinaryReader reader) =>
SpacetimeDB.BSATN.IStructuralReadWrite.Read<PublicTable>(reader);
Expand Down Expand Up @@ -177,13 +149,11 @@ SpacetimeDB.BSATN.ITypeRegistrar registrar
CustomTaggedEnumField.GetAlgebraicType(registrar)
),
new(nameof(ListField), ListField.GetAlgebraicType(registrar)),
new(nameof(DictionaryField), DictionaryField.GetAlgebraicType(registrar)),
new(nameof(NullableValueField), NullableValueField.GetAlgebraicType(registrar)),
new(
nameof(NullableReferenceField),
NullableReferenceField.GetAlgebraicType(registrar)
),
new(nameof(ComplexNestedField), ComplexNestedField.GetAlgebraicType(registrar))
)
}
));
}
Expand Down Expand Up @@ -235,21 +205,13 @@ SpacetimeDB.BSATN.ITypeRegistrar registrar
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: [],
Expand Down
7 changes: 0 additions & 7 deletions crates/cli/src/subcommands/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,6 @@ mod write_type {
write_type(ctx, out, elem_ty)?;
write!(out, ">")?;
}
AlgebraicType::Map(ty) => {
write!(out, "Map<")?;
write_type(ctx, out, &ty.key_ty)?;
write!(out, ", ")?;
write_type(ctx, out, &ty.ty)?;
write!(out, ">")?;
}
AlgebraicType::Ref(r) => {
write!(out, "{}", ctx(*r))?;
}
Expand Down
18 changes: 4 additions & 14 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ fn scalar_or_string_name(b: &AlgebraicType) -> Option<&str> {
AlgebraicType::String => "string",
AlgebraicType::F32 => "float",
AlgebraicType::F64 => "double",
AlgebraicType::Ref(_)
| AlgebraicType::Sum(_)
| AlgebraicType::Product(_)
| AlgebraicType::Array(_)
| AlgebraicType::Map(_) => return None,
AlgebraicType::Ref(_) | AlgebraicType::Sum(_) | AlgebraicType::Product(_) | AlgebraicType::Array(_) => {
return None
}
})
}

Expand All @@ -64,14 +62,6 @@ fn ty_fmt<'a>(ctx: &'a GenCtx, ty: &'a AlgebraicType, namespace: &'a str) -> imp
ty_fmt(ctx, elem_ty, namespace)
)
}
AlgebraicType::Map(ty) => {
write!(
f,
"System.Collections.Generic.Dictionary<{}, {}>",
ty_fmt(ctx, &ty.ty, namespace),
ty_fmt(ctx, &ty.key_ty, namespace)
)
}
AlgebraicType::Ref(r) => {
let name = csharp_typename(ctx, *r);
match &ctx.typespace[*r] {
Expand Down Expand Up @@ -105,7 +95,7 @@ fn default_init(ctx: &GenCtx, ty: &AlgebraicType) -> Option<&'static str> {
// TODO: generate some proper default here (what would it be for tagged enums?).
AlgebraicType::Sum(_) => Some("null!"),
// For product types, arrays, and maps, we can use the default constructor.
AlgebraicType::Product(_) | AlgebraicType::Array(_) | AlgebraicType::Map(_) => Some("new()"),
AlgebraicType::Product(_) | AlgebraicType::Array(_) => Some("new()"),
// Strings must have explicit default value of "".
AlgebraicType::String => Some(r#""""#),
AlgebraicType::Ref(r) => default_init(ctx, &ctx.typespace[*r]),
Expand Down
1 change: 0 additions & 1 deletion crates/cli/src/subcommands/generate/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,6 @@ pub fn write_type<W: Write>(module: &ModuleDef, out: &mut W, ty: &AlgebraicTypeU
write_type(module, out, elem_ty)?;
write!(out, ">")?;
}
AlgebraicTypeUse::Map { .. } => unimplemented!("AlgebraicType::Map is unsupported and will be removed"),
AlgebraicTypeUse::Ref(r) => {
write!(out, "{}", type_ref_name(module, *r))?;
}
Expand Down
2 changes: 0 additions & 2 deletions crates/cli/src/subcommands/generate/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,6 @@ pub fn write_type<W: Write>(
write_type(module, out, elem_ty, ref_prefix)?;
write!(out, "[]")?;
}
AlgebraicTypeUse::Map { .. } => unimplemented!("AlgebraicType::Map is unsupported and will be removed"),
AlgebraicTypeUse::Ref(r) => {
if let Some(prefix) = ref_prefix {
write!(out, "{prefix}")?;
Expand Down Expand Up @@ -978,7 +977,6 @@ fn convert_algebraic_type<'a>(
AlgebraicTypeUse::Primitive(prim) => {
write!(out, "AlgebraicType.create{prim:?}Type()");
}
AlgebraicTypeUse::Map { .. } => unimplemented!(),
AlgebraicTypeUse::Unit => write!(out, "AlgebraicType.createProductType([])"),
AlgebraicTypeUse::Never => unimplemented!(),
AlgebraicTypeUse::String => write!(out, "AlgebraicType.createStringType()"),
Expand Down
Loading

0 comments on commit 4a596fb

Please sign in to comment.