Skip to content

Commit

Permalink
Extract helpers as table methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 5, 2024
1 parent e32366f commit 0b8604a
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand All @@ -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"
Expand Down

0 comments on commit 0b8604a

Please sign in to comment.