Skip to content

Commit a67fa5a

Browse files
dmealingclaude
andcommitted
refactor(csharp): simplify isArray scalar/enum codegen branches
EntityGenerator.ScalarProperty: handle the array case with a single early return (List<T> + [Column]) and compute required/isValue once for the scalar path, removing the duplicated IsRequired/IsValueType calls and the abbreviated req/iv locals introduced alongside the array branch. DbContextGenerator: hoist the per-entity owner = Pascal(e.Name) above the field loops instead of recomputing it in each. Emitted output is byte-identical. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 339ebb0 commit a67fa5a

2 files changed

Lines changed: 24 additions & 29 deletions

File tree

server/csharp/MetaObjects.Codegen/Generators/DbContextGenerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public IEnumerable<EmittedFile> Generate(GenContext ctx)
3434
}
3535
foreach (var e in objects.Where(o => o.IsEntity() && !o.IsReadOnlyProjection()))
3636
{
37+
var owner = CSharpNaming.Pascal(e.Name);
3738
foreach (var f in e.Fields().Where(f => f.SubType == FIELD_SUBTYPE_OBJECT))
3839
if (OwnedTypeConfig(e, f, ctx) is { } cfg) modelLines.Add(cfg);
3940
foreach (var f in e.Fields().Where(f => f.SubType == FIELD_SUBTYPE_ENUM))
4041
{
41-
var owner = CSharpNaming.Pascal(e.Name);
4242
var prop = CSharpNaming.Pascal(f.Name);
4343
if (f.IsArray)
4444
{
@@ -57,7 +57,6 @@ public IEnumerable<EmittedFile> Generate(GenContext ctx)
5757
// .Property(...).ToJson() does not exist on PropertyBuilder<List<T>> (CS1061).
5858
foreach (var f in e.Fields().Where(f => f.IsArray && CSharpNaming.ScalarFor(f.SubType) is not null))
5959
{
60-
var owner = CSharpNaming.Pascal(e.Name);
6160
var prop = CSharpNaming.Pascal(f.Name);
6261
modelLines.Add($" modelBuilder.Entity<{owner}>().PrimitiveCollection(x => x.{prop});");
6362
}

server/csharp/MetaObjects.Codegen/Generators/EntityGenerator.cs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -205,39 +205,35 @@ private static string ScalarProperty(MetaObject owner, MetaField field, IReadOnl
205205
var baseType = CSharpNaming.ScalarFor(field.SubType)!;
206206
var propName = CSharpNaming.Pascal(field.Name);
207207

208-
var sb = new StringBuilder();
209-
if (withAttributes)
208+
// Array fields: emit List<T> with an empty-list initializer and only a [Column]
209+
// attribute — [Key]/[MaxLength]/[Required] are not meaningful for a List<T> jsonb
210+
// column (arrays are never PKs, and the list itself is never null in C#).
211+
if (field.IsArray)
210212
{
211-
if (field.IsArray)
212-
{
213-
// Array fields: only [Column]; [Key]/[MaxLength]/[Required] are not
214-
// meaningful for a List<T> jsonb column (arrays are never PKs).
215-
sb.AppendLine($" [Column(\"{CSharpNaming.Column(field)}\")]");
216-
}
217-
else
218-
{
219-
var required = CSharpNaming.IsRequired(owner, field);
220-
var isValue = CSharpNaming.IsValueType(baseType);
221-
if (pkFields.Count == 1 && pkFields[0] == field.Name)
222-
sb.AppendLine(" [Key]");
223-
sb.AppendLine($" [Column(\"{CSharpNaming.Column(field)}\")]");
224-
if (baseType == "string" && field.MaxLength is long max)
225-
sb.AppendLine($" [MaxLength({max})]");
226-
if (required && !isValue)
227-
sb.AppendLine(" [Required]");
228-
}
213+
var arr = new StringBuilder();
214+
if (withAttributes)
215+
arr.AppendLine($" [Column(\"{CSharpNaming.Column(field)}\")]");
216+
arr.Append($" public List<{baseType}> {propName} {{ get; set; }} = new();");
217+
return arr.ToString();
229218
}
230219

231-
if (field.IsArray)
220+
var required = CSharpNaming.IsRequired(owner, field);
221+
var isValue = CSharpNaming.IsValueType(baseType);
222+
223+
var sb = new StringBuilder();
224+
if (withAttributes)
232225
{
233-
sb.Append($" public List<{baseType}> {propName} {{ get; set; }} = new();");
234-
return sb.ToString();
226+
if (pkFields.Count == 1 && pkFields[0] == field.Name)
227+
sb.AppendLine(" [Key]");
228+
sb.AppendLine($" [Column(\"{CSharpNaming.Column(field)}\")]");
229+
if (baseType == "string" && field.MaxLength is long max)
230+
sb.AppendLine($" [MaxLength({max})]");
231+
if (required && !isValue)
232+
sb.AppendLine(" [Required]");
235233
}
236234

237-
var req = CSharpNaming.IsRequired(owner, field);
238-
var iv = CSharpNaming.IsValueType(baseType);
239-
var type = req ? baseType : baseType + "?";
240-
var init = req && !iv ? " = default!;" : string.Empty;
235+
var type = required ? baseType : baseType + "?";
236+
var init = required && !isValue ? " = default!;" : string.Empty;
241237
sb.Append($" public {type} {propName} {{ get; set; }}{init}");
242238
return sb.ToString();
243239
}

0 commit comments

Comments
 (0)