Skip to content

Commit

Permalink
Refactor code generation and update Namespace
Browse files Browse the repository at this point in the history
- Modified `Namespace.cs` to change `PostgresSettings` from class to record, adding a `required` modifier to `ConnectionString`.
- Updated `ConfigurationKeysGenerator.cs` to support a new parameter in `GenerateConfigurationKeysForType` method to avoid recursive type processing.
- Introduced a check to skip compiler-generated properties in `GenerateConfigurationKeysForType`.
- Implemented a mechanism to track and prevent infinite recursion by using a `HashSet<INamedTypeSymbol>` for processed types.
- Added cleanup logic to remove processed class symbols from the tracking set after processing.
  • Loading branch information
MCGPPeters committed Jun 21, 2024
1 parent 8e10dc2 commit 3432675
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Radix.Generators.Console/Namespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Radix.Generators.Console;


[Configuration]
public class PostgresSettings
public record PostgresSettings
{
public required string ConnectionString { get; init; }
}
18 changes: 15 additions & 3 deletions Radix.Generators/ConfigurationKeysGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private static string GenerateConfigurationKeysClass(Compilation compilation, st

var classSymbol = model.GetDeclaredSymbol(classSyntax);

GenerateConfigurationKeysForType(stringBuilder, classSymbol!, className);
GenerateConfigurationKeysForType(stringBuilder, classSymbol!, className, []);

stringBuilder.AppendLine(" }");
stringBuilder.AppendLine("}");
Expand All @@ -85,10 +85,20 @@ private static string GenerateConfigurationKeysClass(Compilation compilation, st
}


private static void GenerateConfigurationKeysForType(StringBuilder stringBuilder, INamedTypeSymbol classSymbol, string parentPath)
private static void GenerateConfigurationKeysForType(StringBuilder stringBuilder, INamedTypeSymbol classSymbol, string parentPath, HashSet<INamedTypeSymbol> processedTypes)
{
if (!processedTypes.Add(classSymbol))
{
// This type is already being processed, skip to avoid recursion
return;
}

foreach (var property in classSymbol.GetMembers().OfType<IPropertySymbol>())
{
// Skip compiler-generated properties, like for records
if (property.IsImplicitlyDeclared)
continue;

var propertyName = property.Name;
var propertyPath = $"{parentPath}:{propertyName}";

Expand All @@ -100,9 +110,11 @@ private static void GenerateConfigurationKeysForType(StringBuilder stringBuilder

if (property.Type.TypeKind == TypeKind.Class && property.Type.SpecialType != SpecialType.System_String)
{
GenerateConfigurationKeysForType(stringBuilder, (INamedTypeSymbol)property.Type, propertyPath);
GenerateConfigurationKeysForType(stringBuilder, (INamedTypeSymbol)property.Type, propertyPath, processedTypes);
}
}

processedTypes.Remove(classSymbol);
}

}

0 comments on commit 3432675

Please sign in to comment.