diff --git a/crates/bindings-csharp/Codegen/Module.cs b/crates/bindings-csharp/Codegen/Module.cs index 7b45e5721fd..91401edaa99 100644 --- a/crates/bindings-csharp/Codegen/Module.cs +++ b/crates/bindings-csharp/Codegen/Module.cs @@ -156,6 +156,27 @@ public TableDeclaration(GeneratorAttributeSyntaxContext context) protected override ColumnDeclaration ConvertMember(IFieldSymbol field) => new(field); + private IEnumerable<(int Pos, ColumnDeclaration Column)> GetColumnsByConstraint( + ColumnAttrs constraint + ) => + Members + .Select((Column, Pos) => (Pos, Column)) + .Where(pair => pair.Column.Attrs.HasFlag(constraint)); + + // Reimplementation of V8 -> V9 constraint conversion in Rust. + // See https://github.com/clockworklabs/SpacetimeDB/blob/13a800e9f88cbe885b98eab9e45b0fcfd3ab7014/crates/schema/src/def/validate/v8.rs#L74-L78 + // and https://github.com/clockworklabs/SpacetimeDB/blob/13a800e9f88cbe885b98eab9e45b0fcfd3ab7014/crates/lib/src/db/raw_def/v8.rs#L460-L510 + private string GenConstraintList(ColumnAttrs filterByAttr, string makeConstraintFn) => + $$""" + [ + {{string.Join( + ",\n", + GetColumnsByConstraint(filterByAttr) + .Select(pair => $"{makeConstraintFn}({pair.Pos}, nameof({pair.Column.Name}))") + )}} + ] + """; + public override Scope.Extensions ToExtensions() { var extensions = base.ToExtensions(); @@ -186,26 +207,8 @@ public override Scope.Extensions ToExtensions() extensions.BaseTypes.Clear(); extensions.BaseTypes.Add(iTable); - var columns = Members.Select((col, pos) => (col, pos)); - - // Reimplementation of V8 -> V9 constraint conversion in Rust. - // See https://github.com/clockworklabs/SpacetimeDB/blob/13a800e9f88cbe885b98eab9e45b0fcfd3ab7014/crates/schema/src/def/validate/v8.rs#L74-L78 - // and https://github.com/clockworklabs/SpacetimeDB/blob/13a800e9f88cbe885b98eab9e45b0fcfd3ab7014/crates/lib/src/db/raw_def/v8.rs#L460-L510 - string GenConstraintList(ColumnAttrs filterByAttr, string methodName) => - $$""" - [ - {{string.Join( - ",\n", - columns - .Where(pair => pair.col.Attrs.HasFlag(filterByAttr)) - .Select(pair => $"{iTable}.{methodName}({pair.pos}, nameof({pair.col.Name}))") - )}} - ] - """; - - var primaryKey = columns - .Where(pair => pair.col.Attrs.HasFlag(ColumnAttrs.PrimaryKey)) - .Select(pair => (int?)pair.pos) + var primaryKey = GetColumnsByConstraint(ColumnAttrs.PrimaryKey) + .Select(pair => (int?)pair.Pos) .SingleOrDefault(); extensions.Contents.Append( @@ -217,8 +220,8 @@ string GenConstraintList(ColumnAttrs filterByAttr, string methodName) => ProductTypeRef: (uint) new BSATN().GetAlgebraicType(registrar).Ref_, PrimaryKey: {{primaryKey?.ToString() ?? "null"}}, Indexes: [], - UniqueConstraints: {{GenConstraintList(ColumnAttrs.Unique, "MakeUniqueConstraint")}}, - Sequences: {{GenConstraintList(ColumnAttrs.AutoInc, "MakeSequence")}}, + UniqueConstraints: {{GenConstraintList(ColumnAttrs.Unique, $"{iTable}.MakeUniqueConstraint")}}, + Sequences: {{GenConstraintList(ColumnAttrs.AutoInc, $"{iTable}.MakeSequence")}}, Schedule: {{( Scheduled is null ? "null"