-
Notifications
You must be signed in to change notification settings - Fork 1
Add user-defined constraints #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,9 @@ internal class AliasCodeGenerator | |
| private readonly AliasModel _model; | ||
| private readonly StringBuilder _sb = new(); | ||
|
|
||
| const string SingleIndent = " "; | ||
|
|
||
|
|
||
| public AliasCodeGenerator(AliasModel model) | ||
| { | ||
| _model = model; | ||
|
|
@@ -137,20 +140,36 @@ private void AppendField() | |
|
|
||
| private void AppendConstructors() | ||
| { | ||
| var indent = GetMemberIndent(); | ||
| var memberIndent = GetMemberIndent(); | ||
|
|
||
| // Constructor from aliased type (always emitted) | ||
| _sb.AppendLine($"{indent}/// <summary>Creates a new {_model.TypeName} from a {_model.AliasedTypeMinimalName}.</summary>"); | ||
| AppendMethodImplAttribute(indent); | ||
| _sb.AppendLine($"{indent}public {_model.TypeName}({_model.AliasedTypeFullName} value) => _value = value;"); | ||
| _sb.AppendLine($"{memberIndent}/// <summary>Creates a new {_model.TypeName} from a {_model.AliasedTypeMinimalName}.</summary>"); | ||
| AppendMethodImplAttribute(memberIndent); | ||
|
|
||
|
|
||
| if (_model.IncludeConstraints) | ||
| { | ||
| _sb.AppendLine($"{memberIndent}public {_model.TypeName}({_model.AliasedTypeFullName} value)"); | ||
| _sb.Append(memberIndent).Append('{').AppendLine(); | ||
|
|
||
| AppendConstraintChecker(SingleIndent, "value"); | ||
| _sb.Append(SingleIndent).Append(SingleIndent).Append(SingleIndent).AppendLine("_value = value;"); | ||
|
|
||
| _sb.Append(memberIndent).Append('}').AppendLine(); | ||
| } | ||
| else | ||
| { | ||
| _sb.AppendLine($"{memberIndent}public {_model.TypeName}({_model.AliasedTypeFullName} value) => _value = value;"); | ||
| } | ||
|
|
||
| _sb.AppendLine(); | ||
|
|
||
| // Forward constructors from the aliased type (conditionally) | ||
| if (!_model.SuppressConstructorForwarding) | ||
| { | ||
| foreach (var ctor in _model.ForwardedConstructors) | ||
| { | ||
| AppendForwardedConstructor(indent, ctor); | ||
| AppendForwardedConstructor(memberIndent, ctor); | ||
| } | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
@@ -668,7 +687,22 @@ private void AppendForwardedConstructor(string indent, ConstructorInfo ctor) | |
|
|
||
| _sb.AppendLine($"{indent}/// <summary>Forwards {_model.AliasedTypeMinimalName} constructor.</summary>"); | ||
| AppendMethodImplAttribute(indent); | ||
| _sb.AppendLine($"{indent}public {_model.TypeName}({parameters}) => _value = new {_model.AliasedTypeFullName}({arguments});"); | ||
| if (_model.IncludeConstraints) | ||
| { | ||
| const string valueName = "newValue"; | ||
| _sb.AppendLine($"{indent}public {_model.TypeName}({parameters})"); | ||
| _sb.Append(indent).Append('{').AppendLine(); | ||
| _sb.Append(indent).Append(SingleIndent).AppendLine($"var {valueName} = new {_model.AliasedTypeFullName}({arguments});"); | ||
|
|
||
| AppendConstraintChecker(SingleIndent, valueName); | ||
| _sb.Append(SingleIndent).Append(SingleIndent).Append(SingleIndent).AppendLine($"_value = {valueName};"); | ||
|
|
||
| _sb.Append(indent).Append('}').AppendLine(); | ||
| } | ||
| else | ||
| { | ||
| _sb.AppendLine($"{indent}public {_model.TypeName}({parameters}) => _value = new {_model.AliasedTypeFullName}({arguments});"); | ||
| } | ||
|
Comment on lines
+690
to
+705
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same indentation issue in forwarded constructor. The forwarded constructor has the same hardcoded indentation problem. Proposed fix if (_model.IncludeConstraints)
{
const string valueName = "newValue";
_sb.AppendLine($"{indent}public {_model.TypeName}({parameters})");
- _sb.Append(indent).Append('{').AppendLine();
- _sb.Append(indent).Append(SingleIndent).AppendLine($"var {valueName} = new {_model.AliasedTypeFullName}({arguments});");
-
- AppendConstraintChecker(SingleIndent, valueName);
- _sb.Append(SingleIndent).Append(SingleIndent).Append(SingleIndent).AppendLine($"_value = {valueName};");
-
- _sb.Append(indent).Append('}').AppendLine();
+ _sb.AppendLine($"{indent}{{");
+ var bodyIndent = indent + SingleIndent;
+ _sb.AppendLine($"{bodyIndent}var {valueName} = new {_model.AliasedTypeFullName}({arguments});");
+
+ AppendConstraintChecker(bodyIndent, valueName);
+ _sb.AppendLine($"{bodyIndent}_value = {valueName};");
+
+ _sb.AppendLine($"{indent}}}");
}🤖 Prompt for AI Agents |
||
| _sb.AppendLine(); | ||
| } | ||
|
|
||
|
|
@@ -691,6 +725,23 @@ private void AppendMethodImplAttribute(string indent) | |
| _sb.AppendLine(line); | ||
| } | ||
|
|
||
| private void AppendConstraintChecker(string indent, string valueName) | ||
| { | ||
| if (!_model.validValidationMethod) return; | ||
|
|
||
| if (_model.DebugOnlyConstraints) | ||
| _sb.AppendLine("#if DEBUG"); | ||
|
|
||
| _sb.Append(indent).Append(indent).Append(indent) | ||
| .AppendLine($"if (!IsValid({valueName}))"); | ||
| _sb.Append(indent).Append(indent).Append(indent).Append(indent) | ||
| .AppendLine($"throw new InvalidOperationException($\"Failed validation check when trying to create '{_model.TypeName}' with '{_model.AliasedTypeMinimalName}' value: {{{valueName}}}\");"); // we heard you like interpolation | ||
|
|
||
|
|
||
| if (_model.DebugOnlyConstraints) | ||
| _sb.AppendLine("#endif"); | ||
| } | ||
|
Comment on lines
+728
to
+743
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The method receives an Proposed fix- private void AppendConstraintChecker(string indent, string valueName)
+ private void AppendConstraintChecker(string bodyIndent, string valueName)
{
if (!_model.validValidationMethod) return;
if (_model.DebugOnlyConstraints)
_sb.AppendLine("#if DEBUG");
- _sb.Append(indent).Append(indent).Append(indent)
- .AppendLine($"if (!IsValid({valueName}))");
- _sb.Append(indent).Append(indent).Append(indent).Append(indent)
- .AppendLine($"throw new InvalidOperationException($\"Failed validation check when trying to create '{_model.TypeName}' with '{_model.AliasedTypeMinimalName}' value: {{{valueName}}}\");");
+ _sb.AppendLine($"{bodyIndent}if (!IsValid({valueName}))");
+ _sb.AppendLine($"{bodyIndent}{SingleIndent}throw new InvalidOperationException($\"Failed validation check when trying to create '{_model.TypeName}' with '{_model.AliasedTypeMinimalName}' value: {{{valueName}}}\");");
if (_model.DebugOnlyConstraints)
_sb.AppendLine("#endif");
}🤖 Prompt for AI Agents |
||
|
|
||
| private static string FormatConstructorParameters(ConstructorInfo ctor) | ||
| { | ||
| return string.Join(", ", ctor.Parameters.Array.Select(p => | ||
|
|
@@ -728,7 +779,7 @@ private static string FormatConstructorArguments(ConstructorInfo ctor) | |
| })); | ||
| } | ||
|
|
||
| private string GetMemberIndent() => string.IsNullOrEmpty(_model.Namespace) ? " " : " "; | ||
| private string GetMemberIndent() => string.IsNullOrEmpty(_model.Namespace) ? SingleIndent : $"{SingleIndent}{SingleIndent}"; | ||
|
|
||
| private static string? GetOperatorSymbol(string operatorName) | ||
| { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent indentation in generated constructor body.
The
_value = value;assignment (line 156) uses threeSingleIndentlevels hardcoded, butmemberIndentalready accounts for namespace depth. When a namespace is present,memberIndentis 8 spaces, and the body should be indented one level deeper (12 spaces). However,SingleIndent * 3always produces 12 spaces regardless of namespace presence.For types without a namespace, the member indent is 4 spaces, so body lines should be 8 spaces, but this code emits 12.
Proposed fix
if (_model.IncludeConstraints) { _sb.AppendLine($"{memberIndent}public {_model.TypeName}({_model.AliasedTypeFullName} value)"); - _sb.Append(memberIndent).Append('{').AppendLine(); - - AppendConstraintChecker(SingleIndent, "value"); - _sb.Append(SingleIndent).Append(SingleIndent).Append(SingleIndent).AppendLine("_value = value;"); - - _sb.Append(memberIndent).Append('}').AppendLine(); + _sb.AppendLine($"{memberIndent}{{"); + + var bodyIndent = memberIndent + SingleIndent; + AppendConstraintChecker(bodyIndent, "value"); + _sb.AppendLine($"{bodyIndent}_value = value;"); + + _sb.AppendLine($"{memberIndent}}}"); }🤖 Prompt for AI Agents