ardalis / ardalis/SmartEnum

Add source generator

Open
#355 0 comments 5 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2.4k
Forks
184
PR merge metrics
No merged PRs in 30d

Description

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
```csharp
[SmartEnumGenerator]
public sealed partial class Permissions
{
public static readonly Permissions Dashboard;
public static readonly Permissions UserManagement;
}
```
generated code will be
```csharp
namespace SmartEnum.SourceGenerator.UnitTests
{
public sealed partial class Permissions : SmartEnum
{
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
```csharp
namespace SmartEnum.SourceGenerator.UnitTests
{
[SmartEnumGenerator]
public sealed partial class Subscriptions : SmartEnum
{
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
```csharp
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

Contributor guide

Open the contributing guide

Research direction

Start with the issue's two C# examples and compare the requested generated output for inherited and non-inherited classes. Define completion as a separate SmartEnum.SourceGenerator package that generates the shown constructors, static initialization, names, and values, with tests covering both examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
devtools, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.