-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: source generated partials when using attributes derived from ValueObjectAttribute<T> #321
fix: source generated partials when using attributes derived from ValueObjectAttribute<T> #321
Conversation
@SteveDunn I see that the snapshot tests for generic attributes have been commented out. I'll add them back, potentially clean them up (if they're missing anything), and add my derived attribute test to it unless there's a reason for them to be commented out. Shouldn't be too out of scope of this PR. |
Holding off on this PR until #322 is completed |
Yes, I had a lot of trouble testing these. I tested them in other way though, like in the consumer tests. I can't remember the exact reason why there were disabled, although it will likely become clear when trying to run them 🙈 ! |
b11b363
to
282dfc9
Compare
Rebased onto |
Hey @SteveDunn, in case this got lost in your notifications (or you didn't get any) this is good to go 👌 |
Great - thank you @jammehcow - sorry for the slow reply (it did get lost in a mountain of other notifications this week!) Really appreciate your help - many thanks. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
This doesn't seem to be working for me. Did you try with a non-int value? I've tried: public class IdAttribute : ValueObjectAttribute
{
public IdAttribute() : base(typeof(long), Conversions.None) { }
} and: public class IdAttribute : ValueObjectAttribute<long>
{
public IdAttribute() : base(Conversions.None) { }
} And the underlying type remains as |
@glen-84 Well you're right - that snapshot test fails if the type isn't |
I've got semi-working fix generating source for derived types but it requires you to copy the base constructor into your class which isn't ideal. Will try to figure out how to read parameter values of the bass class' constructor tomorrow, and give an explanation of why this feature didn't work. |
In Vogen's current state, parameters passed to the attribute's constructor are extracted from the constructor as constants - not pulled from some post-construction field inside of There's a small fix required to extract generic type values & parameters from derived attributes which I'll add today, as well as a note in the README about requiring default parameter values in derived constructors. I guess the real fix here would be to compile the attribute class if it's not one of Vogen's ones, then extract the values passed to |
@jupjohn @SteveDunn Can I get confirmation this should be working? public class CustomValueObjectAttribute<T> : ValueObjectAttribute<T>
{
public CustomValueObjectAttribute(
Conversions conversions = Conversions.Default | Conversions.EfCoreValueConverter,
CastOperator toPrimitiveCasting = CastOperator.Implicit,
CastOperator fromPrimitiveCasting = CastOperator.Implicit,
ComparisonGeneration comparisonGeneration = ComparisonGeneration.Omit)
{ }
} This is currently failing with InvalidCastException. [CustomValueObject<Guid>]
public readonly partial record struct ConversationId
{
}
Vogen |
Hi, yes, it should work. The whole deriving from attributes scenario is a bit up in the air. Ideally, people wouldn't derive, and that'd open up the possibility of using a faster Roslyn generator. It'd also make the Vogen code easier. But I can see the compelling reason to derive. |
Not to get too in the weeds of it (and keep in mind I don't know all of the limitations of source generators), but what if that derived attribute complexity was yanked out of the existing generator and moved to something that source generated partials with their corresponding Food for thought - I don't know if you can walk the syntax of another generator's output. |
Hi @jupjohn - unfortunately, it's not possible to combine or set dependencies on different source generators. There's a thread here: dotnet/roslyn#57239 |
This PR aims to fix #319 where struct/class partials were not being generated when using custom attributes inheriting
ValueObjectAttribute<T>
. Root cause of this is explained in this comment: #319 (comment)TODO:
BaseType
check on attribute'sBaseType
ValueObjectAttribute<T>
generate the same source code as direct usage ofValueObjectAttribute<T>