SmartEnum icon indicating copy to clipboard operation
SmartEnum copied to clipboard

Add source generator

Open marinasundstrom opened this issue 2 years ago • 7 comments

This could be improved further with source generators:

Allowing you to just decorate a partial class like so:

using Ardalis.SmartEnum;

[SmartEnum]
public sealed partial class TestEnum
{
    [EnumMember]
    public static readonly TestEnum One;

    [EnumMember]
    public static readonly TestEnum Two;

    [EnumMember]
    public static readonly TestEnum Three;
}

Generated code:

using Ardalis.SmartEnum;

sealed partial class TestEnum : SmartEnum<TestEnum>
{
    private TestEnum(string name, int value) : base(name, value)
    {
        One = new TestEnum(nameof(One), 1);
        Two = new TestEnum(nameof(Two), 2);
        Three = new TestEnum(nameof(Three), 3);
    }
}

marinasundstrom avatar Apr 18 '22 14:04 marinasundstrom

I like it. Do you want to create one and make a PR?

ardalis avatar Apr 18 '22 14:04 ardalis

If nobody have requested to work on this, I'd like to volunteer. I don't know if this is the way to go about it as I'm new to contributing

wilsonrivera avatar Aug 05 '22 03:08 wilsonrivera

Go for it @wilsonrivera

ardalis avatar Aug 05 '22 15:08 ardalis

A few questions, since I'll be taking #293 too A) would you rather this to be its own package, something like SmartEnum.SourceGenerator where the user needs to opt-in or, B) would like it to be implicitly included in the SmartEnum package, the same way is done with System.Text.Json, where the source generator is other package and is just included as an analyzer with the aforementioned package?

If goes for option B, would it be ok to include the attributes in the SmartEnum or would rather they are auto generated too?

wilsonrivera avatar Aug 05 '22 16:08 wilsonrivera

Let's see what it looks like all in one, and if it seems too intrusive we can split it. I'm not sure what it's going to look like, yet, until you implement it, which is why I'm uncertain. Ok?

ardalis avatar Aug 05 '22 16:08 ardalis

Yeah, sounds good, I'll work on it and get a PR ASAP!

wilsonrivera avatar Aug 05 '22 16:08 wilsonrivera

See also https://github.com/ardalis/SmartEnum/pull/356

ardalis avatar Jan 16 '24 20:01 ardalis