StefH / FluentBuilder

A project which uses Source Generation to create a FluentBuilder for a specified model or DTO
MIT License
169 stars 15 forks source link

Builder generation fails if the target type has at least one ctor with parameters #30

Closed iam-mholle closed 2 years ago

iam-mholle commented 2 years ago

Due to the following check, an exception is thrown if the target DTO has at least one ctor with parameters, even if a parameterless one is available.

https://github.com/StefH/FluentBuilder/blob/main/src/FluentBuilderGenerator/FileGenerators/FluentBuilderClassesGenerator.cs#L247-L252

The condition should be changed to

classSymbol.NamedTypeSymbol.Constructors.IsEmpty || classSymbol.NamedTypeSymbol.Constructors.All(c => !c.Parameters.IsEmpty)

Writing out the thrown exception into the Error.g.cs file is great to allow errors to be analyzed - wouldn't it be good to also emit a compiler warning/error via GeneratorExecutionContext.ReportDiagnostic(...) for such an error? I think this would make the development experience even better.

Anyway, thank you for your work on this project!

StefH commented 2 years ago

@iam-mholle Can you create a PR and also add this specific test case as an example class like here: image