Skip to content

Commit

Permalink
Implement consistent filtering rules for C# modules
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed May 22, 2024
1 parent 9257208 commit 79fdd08
Showing 1 changed file with 51 additions and 39 deletions.
90 changes: 51 additions & 39 deletions crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,54 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.Select(a => (ColumnAttrs)a.ConstructorArguments[0].Value!)
.SingleOrDefault();

if (indexKind.HasFlag(ColumnAttrs.AutoInc))
var isInteger = f.Type.SpecialType switch
{
var isValidForAutoInc = f.Type.SpecialType switch
SpecialType.System_Byte
or SpecialType.System_SByte
or SpecialType.System_Int16
or SpecialType.System_UInt16
or SpecialType.System_Int32
or SpecialType.System_UInt32
or SpecialType.System_Int64
or SpecialType.System_UInt64
=> true,
SpecialType.None
=> f.Type.ToString() is "System.Int128" or "System.UInt128",
_ => false
};

if (indexKind.HasFlag(ColumnAttrs.AutoInc) && !isInteger)
{
throw new System.Exception(
$"{f.Type} {f.Name} is not valid for AutoInc or Identity as it's not an integer."
);
}

var isEquatable =
isInteger
|| f.Type.SpecialType switch
{
SpecialType.System_Byte
or SpecialType.System_SByte
or SpecialType.System_Int16
or SpecialType.System_UInt16
or SpecialType.System_Int32
or SpecialType.System_UInt32
or SpecialType.System_Int64
or SpecialType.System_UInt64
=> true,
SpecialType.System_String or SpecialType.System_Boolean => true,
SpecialType.None
=> f.Type.ToString() switch
{
"System.Int128" or "System.UInt128" => true,
_ => false
},
_ => false
=> f.Type.ToString()
is "SpacetimeDB.Runtime.Address"
or "SpacetimeDB.Runtime.Identity",
_ => false,
};

if (!isValidForAutoInc)
{
throw new System.Exception(
$"Type {f.Type} is not valid for AutoInc or Identity as it's not an integer."
);
}
if (indexKind.HasFlag(ColumnAttrs.Unique) && !isEquatable)
{
throw new System.Exception(
$"{f.Type} {f.Name} is not valid for Identity, PrimaryKey or PrimaryKeyAuto as it's not an equatable primitive."
);
}

return (
Name: f.Name,
f.Name,
Type: SymbolToName(f.Type),
TypeInfo: GetTypeInfo(f.Type),
IndexKind: indexKind
IndexKind: indexKind,
IsEquatable: isEquatable
);
})
.ToArray();
Expand Down Expand Up @@ -149,19 +162,26 @@ public void Insert() {{
";

foreach (
var (f, index) in t.Fields.Select(
(f, i) => (f, $"new SpacetimeDB.RawBindings.ColId({i})")
)
var (f, i) in t.Fields.Select((field, i) => (field, i))
.Where(pair => pair.field.IsEquatable)
)
{
var index = $"new SpacetimeDB.RawBindings.ColId({i})";

extensions +=
$@"
public static IEnumerable<{t.Name}> FilterBy{f.Name}({f.Type} {f.Name}) =>
GetSatsTypeInfo().ReadBytes(
SpacetimeDB.Runtime.IterByColEq(tableId.Value, {index}, {f.TypeInfo}.ToBytes({f.Name}))
);
";

if (f.IndexKind.HasFlag(ColumnAttrs.Unique))
{
extensions +=
$@"
public static {t.Name}? FindBy{f.Name}({f.Type} {f.Name}) =>
GetSatsTypeInfo().ReadBytes(
SpacetimeDB.Runtime.IterByColEq(tableId.Value, {index}, {f.TypeInfo}.ToBytes({f.Name}))
)
FilterBy{f.Name}({f.Name})
.Cast<{t.Name}?>()
.SingleOrDefault();
Expand All @@ -172,14 +192,6 @@ public void Insert() {{
SpacetimeDB.Runtime.UpdateByColEq(tableId.Value, {index}, {f.TypeInfo}.ToBytes({f.Name}), GetSatsTypeInfo().ToBytes(value));
";
}

extensions +=
$@"
public static IEnumerable<{t.Name}> FilterBy{f.Name}({f.Type} {f.Name}) =>
GetSatsTypeInfo().ReadBytes(
SpacetimeDB.Runtime.IterByColEq(tableId.Value, {index}, {f.TypeInfo}.ToBytes({f.Name}))
);
";
}

return new KeyValuePair<string, string>(
Expand Down

0 comments on commit 79fdd08

Please sign in to comment.