NetEscapades.EnumGenerators icon indicating copy to clipboard operation
NetEscapades.EnumGenerators copied to clipboard

Duplciate case label value

Open TrueBurn opened this issue 2 years ago • 3 comments

If you have an enum where one value is equal to another value then when you generate the extensions you get a CS8510 error.

Example Enum:

[EnumExtensions]
public enum GroupLevel
{
    Clear,  // Mapped to null
    Root = 1,
    Marketplace = 2,
    Partner = 3,
    Reseller = Partner,
    Company = 4,
    Customer = Company,
    BaseProduct = 5,
    Product = 6
}

Example of generated code:

public static string ToStringFast(this global::Redstor.ModelShared.Enums.GroupLevel value)
            => value switch
            {
                global::Redstor.ModelShared.Enums.GroupLevel.Clear => nameof(global::Redstor.ModelShared.Enums.GroupLevel.Clear),
                global::Redstor.ModelShared.Enums.GroupLevel.Root => nameof(global::Redstor.ModelShared.Enums.GroupLevel.Root),
                global::Redstor.ModelShared.Enums.GroupLevel.Marketplace => nameof(global::Redstor.ModelShared.Enums.GroupLevel.Marketplace),
                global::Redstor.ModelShared.Enums.GroupLevel.Partner => nameof(global::Redstor.ModelShared.Enums.GroupLevel.Partner),
                global::Redstor.ModelShared.Enums.GroupLevel.Reseller => nameof(global::Redstor.ModelShared.Enums.GroupLevel.Reseller),
                global::Redstor.ModelShared.Enums.GroupLevel.Company => nameof(global::Redstor.ModelShared.Enums.GroupLevel.Company),
                global::Redstor.ModelShared.Enums.GroupLevel.Customer => nameof(global::Redstor.ModelShared.Enums.GroupLevel.Customer),
                global::Redstor.ModelShared.Enums.GroupLevel.BaseProduct => nameof(global::Redstor.ModelShared.Enums.GroupLevel.BaseProduct),
                global::Redstor.ModelShared.Enums.GroupLevel.Product => nameof(global::Redstor.ModelShared.Enums.GroupLevel.Product),
                _ => value.ToString(),
            };

image

Error message: The pattern is unreachable. It has already been handled by a previous arm of the switch expression or it is impossible to match.

TrueBurn avatar Jun 14 '23 08:06 TrueBurn

Hi,

Is this not just because of the way you've structure your enum? You have Reseller = Partner, meaning Reseller == 3. By having this, you can never match to reseller because it would have already been matched to Partner first.

Take the example of running GroupLevel.Reseller.ToStringFast() - it would match on Partner and exit.

pippinmole avatar Oct 14 '23 19:10 pippinmole

Hi, sorry for the delay in getting to this. To be honest, I'm not sure the best way to handle it 🤔 It is annoying that it won't even compile...

Maybe the answer is to simply not generate the ToStringFast() method in this case 🤔 The inconsistency is a bit annoying though... Maybe we just can't support this kind of enum

andrewlock avatar May 14 '24 21:05 andrewlock