Customizations
Intellenum allows developers to tailor enum behavior to specific needs by utilizing customizable options.
Global customizations:
[assembly: IntellenumDefaults(customizations: Customizations.TreatNumberAsStringInSystemTextJson)]
: This customization instructs Intellenum to treat enums as strings when serializing them with System.Text.Json. This is particularly useful for scenarios where enums need to be represented as strings in JSON payloads. Source:tests/SnapshotTests/Config/GlobalConfigTests.cs
Example:
using System;
using Intellenum;
[assembly: IntellenumDefaults(customizations: Customizations.TreatNumberAsStringInSystemTextJson)]
namespace Whatever;
[Intellenum]
[Member("Normal", 0)]
[Member("Gold", 1)]
public partial class CustomerType { }
Enum customizations:
[Member("Freezing", 0.0f)]
: This attribute allows developers to specify the underlying value of an enum member, overriding the default integer-based value. This is helpful for scenarios where enums need to represent a specific value, such as a floating-point number, rather than an integer. Source:samples/Intellenum.Examples/TypicalScenarios/MembersScenario.cs
[Member("Unspecified", "[UNSPCFD]")]
: This attribute provides a way to associate a string value with an enum member. This is useful for situations where enums need to be represented as specific strings in data serialization, like when using JSON or XML. Source:samples/Intellenum.Examples/TypicalScenarios/MembersScenario.cs
[Member("Salt", 1)]
: This attribute defines an enum member with both an integer and a string value. This can be handy for scenarios where the enum needs to be represented both as a string and an integer. Source:samples/Intellenum.Examples/TypicalScenarios/MembersScenario.cs
Members("Salt, Pepper, Vinegar, Mustard, Mayo, Ketchup")
: This allows developers to declare multiple enum members through a single line of code. This provides a more concise and efficient way to define enums with a large number of members. Source:samples/Intellenum.Examples/TypicalScenarios/MembersScenario.cs
Examples:
[Intellenum]
[Member("Freezing", 0.0f)]
[Member("Boiling", 100.0f)]
[Member("AbsoluteZero", -273.15f)]
public partial class Centigrade;
[Intellenum]
[Member("Unspecified", "[UNSPCFD]")]
[Member("Invalid", "[INVLD]")]
[Member("Standard", "[STD]")]
[Member("Preferred", "[PRF]")]
public partial class VendorType;
[Intellenum]
[Member("Salt", 1)]
[Member("Pepper", 2)]
public partial class CondimentMixedMembers
{
public static readonly CondimentMixedMembers Mayo = new CondimentMixedMembers("Mayo", 5);
public static readonly CondimentMixedMembers Ketchup = new CondimentMixedMembers("Ketchup", 6);
static CondimentMixedMembers()
{
Member("Vinegar", 3);
Member("Mustard", 4);
}
}
Customizations validation:
Validate(ImmutableArray diagnostics)
: Intellenum includes validation to ensure that specified customizations are valid. If an invalid customization is detected, an error message is generated. Source:tests/AnalyzerTests/GlobalConfig/SadTests.cs
Example:
[Fact]
public async Task Not_valid_customization()
{
var source = @"using System;
using Intellenum;
[assembly: IntellenumDefaults(customizations: (Customizations)666)]
namespace Whatever;
[Intellenum]
[Member(""Normal"", 0)]
[Member(""Gold"", 1)]
public partial struct CustomerType { }
";
await new TestRunner()
.WithSource(source)
.ValidateWith(Validate)
.RunOnAllFrameworks();
void Validate(ImmutableArray diagnostics)
{
diagnostics.Should().HaveCount(1);
diagnostics.Should().SatisfyRespectively(
first =>
{
first.Id.Should().Be("INTELLENUM019");
first.ToString().Should().Be(
"(4,12): error INTELLENUM019: The Customizations specified do not match any known customizations - see the Customizations type");
});
}
}
Key points:
- Intellenum provides various customizable options for tailoring enums. Source:
README.md
- Global customizations apply at the assembly level. Source:
tests/SnapshotTests/Config/GlobalConfigTests.cs
- Enum customizations apply to individual enums. Source:
samples/Intellenum.Examples/TypicalScenarios/MembersScenario.cs
- Validations ensure that only valid customization options are used. Source:
tests/AnalyzerTests/GlobalConfig/SadTests.cs
Top-Level Directory Explanations
samples/ - This directory contains example projects demonstrating the usage of Intellenum.
samples/Intellenum.Examples/ - Contains various example projects demonstrating different aspects of Intellenum, such as serialization, conversion, syntax examples, types, typical scenarios, and more.
samples/WebApplication/ - Contains a sample web application that uses Intellenum.
src/ - This directory contains the source code of the Intellenum library.
src/Benchmarks/ - Contains benchmark tests for the Intellenum library.
src/Intellenum.CodeFixers/ - Contains code fixers for the Intellenum library.
src/Intellenum.SharedTypes/ - Contains shared types used across the Intellenum library.
src/Intellenum/ - Contains the main source code for the Intellenum library. This directory is further divided into subdirectories for diagnostics, extensions, generators, member building, properties, rules, static constructor building, templates, and more.
tests/ - This directory contains test projects for the Intellenum library.
tests/AnalyzerTests/ - Contains unit tests for the Intellenum analyzer.
tests/ConsumerTests/ - Contains tests for consuming the Intellenum library.
tests/Intellenum.Tests/ - Contains additional tests for the Intellenum library.
tests/Shared/ - Contains shared test files.
tests/SnapshotTests/ - Contains snapshot tests for the Intellenum library.