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

Add source generator #355

Open
aljaff94 opened this issue Jan 23, 2023 · 0 comments
Open

Add source generator #355

aljaff94 opened this issue Jan 23, 2023 · 0 comments

Comments

@aljaff94
Copy link

aljaff94 commented Jan 23, 2023

Auto assign defined fields to name of field and incremental value start from 1
class must be defined as partial with Attribute [SmartEnumGenerator] and all fields must be static

example class with SmartEnumGenerator and no inheritance from SmartEnum

[SmartEnumGenerator]
public sealed partial class Permissions
{
    public static readonly Permissions Dashboard;
    public static readonly Permissions UserManagement;
}

generated code will be

namespace SmartEnum.SourceGenerator.UnitTests
{
    public sealed partial class Permissions  : SmartEnum <Permissions> 
    {
        public Permissions(string name, int value) : base(name, value) {}

        static Permissions()
        {
	   Dashboard = new Permissions(nameof(Dashboard), 1);
	   UserManagement = new Permissions(nameof(UserManagement), 2);
        }
    }
}

example class with SmartEnumGenerator and inheritanced from SmartEnum

namespace SmartEnum.SourceGenerator.UnitTests
{
    [SmartEnumGenerator]
    public sealed partial class Subscriptions : SmartEnum<Subscriptions>
    {
        public static readonly Subscriptions Free;
        public static readonly Subscriptions Sliver;
        public static readonly Subscriptions Gold;

        public Subscriptions(string name, int value) : base(name, value)
        {
        }
    }
}

generated code will be

namespace SmartEnum.SourceGenerator.UnitTests

{
    public sealed partial class Subscriptions  
    {
        static Subscriptions()
        {
	  Free = new Subscriptions(nameof(Free), 1);
	  Sliver = new Subscriptions(nameof(Sliver), 2);
	  Gold = new Subscriptions(nameof(Gold), 3);
        }
    }
}

Note: //in future we can define a custom attribute to ignore field
I'll do a pull request for this feature in separated package called SmartEnum.SourceGenerator

aljaff94 added a commit to aljaff94/SmartEnum that referenced this issue Jan 23, 2023
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

1 participant