Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement consistent filtering rules for C# modules #1282

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 50 additions & 37 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,18 +162,25 @@ 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}) =>
new SpacetimeDB.Runtime.RawTableIterByColEq(tableId.Value, {index}, {f.TypeInfo}.ToBytes({f.Name}))
.SelectMany(GetSatsTypeInfo().ReadBytes);
";

if (f.IndexKind.HasFlag(ColumnAttrs.Unique))
{
extensions +=
$@"
public static {t.Name}? FindBy{f.Name}({f.Type} {f.Name}) =>
new SpacetimeDB.Runtime.RawTableIterByColEq(tableId.Value, {index}, {f.TypeInfo}.ToBytes({f.Name}))
.SelectMany(GetSatsTypeInfo().ReadBytes)
FilterBy{f.Name}({f.Name})
.Cast<{t.Name}?>()
.SingleOrDefault();

Expand All @@ -171,13 +191,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}) =>
new SpacetimeDB.Runtime.RawTableIterByColEq(tableId.Value, {index}, {f.TypeInfo}.ToBytes({f.Name}))
.SelectMany(GetSatsTypeInfo().ReadBytes);
";
}

return new KeyValuePair<string, string>(
Expand Down
Loading