diff --git a/src/FluentCommand.Generators/DataReaderFactoryGenerator.cs b/src/FluentCommand.Generators/DataReaderFactoryGenerator.cs index 38ecb4cf..0fcaf345 100644 --- a/src/FluentCommand.Generators/DataReaderFactoryGenerator.cs +++ b/src/FluentCommand.Generators/DataReaderFactoryGenerator.cs @@ -25,7 +25,7 @@ protected static void WriteSource(SourceProductionContext context, EntityClass e context.AddSource($"{qualifiedName}DataReaderExtensions.g.cs", source); } - protected static EntityContext CreateContext(Location location, INamedTypeSymbol targetSymbol) + protected static EntityClass CreateClass(Location location, INamedTypeSymbol targetSymbol, List diagnostics) { if (targetSymbol == null) return null; @@ -47,11 +47,10 @@ protected static EntityContext CreateContext(Location location, INamedTypeSymbol .ToArray(); var entity = new EntityClass(mode, fullyQualified, classNamespace, className, propertyArray); - return new EntityContext(entity, []); + return entity; } // constructor initialization - var diagnostics = new List(); // constructor with same number of parameters as properties var constructor = targetSymbol.Constructors.FirstOrDefault(c => c.Parameters.Length == propertySymbols.Count); @@ -66,7 +65,7 @@ protected static EntityContext CreateContext(Location location, INamedTypeSymbol diagnostics.Add(constructorDiagnostic); - return new EntityContext(null, diagnostics); + return null; } var properties = new List(); @@ -95,8 +94,7 @@ protected static EntityContext CreateContext(Location location, INamedTypeSymbol properties.Add(property); } - var entityClass = new EntityClass(mode, fullyQualified, classNamespace, className, properties); - return new EntityContext(entityClass, diagnostics); + return new EntityClass(mode, fullyQualified, classNamespace, className, properties); } protected static List GetProperties(INamedTypeSymbol targetSymbol) diff --git a/src/FluentCommand.Generators/GenerateAttributeGenerator.cs b/src/FluentCommand.Generators/GenerateAttributeGenerator.cs index 683d7f72..d8eda9b8 100644 --- a/src/FluentCommand.Generators/GenerateAttributeGenerator.cs +++ b/src/FluentCommand.Generators/GenerateAttributeGenerator.cs @@ -24,7 +24,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) context.RegisterSourceOutput(diagnostics, ReportDiagnostic); var entityClasses = provider - .Select(static (item, _) => item.EntityClass) + .SelectMany(static (item, _) => item.EntityClasses) .Where(static item => item is not null); context.RegisterSourceOutput(entityClasses, WriteSource); @@ -40,17 +40,26 @@ private static EntityContext SemanticTransform(GeneratorAttributeSyntaxContext c if (context.Attributes.Length == 0) return null; - var attribute = context.Attributes[0]; - if (attribute == null) - return null; + var classes = new List(); + var diagnostics = new List(); - if (attribute.ConstructorArguments.Length != 1) - return null; + foreach (var attribute in context.Attributes) + { + if (attribute == null) + return null; - var comparerArgument = attribute.ConstructorArguments[0]; - if (comparerArgument.Value is not INamedTypeSymbol targetSymbol) - return null; + if (attribute.ConstructorArguments.Length != 1) + return null; + + var comparerArgument = attribute.ConstructorArguments[0]; + if (comparerArgument.Value is not INamedTypeSymbol targetSymbol) + return null; + + var entityClass = CreateClass(context.TargetNode.GetLocation(), targetSymbol, diagnostics); + if (entityClass != null) + classes.Add(entityClass); + } - return CreateContext(context.TargetNode.GetLocation(), targetSymbol); + return new EntityContext(classes, diagnostics); } } diff --git a/src/FluentCommand.Generators/Models/EntityContext.cs b/src/FluentCommand.Generators/Models/EntityContext.cs index 24c5a880..d4428f03 100644 --- a/src/FluentCommand.Generators/Models/EntityContext.cs +++ b/src/FluentCommand.Generators/Models/EntityContext.cs @@ -3,6 +3,6 @@ namespace FluentCommand.Generators.Models; public record EntityContext( - EntityClass EntityClass, + EquatableArray EntityClasses, EquatableArray Diagnostics ); diff --git a/src/FluentCommand.Generators/TableAttributeGenerator.cs b/src/FluentCommand.Generators/TableAttributeGenerator.cs index 68d6a4b2..1209e11f 100644 --- a/src/FluentCommand.Generators/TableAttributeGenerator.cs +++ b/src/FluentCommand.Generators/TableAttributeGenerator.cs @@ -26,7 +26,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) context.RegisterSourceOutput(diagnostics, ReportDiagnostic); var entityClasses = provider - .Select(static (item, _) => item.EntityClass) + .SelectMany(static (item, _) => item.EntityClasses) .Where(static item => item is not null); context.RegisterSourceOutput(entityClasses, WriteSource); @@ -49,6 +49,12 @@ private static EntityContext SemanticTransform(GeneratorAttributeSyntaxContext c if (context.TargetSymbol is not INamedTypeSymbol targetSymbol) return null; - return CreateContext(context.TargetNode.GetLocation(), targetSymbol); + var classes = new List(); + var diagnostics = new List(); + + var entityClass = CreateClass(context.TargetNode.GetLocation(), targetSymbol, diagnostics); + classes.Add(entityClass); + + return new EntityContext(classes, diagnostics); } } diff --git a/test/FluentCommand.Entities/Brand.cs b/test/FluentCommand.Entities/Brand.cs index af7411d6..78c33bce 100644 --- a/test/FluentCommand.Entities/Brand.cs +++ b/test/FluentCommand.Entities/Brand.cs @@ -1,8 +1,3 @@ -using FluentCommand.Attributes; -using FluentCommand.Entities; - -[assembly: GenerateReader(typeof(Brand))] - namespace FluentCommand.Entities; public class Brand diff --git a/test/FluentCommand.Entities/Product.cs b/test/FluentCommand.Entities/Product.cs new file mode 100644 index 00000000..53735589 --- /dev/null +++ b/test/FluentCommand.Entities/Product.cs @@ -0,0 +1,8 @@ +namespace FluentCommand.Entities; + +public class Product +{ + public int Id { get; set; } + public string Name { get; set; } + public string Description { get; set; } +} diff --git a/test/FluentCommand.Entities/ReaderGeneration.cs b/test/FluentCommand.Entities/ReaderGeneration.cs new file mode 100644 index 00000000..98605e89 --- /dev/null +++ b/test/FluentCommand.Entities/ReaderGeneration.cs @@ -0,0 +1,5 @@ +using FluentCommand.Attributes; +using FluentCommand.Entities; + +[assembly: GenerateReader(typeof(Brand))] +[assembly: GenerateReader(typeof(Product))]