From 8b8a981e746b64a408474e6a135508425606e960 Mon Sep 17 00:00:00 2001 From: Paul Welter Date: Thu, 14 Sep 2023 11:00:11 -0500 Subject: [PATCH] fix bug with reader generation --- .../DataReaderFactoryGenerator.cs | 36 ++++++++++++++----- test/FluentCommand.Tests/Models/Truck.cs | 10 ++++++ test/FluentCommand.Tests/Models/Vehicle.cs | 10 ++++++ 3 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 test/FluentCommand.Tests/Models/Truck.cs create mode 100644 test/FluentCommand.Tests/Models/Vehicle.cs diff --git a/src/FluentCommand.Generators/DataReaderFactoryGenerator.cs b/src/FluentCommand.Generators/DataReaderFactoryGenerator.cs index 3bc93a3b..f5094edd 100644 --- a/src/FluentCommand.Generators/DataReaderFactoryGenerator.cs +++ b/src/FluentCommand.Generators/DataReaderFactoryGenerator.cs @@ -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); } @@ -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() - .Where(IsIncluded) - .ToList(); + var propertySymbols = GetProperties(targetSymbol); if (mode == InitializationMode.ObjectInitializer) { @@ -143,6 +138,31 @@ private static EntityContext SemanticTransform(GeneratorAttributeSyntaxContext c return new EntityContext(entityClass, diagnostics); } + private static List GetProperties(INamedTypeSymbol targetSymbol) + { + var properties = new Dictionary(); + + var currentSymbol = targetSymbol; + + // get nested properties + while (currentSymbol != null) + { + var propertySymbols = currentSymbol + .GetMembers() + .Where(m => m.Kind == SymbolKind.Property) + .OfType() + .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(); diff --git a/test/FluentCommand.Tests/Models/Truck.cs b/test/FluentCommand.Tests/Models/Truck.cs new file mode 100644 index 00000000..8157fb2b --- /dev/null +++ b/test/FluentCommand.Tests/Models/Truck.cs @@ -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; } +} diff --git a/test/FluentCommand.Tests/Models/Vehicle.cs b/test/FluentCommand.Tests/Models/Vehicle.cs new file mode 100644 index 00000000..9d493894 --- /dev/null +++ b/test/FluentCommand.Tests/Models/Vehicle.cs @@ -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; } +}