Skip to content

Commit

Permalink
Fix getting properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Codelisk committed Aug 3, 2024
1 parent 2b58810 commit 9b56a0f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ namespace Foundation.Crawler.Extensions.New
{
public static class DtoExtensions
{
public static IEnumerable<PropertyDeclarationSyntax> DtoForeignProperties(
this ClassDeclarationSyntax dto
public static IEnumerable<IPropertySymbol> DtoForeignProperties(
this ClassDeclarationSyntax dto,
SemanticModel semanticModel
)
{
var result = dto.GetAllProperties(true, false)
return dto.GetAllPropertiesWithBaseClass(semanticModel, true)
.Where(x => x.HasAttribute(AttributeNames.ForeignKey));
return result;
}

public static IncrementalValueProvider<ImmutableArray<ClassDeclarationSyntax>> Dtos(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,46 +65,58 @@ public static string GetNamespace(this ClassDeclarationSyntax classDeclaration)
return null;
}

public static List<PropertyDeclarationSyntax> GetAllProperties(
public static IEnumerable<IPropertySymbol>? GetAllPropertiesWithBaseClass(
this ClassDeclarationSyntax classDeclaration,
bool withBaseTypes,
SemanticModel semanticModel,
bool onlyPublic
)
{
var properties = new List<PropertyDeclarationSyntax>();
var currentClass = classDeclaration;
var allProperties = new List<IPropertySymbol>();

var classSymbol = semanticModel.GetDeclaredSymbol(classDeclaration) as INamedTypeSymbol;
if (classSymbol == null)
return null;

while (currentClass != null)
allProperties = classSymbol.GetAllProperties(true);
if (onlyPublic)
{
var classProperties = currentClass.Members.OfType<PropertyDeclarationSyntax>();
if (onlyPublic)
{
classProperties = classProperties.Where(p =>
p.Modifiers.Any(m => m.IsKind(SyntaxKind.PublicKeyword))
);
}
properties.AddRange(classProperties);
return allProperties.Where(p => p.DeclaredAccessibility == Accessibility.Public);
}

return allProperties;
}

if (!withBaseTypes)
break;
currentClass = currentClass.GetBaseClass();
private static IEnumerable<INamedTypeSymbol> GetBaseTypes(INamedTypeSymbol type)
{
var baseTypes = new List<INamedTypeSymbol>();
var currentType = type.BaseType;

while (currentType != null)
{
baseTypes.Add(currentType);
currentType = currentType.BaseType;
}

return properties.ToList();
return baseTypes;
}

private static ClassDeclarationSyntax GetBaseClass(
this ClassDeclarationSyntax classDeclaration
public static List<PropertyDeclarationSyntax> GetAllProperties(
this ClassDeclarationSyntax classDeclaration,
bool onlyPublic
)
{
var baseType = classDeclaration.BaseList?.Types.FirstOrDefault();
if (baseType == null)
return null;
var properties = new List<PropertyDeclarationSyntax>();

var root = classDeclaration.SyntaxTree.GetRoot();
return root.DescendantNodes()
.OfType<ClassDeclarationSyntax>()
.FirstOrDefault(c => c.Identifier.Text == baseType.Type.ToString());
var classProperties = classDeclaration.Members.OfType<PropertyDeclarationSyntax>();
if (onlyPublic)
{
classProperties = classProperties.Where(p =>
p.Modifiers.Any(m => m.IsKind(SyntaxKind.PublicKeyword))
);
}
properties.AddRange(classProperties);

return properties.ToList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ ClassDeclarationSyntax dto
result.AddConstructor();
var constructor = result.AddConstructor().AddParameter(dto.Identifier.Text);

var properties = dto.GetAllProperties(true, true);
var properties = dto.GetAllProperties(true);

constructor.WithBody(x =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ internal class ExtensionsGenerator : BaseGenerator
public override void Initialize(IncrementalGeneratorInitializationContext context)
{
var dtos = context.Dtos();
var compilationAndClasses = context.CompilationProvider.Combine(dtos);
context.RegisterSourceOutput(
dtos,
static (sourceProductionContext, dtos) =>
compilationAndClasses,
static (sourceProductionContext, compilationAndClasses) =>
{
var dtos = compilationAndClasses.Right;
var result = new List<CodeBuilder?>();
var nameSpace = dtos.First().GetNamespace();

var builder = CodeBuilder.Create(nameSpace);
Class(builder, dtos);
Class(builder, dtos, compilationAndClasses.Left);
result.Add(builder);

var codeBuildersTuples = new List<(
Expand All @@ -48,7 +50,8 @@ public override void Initialize(IncrementalGeneratorInitializationContext contex

private static List<CodeBuilder?> Class(
CodeBuilder builder,
ImmutableArray<ClassDeclarationSyntax> dtos
ImmutableArray<ClassDeclarationSyntax> dtos,
Compilation compilation
)
{
var result = new List<CodeBuilder?>();
Expand All @@ -61,7 +64,8 @@ ImmutableArray<ClassDeclarationSyntax> dtos

foreach (var dto in dtos)
{
var properties = dto.GetAllProperties(true, true);
var semanticModel = compilation.GetSemanticModel(dto.SyntaxTree);
var properties = dto.GetAllPropertiesWithBaseClass(semanticModel, true);
var dtoName = dto.GetName();
var entityName = dto.GetEntityName();
extensionsClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ public class FullModelGenerator : BaseGenerator
public override void Initialize(IncrementalGeneratorInitializationContext context)
{
var dtos = context.Dtos();
var compilationAndClasses = context.CompilationProvider.Combine(dtos);
context.RegisterSourceOutput(
dtos,
static (sourceProductionContext, dtos) =>
compilationAndClasses,
static (sourceProductionContext, compilationAndClasses) =>
{
var result = new List<CodeBuilder?>();
var nameSpace = dtos.First().GetNamespace();
foreach (var dto in dtos)
var nameSpace = compilationAndClasses.Right.First().GetNamespace();
foreach (var dto in compilationAndClasses.Right)
{
var builder = CodeBuilder.Create(nameSpace);
Class(builder, dto, dtos);
Class(
builder,
dto,
compilationAndClasses.Right,
compilationAndClasses.Left
);
result.Add(builder);
}
var codeBuildersTuples = new List<(
Expand All @@ -51,7 +57,8 @@ public override void Initialize(IncrementalGeneratorInitializationContext contex
private static IReadOnlyList<ClassBuilder> Class(
CodeBuilder builder,
ClassDeclarationSyntax dto,
ImmutableArray<ClassDeclarationSyntax> dtos
ImmutableArray<ClassDeclarationSyntax> dtos,
Compilation compilation
)
{
var result = builder
Expand All @@ -63,8 +70,10 @@ ImmutableArray<ClassDeclarationSyntax> dtos
.AddProperty(dto.GetName().GetParameterName(), Accessibility.Public)
.SetType(dto.GetName())
.UseAutoProps();

//Removed for performance result.AddDtoUsing(context);
var dtoPropertiesWithForeignKey = dto.DtoForeignProperties();
var semanticModel = compilation.GetSemanticModel(dto.SyntaxTree);
var dtoPropertiesWithForeignKey = dto.DtoForeignProperties(semanticModel);

foreach (var dtoProperty in dtoPropertiesWithForeignKey)
{
Expand Down

0 comments on commit 9b56a0f

Please sign in to comment.