Skip to content

Commit

Permalink
fix bug in GenerateAttributeGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
pwelter34 committed Sep 21, 2024
1 parent 91127d0 commit 06f13e2
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 24 deletions.
10 changes: 4 additions & 6 deletions src/FluentCommand.Generators/DataReaderFactoryGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Diagnostic> diagnostics)
{
if (targetSymbol == null)
return null;
Expand All @@ -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<Diagnostic>();

// constructor with same number of parameters as properties
var constructor = targetSymbol.Constructors.FirstOrDefault(c => c.Parameters.Length == propertySymbols.Count);
Expand All @@ -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<EntityProperty>();
Expand Down Expand Up @@ -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<IPropertySymbol> GetProperties(INamedTypeSymbol targetSymbol)
Expand Down
29 changes: 19 additions & 10 deletions src/FluentCommand.Generators/GenerateAttributeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<EntityClass>();
var diagnostics = new List<Diagnostic>();

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);
}
}
2 changes: 1 addition & 1 deletion src/FluentCommand.Generators/Models/EntityContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
namespace FluentCommand.Generators.Models;

public record EntityContext(
EntityClass EntityClass,
EquatableArray<EntityClass> EntityClasses,
EquatableArray<Diagnostic> Diagnostics
);
10 changes: 8 additions & 2 deletions src/FluentCommand.Generators/TableAttributeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<EntityClass>();
var diagnostics = new List<Diagnostic>();

var entityClass = CreateClass(context.TargetNode.GetLocation(), targetSymbol, diagnostics);
classes.Add(entityClass);

return new EntityContext(classes, diagnostics);
}
}
5 changes: 0 additions & 5 deletions test/FluentCommand.Entities/Brand.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
using FluentCommand.Attributes;
using FluentCommand.Entities;

[assembly: GenerateReader(typeof(Brand))]

namespace FluentCommand.Entities;

public class Brand
Expand Down
8 changes: 8 additions & 0 deletions test/FluentCommand.Entities/Product.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
5 changes: 5 additions & 0 deletions test/FluentCommand.Entities/ReaderGeneration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using FluentCommand.Attributes;
using FluentCommand.Entities;

[assembly: GenerateReader(typeof(Brand))]
[assembly: GenerateReader(typeof(Product))]

0 comments on commit 06f13e2

Please sign in to comment.