Skip to content

Commit

Permalink
fix bug with reader generation
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Sep 14, 2023
1 parent 64c8065 commit 8b8a981
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/FluentCommand.Generators/DataReaderFactoryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ private static void Execute(SourceProductionContext context, EntityClass entityC
private static bool SyntacticPredicate(SyntaxNode syntaxNode, CancellationToken cancellationToken)
{
return syntaxNode is ClassDeclarationSyntax
{ AttributeLists.Count: > 0 } classDeclaration
{ AttributeLists.Count: > 0 } classDeclaration
&& !classDeclaration.Modifiers.Any(SyntaxKind.AbstractKeyword)
&& !classDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword)
|| syntaxNode is RecordDeclarationSyntax
{ AttributeLists.Count: > 0 } recordDeclaration
{ AttributeLists.Count: > 0 } recordDeclaration
&& !recordDeclaration.Modifiers.Any(SyntaxKind.AbstractKeyword)
&& !recordDeclaration.Modifiers.Any(SyntaxKind.StaticKeyword);
}
Expand All @@ -78,12 +78,7 @@ private static EntityContext SemanticTransform(GeneratorAttributeSyntaxContext c
? InitializationMode.ObjectInitializer
: InitializationMode.Constructor;

var propertySymbols = targetSymbol
.GetMembers()
.Where(m => m.Kind == SymbolKind.Property)
.OfType<IPropertySymbol>()
.Where(IsIncluded)
.ToList();
var propertySymbols = GetProperties(targetSymbol);

if (mode == InitializationMode.ObjectInitializer)
{
Expand Down Expand Up @@ -143,6 +138,31 @@ private static EntityContext SemanticTransform(GeneratorAttributeSyntaxContext c
return new EntityContext(entityClass, diagnostics);
}

private static List<IPropertySymbol> GetProperties(INamedTypeSymbol targetSymbol)
{
var properties = new Dictionary<string, IPropertySymbol>();

var currentSymbol = targetSymbol;

// get nested properties
while (currentSymbol != null)
{
var propertySymbols = currentSymbol
.GetMembers()
.Where(m => m.Kind == SymbolKind.Property)
.OfType<IPropertySymbol>()
.Where(IsIncluded)
.Where(p => !properties.ContainsKey(p.Name));

foreach (var propertySymbol in propertySymbols)
properties.Add(propertySymbol.Name, propertySymbol);

currentSymbol = currentSymbol.BaseType;
}

return properties.Values.ToList();
}

private static EntityProperty CreateProperty(IPropertySymbol propertySymbol, string parameterName = null)
{
var propertyType = propertySymbol.Type.ToDisplayString();
Expand Down
10 changes: 10 additions & 0 deletions test/FluentCommand.Tests/Models/Truck.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace FluentCommand.Tests.Models;

[Table(nameof(Truck))]
public class Truck : Vehicle
{
public string Color { get; set; }
public override int Type { get; set; }
}
10 changes: 10 additions & 0 deletions test/FluentCommand.Tests/Models/Vehicle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations.Schema;

namespace FluentCommand.Tests.Models;

[Table(nameof(Vehicle))]
public class Vehicle
{
public string Name { get; set; }
public virtual int Type { get; set; }
}

0 comments on commit 8b8a981

Please sign in to comment.