Skip to content

Commit

Permalink
[release/v1.0.0-rc1]: Merge remote-tracking branch 'origin/jeremie/cs…
Browse files Browse the repository at this point in the history
…-connection-state-fix' into release/v1.0.0-rc1
  • Loading branch information
bfops committed Oct 24, 2024
2 parents a0a0f2c + 4ceb5df commit ba16f99
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
14 changes: 11 additions & 3 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,14 +412,22 @@ fn autogen_csharp_access_funcs_for_struct(
"public readonly ref struct {csharp_field_name_pascal}UniqueIndex"
);
indented_block(output, |output| {
writeln!(output, "readonly {csharp_table_name}Handle Handle;");
write!(
output,
"internal {csharp_field_name_pascal}UniqueIndex({csharp_table_name}Handle handle) => "
);
writeln!(output, "Handle = handle;");
writeln!(output);

writeln!(
output,
"public {struct_name_pascal_case}? Find({csharp_field_type} value)"
);
indented_block(output, |output| {
writeln!(
output,
"{csharp_field_name_pascal}_Index.TryGetValue(value, out var r);"
"Handle.{csharp_field_name_pascal}_Index.TryGetValue(value, out var r);"
);
writeln!(output, "return r;");
});
Expand All @@ -428,7 +436,7 @@ fn autogen_csharp_access_funcs_for_struct(
writeln!(output);
writeln!(
output,
"public {csharp_field_name_pascal}UniqueIndex {csharp_field_name_pascal} => new();"
"public {csharp_field_name_pascal}UniqueIndex {csharp_field_name_pascal} => new(this);"
);
writeln!(output);
}
Expand Down Expand Up @@ -592,7 +600,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str)
let type_name = ty_fmt(ctx, &col.col_type, namespace);
writeln!(
output,
"private static Dictionary<{type_name}, {table_type}> {field_name}_Index = new(16);"
"private Dictionary<{type_name}, {table_type}> {field_name}_Index = new(16);"
);
unique_indexes.push(field_name);
}
Expand Down
36 changes: 24 additions & 12 deletions crates/cli/tests/snapshots/codegen__codegen_csharp.snap
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ namespace SpacetimeDB

public class PkMultiIdentityHandle : RemoteTableHandle<EventContext, PkMultiIdentity>
{
private static Dictionary<uint, PkMultiIdentity> Id_Index = new(16);
private static Dictionary<uint, PkMultiIdentity> Other_Index = new(16);
private Dictionary<uint, PkMultiIdentity> Id_Index = new(16);
private Dictionary<uint, PkMultiIdentity> Other_Index = new(16);

public override void InternalInvokeValueInserted(IDatabaseRow row)
{
Expand All @@ -690,27 +690,33 @@ namespace SpacetimeDB

public readonly ref struct IdUniqueIndex
{
readonly PkMultiIdentityHandle Handle;
internal IdUniqueIndex(PkMultiIdentityHandle handle) => Handle = handle;

public PkMultiIdentity? Find(uint value)
{
Id_Index.TryGetValue(value, out var r);
Handle.Id_Index.TryGetValue(value, out var r);
return r;
}

}

public IdUniqueIndex Id => new();
public IdUniqueIndex Id => new(this);

public readonly ref struct OtherUniqueIndex
{
readonly PkMultiIdentityHandle Handle;
internal OtherUniqueIndex(PkMultiIdentityHandle handle) => Handle = handle;

public PkMultiIdentity? Find(uint value)
{
Other_Index.TryGetValue(value, out var r);
Handle.Other_Index.TryGetValue(value, out var r);
return r;
}

}

public OtherUniqueIndex Other => new();
public OtherUniqueIndex Other => new(this);

internal PkMultiIdentityHandle()
{
Expand Down Expand Up @@ -754,7 +760,7 @@ namespace SpacetimeDB

public class RepeatingTestArgHandle : RemoteTableHandle<EventContext, RepeatingTestArg>
{
private static Dictionary<ulong, RepeatingTestArg> ScheduledId_Index = new(16);
private Dictionary<ulong, RepeatingTestArg> ScheduledId_Index = new(16);

public override void InternalInvokeValueInserted(IDatabaseRow row)
{
Expand All @@ -769,15 +775,18 @@ namespace SpacetimeDB

public readonly ref struct ScheduledIdUniqueIndex
{
readonly RepeatingTestArgHandle Handle;
internal ScheduledIdUniqueIndex(RepeatingTestArgHandle handle) => Handle = handle;

public RepeatingTestArg? Find(ulong value)
{
ScheduledId_Index.TryGetValue(value, out var r);
Handle.ScheduledId_Index.TryGetValue(value, out var r);
return r;
}

}

public ScheduledIdUniqueIndex ScheduledId => new();
public ScheduledIdUniqueIndex ScheduledId => new(this);

internal RepeatingTestArgHandle()
{
Expand Down Expand Up @@ -821,7 +830,7 @@ namespace SpacetimeDB

public class TestEHandle : RemoteTableHandle<EventContext, TestE>
{
private static Dictionary<ulong, TestE> Id_Index = new(16);
private Dictionary<ulong, TestE> Id_Index = new(16);

public override void InternalInvokeValueInserted(IDatabaseRow row)
{
Expand All @@ -836,15 +845,18 @@ namespace SpacetimeDB

public readonly ref struct IdUniqueIndex
{
readonly TestEHandle Handle;
internal IdUniqueIndex(TestEHandle handle) => Handle = handle;

public TestE? Find(ulong value)
{
Id_Index.TryGetValue(value, out var r);
Handle.Id_Index.TryGetValue(value, out var r);
return r;
}

}

public IdUniqueIndex Id => new();
public IdUniqueIndex Id => new(this);

public class NameIndex
{
Expand Down

0 comments on commit ba16f99

Please sign in to comment.