Skip to content

skarllot/EnumUtilities

Repository files navigation

Enum Utilities

Build status OpenSSF Scorecard GitHub license Nuget Nuget

A source generator for C# that uses Roslyn to create extensions and parsers for enumerations

🏃 Quickstart   |   📖 Documentation   |   📦 NuGet


A source generator for C# that uses Roslyn to create extensions and parsers for enumerations, allowing to get a value associated to enum member or parse back from attribute value to enum member. All code generated at compile time thus avoid using reflection or boilerplate code.

Compatibility

Raiqub.Generators.EnumUtilities runs with Roslyn compiler so does not introduce a new dependency to your project besides a library containing the EnumGenerator attribute.

It requires the .NET 6 SDK or later, and consuming projects must target .NET 6 or later.

Documentation

This README aims to give a quick overview of some Raiqub Enum Utilities features. For deeper detail of available features, be sure also to check out Documentation Page.

Quickstart

Add the package to your application using

dotnet add package Raiqub.Generators.EnumUtilities

Adding the package will automatically add a marker attribute, [EnumGenerator], to your project.

To use the generator, add the [EnumGenerator] attribute to an enum. For example:

[EnumGenerator]
public enum Categories
{
   Electronics,
   Food,
   Automotive,
   Arts,
   BeautyCare,
   Fashion
}

This will generate 3 classes with the following methods:

  • CategoriesExtensions
    • ToStringFast(this Categories)
    • GetStringCount(this Categories)
    • IsDefined(this Categories)
    • InterlockedAdd(this ref Categories, int)
    • InterlockedDecrement(this ref Categories)
    • InterlockedIncrement(this ref Categories)
    • InterlockedCompareExchange(this ref Categories, Categories, Categories)
    • InterlockedExchange(this ref Categories, Categories)
    • Match(this Categories, ...)
  • CategoriesFactory
    • Parse(string, bool = false)
    • Parse(ReadOnlySpan, bool = false)
    • ParseOrNull(string?, bool = false)
    • TryParse(string?, bool, out Categories)
    • TryParse(string?, out Categories)
    • TryParse(string?, bool = false)
    • TryParse(ReadOnlySpan, bool, out Categories)
    • TryParse(ReadOnlySpan, out Categories)
    • TryParse(ReadOnlySpan, bool = false)
    • GetValues()
    • GetNames()
  • CategoriesValidation
    • IsDefined(Categories)
    • IsDefined(string?, StringComparison)
    • IsDefinedIgnoreCase(string?)
    • IsDefined(string?)

Bit flags enums are supported too:

[Flags]
[EnumGenerator]
public enum Colours
{
    Red = 1,
    Blue = 2,
    Green = 4,
}

Then 3 classes will be generated with the following methods:

  • ColoursExtensions
    • ToStringFast(this Colours)
    • HasFlagFast(this Colours, Colours)
    • GetStringCount(this Colours)
    • IsDefined(this Colours)
    • InterlockedAnd(this ref Colours, Colours)
    • InterlockedOr(this ref Colours, Colours)
    • InterlockedCompareExchange(this ref Colours, Colours, Colours)
    • InterlockedExchange(this ref Colours, Colours)
  • ColoursFactory
    • Parse(string, bool = false)
    • Parse(ReadOnlySpan, bool = false)
    • ParseOrNull(string?, bool = false)
    • TryParse(string?, bool, out Categories)
    • TryParse(string?, out Categories)
    • TryParse(string?, bool = false)
    • TryParse(ReadOnlySpan, bool, out Categories)
    • TryParse(ReadOnlySpan, out Categories)
    • TryParse(ReadOnlySpan, bool = false)
    • GetValues()
    • GetNames()
  • ColoursValidation
    • IsDefined(Colours)
    • IsDefined(string?, StringComparison)
    • IsDefinedIgnoreCase(string?)
    • IsDefined(string?)

All generated code are properly nullable annotated and removed from code coverage.

Supported members attributes

The following attributes are supported:

JSON Serialization

Besides the member name, supports the EnumMemberAttribute and JsonPropertyNameAttribute attributes.

Example:

[JsonConverterGenerator]
[JsonConverter(typeof(SeasonJsonConverter))]
public enum Season
{
    [EnumMember(Value = "\ud83c\udf31")]
    Spring = 1,
    [EnumMember(Value = "\u2600\ufe0f")]
    Summer,
    [EnumMember(Value = "\ud83c\udf42")]
    Autumn,
    [EnumMember(Value = "\u26c4")]
    Winter
}

This will generate the following JSON converter: SeasonJsonConverter.

Contributing

If something is not working for you or if you think that the source file should change, feel free to create an issue or Pull Request. I will be happy to discuss and potentially integrate your ideas!

License

See the LICENSE file for details.

About

A source generator for C# that uses Roslyn to create extensions and parsers for enumerations

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

17 stars

Watchers

2 watching

Forks

Sponsor this project

 

Contributors