Skip to content

Commit 60e4a64

Browse files
authored
C# module bindings for Procedures (#3732)
# Description of Changes Implements the C# module bindings for Procedures (#3510) # API and ABI breaking changes None # Expected complexity level and risk 2 # Testing - [X] Locally tested against existing C# Procedures regression test client (minus the Transaction and HTTP portions, as those are not in this change).
1 parent ed2a18c commit 60e4a64

File tree

20 files changed

+1014
-23
lines changed

20 files changed

+1014
-23
lines changed

crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module.verified.txt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public partial record TestTableTaggedEnum : SpacetimeDB.TaggedEnum<(int X, int Y
9494
Message: Index attribute on a field applies directly to that field, so it doesn't accept the Columns parameter.,
9595
Severity: Error,
9696
Descriptor: {
97-
Id: STDB0013,
97+
Id: STDB0015,
9898
Title: Index attribute on a field must not specify Columns,
9999
MessageFormat: Index attribute on a field applies directly to that field, so it doesn't accept the Columns parameter.,
100100
Category: SpacetimeDB,
@@ -145,7 +145,7 @@ public partial struct TestIndexIssues
145145
Message: Could not find the specified column UnknownColumn in TestIndexIssues.,
146146
Severity: Error,
147147
Descriptor: {
148-
Id: STDB0014,
148+
Id: STDB0016,
149149
Title: Unknown column,
150150
MessageFormat: Could not find the specified column {0} in {1}.,
151151
Category: SpacetimeDB,
@@ -170,7 +170,7 @@ public partial struct TestIndexIssues
170170
Message: TestScheduleWithoutPrimaryKey is a scheduled table but doesn't have a primary key of type `ulong`.,
171171
Severity: Error,
172172
Descriptor: {
173-
Id: STDB0012,
173+
Id: STDB0014,
174174
Title: Invalid scheduled table declaration,
175175
MessageFormat: {0},
176176
Category: SpacetimeDB,
@@ -195,7 +195,7 @@ public partial struct TestIndexIssues
195195
Message: TestScheduleWithWrongPrimaryKeyType is a scheduled table but doesn't have a primary key of type `ulong`.,
196196
Severity: Error,
197197
Descriptor: {
198-
Id: STDB0012,
198+
Id: STDB0014,
199199
Title: Invalid scheduled table declaration,
200200
MessageFormat: {0},
201201
Category: SpacetimeDB,
@@ -212,7 +212,7 @@ public partial struct TestIndexIssues
212212
Message: Could not find the specified column ScheduledAt in TestScheduleIssues.,
213213
Severity: Error,
214214
Descriptor: {
215-
Id: STDB0014,
215+
Id: STDB0016,
216216
Title: Unknown column,
217217
MessageFormat: Could not find the specified column {0} in {1}.,
218218
Category: SpacetimeDB,
@@ -237,7 +237,7 @@ public partial struct TestIndexIssues
237237
Message: TestScheduleWithWrongScheduleAtType is a scheduled table but doesn't have a primary key of type `ulong`.,
238238
Severity: Error,
239239
Descriptor: {
240-
Id: STDB0012,
240+
Id: STDB0014,
241241
Title: Invalid scheduled table declaration,
242242
MessageFormat: {0},
243243
Category: SpacetimeDB,
@@ -262,7 +262,7 @@ public partial struct TestScheduleIssues
262262
Message: Could not find the specified column MissingField in TestScheduleIssues.,
263263
Severity: Error,
264264
Descriptor: {
265-
Id: STDB0014,
265+
Id: STDB0016,
266266
Title: Unknown column,
267267
MessageFormat: Could not find the specified column {0} in {1}.,
268268
Category: SpacetimeDB,
@@ -287,7 +287,7 @@ public partial struct TestScheduleIssues
287287
Message: View 'ViewDefNoPublic' must have Public = true. Views are always public in SpacetimeDB.,
288288
Severity: Error,
289289
Descriptor: {
290-
Id: STDB0023,
290+
Id: STDB0025,
291291
Title: Views must be public,
292292
MessageFormat: View '{0}' must have Public = true. Views are always public in SpacetimeDB.,
293293
Category: SpacetimeDB,
@@ -304,7 +304,7 @@ public partial struct TestScheduleIssues
304304
Message: View method ViewDefNoContext must have a first parameter of type ViewContext or AnonymousViewContext.,
305305
Severity: Error,
306306
Descriptor: {
307-
Id: STDB0020,
307+
Id: STDB0022,
308308
Title: Views must start with ViewContext or AnonymousViewContext,
309309
MessageFormat: View method {0} must have a first parameter of type ViewContext or AnonymousViewContext.,
310310
Category: SpacetimeDB,
@@ -321,7 +321,7 @@ public partial struct TestScheduleIssues
321321
Message: View method ViewDefWrongContext must have a first parameter of type ViewContext or AnonymousViewContext.,
322322
Severity: Error,
323323
Descriptor: {
324-
Id: STDB0020,
324+
Id: STDB0022,
325325
Title: Views must start with ViewContext or AnonymousViewContext,
326326
MessageFormat: View method {0} must have a first parameter of type ViewContext or AnonymousViewContext.,
327327
Category: SpacetimeDB,
@@ -346,7 +346,7 @@ public partial struct TestScheduleIssues
346346
Message: View 'ViewDefWrongReturn' must return Vec<T> or Option<T>.,
347347
Severity: Error,
348348
Descriptor: {
349-
Id: STDB0022,
349+
Id: STDB0024,
350350
Title: Views must return Vec<T> or Option<T>,
351351
MessageFormat: View '{0}' must return Vec<T> or Option<T>.,
352352
Category: SpacetimeDB,
@@ -397,7 +397,7 @@ public partial struct TestScheduleIssues
397397
Message: Reducer method OnReducerWithReservedPrefix starts with 'On', which is a reserved prefix.,
398398
Severity: Error,
399399
Descriptor: {
400-
Id: STDB0009,
400+
Id: STDB0010,
401401
Title: Reducer method has a reserved name prefix,
402402
MessageFormat: Reducer method {0} starts with '{1}', which is a reserved prefix.,
403403
Category: SpacetimeDB,
@@ -414,7 +414,7 @@ public partial struct TestScheduleIssues
414414
Message: Reducer method __ReducerWithReservedPrefix starts with '__', which is a reserved prefix.,
415415
Severity: Error,
416416
Descriptor: {
417-
Id: STDB0009,
417+
Id: STDB0010,
418418
Title: Reducer method has a reserved name prefix,
419419
MessageFormat: Reducer method {0} starts with '{1}', which is a reserved prefix.,
420420
Category: SpacetimeDB,
@@ -427,7 +427,7 @@ public partial struct TestScheduleIssues
427427
Message: Several reducers are assigned to the same lifecycle kind Init: Reducers.TestDuplicateReducerKind1, Reducers.TestDuplicateReducerKind2,
428428
Severity: Error,
429429
Descriptor: {
430-
Id: STDB0011,
430+
Id: STDB0013,
431431
Title: Multiple reducers of the same kind,
432432
MessageFormat: Several reducers are assigned to the same lifecycle kind {0}: {1},
433433
Category: SpacetimeDB,
@@ -483,7 +483,7 @@ public partial struct TestScheduleIssues
483483
Message: Field MY_FILTER is marked as [ClientVisibilityFilter] but it is not public static readonly,
484484
Severity: Error,
485485
Descriptor: {
486-
Id: STDB0016,
486+
Id: STDB0018,
487487
Title: ClientVisibilityFilters must be public static readonly,
488488
MessageFormat: Field {0} is marked as [ClientVisibilityFilter] but it is not public static readonly,
489489
Category: SpacetimeDB,
@@ -500,7 +500,7 @@ public partial struct TestScheduleIssues
500500
Message: Field MY_SECOND_FILTER is marked as [ClientVisibilityFilter] but it is not public static readonly,
501501
Severity: Error,
502502
Descriptor: {
503-
Id: STDB0016,
503+
Id: STDB0018,
504504
Title: ClientVisibilityFilters must be public static readonly,
505505
MessageFormat: Field {0} is marked as [ClientVisibilityFilter] but it is not public static readonly,
506506
Category: SpacetimeDB,
@@ -517,7 +517,7 @@ public partial struct TestScheduleIssues
517517
Message: Field MY_THIRD_FILTER is marked as ClientVisibilityFilter but it has type string which is not SpacetimeDB.Filter,
518518
Severity: Error,
519519
Descriptor: {
520-
Id: STDB0015,
520+
Id: STDB0017,
521521
Title: ClientVisibilityFilters must be Filters,
522522
MessageFormat: Field {0} is marked as ClientVisibilityFilter but it has type {1} which is not SpacetimeDB.Filter,
523523
Category: SpacetimeDB,

crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,32 @@ Timestamp time
3838
}
3939
}
4040

41+
public sealed record ProcedureContext : Internal.IProcedureContext
42+
{
43+
public readonly Identity Sender;
44+
public readonly ConnectionId? ConnectionId;
45+
public readonly Random Rng;
46+
public readonly Timestamp Timestamp;
47+
public readonly AuthCtx SenderAuth;
48+
49+
// We need this property to be non-static for parity with client SDK.
50+
public Identity Identity => Internal.IProcedureContext.GetIdentity();
51+
52+
internal ProcedureContext(
53+
Identity identity,
54+
ConnectionId? connectionId,
55+
Random random,
56+
Timestamp time
57+
)
58+
{
59+
Sender = identity;
60+
ConnectionId = connectionId;
61+
Rng = random;
62+
Timestamp = time;
63+
SenderAuth = AuthCtx.BuildFromSystemTables(connectionId, identity);
64+
}
65+
}
66+
4167
public sealed record ViewContext : DbContext<Internal.LocalReadOnly>, Internal.IViewContext
4268
{
4369
public Identity Sender { get; }
@@ -1646,6 +1672,10 @@ public static void Main()
16461672
SpacetimeDB.Internal.Module.SetAnonymousViewContextConstructor(
16471673
() => new SpacetimeDB.AnonymousViewContext(new SpacetimeDB.Internal.LocalReadOnly())
16481674
);
1675+
SpacetimeDB.Internal.Module.SetProcedureContextConstructor(
1676+
(identity, connectionId, random, time) =>
1677+
new SpacetimeDB.ProcedureContext(identity, connectionId, random, time)
1678+
);
16491679
var __memoryStream = new MemoryStream();
16501680
var __writer = new BinaryWriter(__memoryStream);
16511681

@@ -1731,6 +1761,32 @@ SpacetimeDB.Internal.BytesSink error
17311761
error
17321762
);
17331763

1764+
[UnmanagedCallersOnly(EntryPoint = "__call_procedure__")]
1765+
public static SpacetimeDB.Internal.Errno __call_procedure__(
1766+
uint id,
1767+
ulong sender_0,
1768+
ulong sender_1,
1769+
ulong sender_2,
1770+
ulong sender_3,
1771+
ulong conn_id_0,
1772+
ulong conn_id_1,
1773+
SpacetimeDB.Timestamp timestamp,
1774+
SpacetimeDB.Internal.BytesSource args,
1775+
SpacetimeDB.Internal.BytesSink result_sink
1776+
) =>
1777+
SpacetimeDB.Internal.Module.__call_procedure__(
1778+
id,
1779+
sender_0,
1780+
sender_1,
1781+
sender_2,
1782+
sender_3,
1783+
conn_id_0,
1784+
conn_id_1,
1785+
timestamp,
1786+
args,
1787+
result_sink
1788+
);
1789+
17341790
[UnmanagedCallersOnly(EntryPoint = "__call_view__")]
17351791
public static SpacetimeDB.Internal.Errno __call_view__(
17361792
uint id,

crates/bindings-csharp/Codegen/Diag.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ IEnumerable<string> fullNames
8080
method => method.ParameterList
8181
);
8282

83+
public static readonly ErrorDescriptor<MethodDeclarationSyntax> ProcedureContextParam =
84+
new(
85+
group,
86+
"Procedures must have a first argument of type ProcedureContext",
87+
method =>
88+
$"Procedure method {method.Identifier} does not have a ProcedureContext parameter.",
89+
method => method.ParameterList
90+
);
91+
8392
public static readonly ErrorDescriptor<(
8493
MethodDeclarationSyntax method,
8594
string prefix
@@ -92,6 +101,18 @@ string prefix
92101
ctx => ctx.method.Identifier
93102
);
94103

104+
public static readonly ErrorDescriptor<(
105+
MethodDeclarationSyntax method,
106+
string prefix
107+
)> ProcedureReservedPrefix =
108+
new(
109+
group,
110+
"Procedure method has a reserved name prefix",
111+
ctx =>
112+
$"Procedure method {ctx.method.Identifier} starts with '{ctx.prefix}', which is a reserved prefix.",
113+
ctx => ctx.method.Identifier
114+
);
115+
95116
public static readonly UnusedErrorDescriptor IncompatibleTableSchedule = new(group);
96117

97118
public static readonly ErrorDescriptor<(

0 commit comments

Comments
 (0)