Skip to content
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

Allow support for nested StrongleTypeIds #153

Open
Moo-Juice opened this issue Nov 7, 2024 · 7 comments
Open

Allow support for nested StrongleTypeIds #153

Moo-Juice opened this issue Nov 7, 2024 · 7 comments

Comments

@Moo-Juice
Copy link

Moo-Juice commented Nov 7, 2024

Really like this library and have began integrating it in to my engine. However, there's a part of my project I cannot use this library easily, and that's where the strongly typed Id could be used as a nested class.

Essentially, what i want to do is this:

public class Factory<T> where T : ISomeInterface
{
    [StronglyTypedId(generateJsonConverter: false, StronglyTypedIdBackingType.Guid)]
    public partial struct Handle { }
}

Is this sort of thing possible? If I need to make Factory<T> partial also, I'd not be against it.

@andrewlock
Copy link
Owner

Yes, this is supported in the latest version 🙂

public Task CanGenerateGenericVeryNestedIdInFileScopeNamespace()

@Moo-Juice
Copy link
Author

Moo-Juice commented Nov 8, 2024

Hi there, it appears I am using the latest version. When I add the handle, I get 2 errors. This is the relevant generated section:

image

And the two errors:

error CS0101: The namespace 'Jigsaw.Memory' already contains a definition for 'Factory'
error CS0416: 'Jigsaw.Memory.Factory<T>.Handle.HandleTypeConverter': an attribute argument cannot use type parameters

I am probably doing something obviously wrong. Thanks for you assistance! :)

Oh, and the original source:

image

@andrewlock
Copy link
Owner

Ah, add partial to your Factory<T> type 🙂

@Moo-Juice
Copy link
Author

Moo-Juice commented Nov 8, 2024

Thanks for your prompt response, but I still had this issue :)

[System.ComponentModel.TypeConverter(typeof(HandleTypeConverter))]

error CS0416: 'Jigsaw.Memory.Factory<T>.Handle.HandleTypeConverter': an attribute argument cannot use type parameters

image

Hope you're well :)

@andrewlock
Copy link
Owner

Yeah unfortunately, that's not valid c# to use that attribute, because the attribute is defined as a nested type, which means the full name has a generic type parameter in it, and you can't use that. Your only options are to remove the attribute or to not nest it in the generic unfortunately

@Moo-Juice
Copy link
Author

Ok, understood! Do I need to remove it from the generated file, or is there an option I can use to to have it removed as part of the source generation? Have a good day :)

@andrewlock
Copy link
Owner

Unfortunately you will need to use a custom template to remove the attribute from the generated code. You can read about how to use the codefix provider to create a custom template in the README and you can read more about what you can do with them here. Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants