-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support enum naming strategies for DescriptionAttribute and Enu…
…mMemberAttribute (#1507)
- Loading branch information
Showing
17 changed files
with
523 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Riok.Mapperly/Configuration/ComponentModelDescriptionAttributeConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Riok.Mapperly.Configuration; | ||
|
||
/// <summary> | ||
/// Configuration class to represent <see cref="System.ComponentModel.DescriptionAttribute"/> | ||
/// </summary> | ||
public record ComponentModelDescriptionAttributeConfiguration(string? Description) | ||
{ | ||
public ComponentModelDescriptionAttributeConfiguration() | ||
: this((string?)null) { } | ||
} |
64 changes: 64 additions & 0 deletions
64
src/Riok.Mapperly/Descriptors/MappingBodyBuilders/BuilderContext/EnumMappingBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using System.ComponentModel; | ||
using System.Runtime.Serialization; | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Configuration; | ||
using Riok.Mapperly.Diagnostics; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext; | ||
|
||
public static class EnumMappingBuilder | ||
{ | ||
internal static string GetMemberName(MappingBuilderContext ctx, IFieldSymbol field) => | ||
GetMemberName(ctx, field, ctx.Configuration.Enum.NamingStrategy); | ||
|
||
private static string GetMemberName(MappingBuilderContext ctx, IFieldSymbol field, EnumNamingStrategy namingStrategy) | ||
{ | ||
return namingStrategy switch | ||
{ | ||
EnumNamingStrategy.MemberName => field.Name, | ||
EnumNamingStrategy.CamelCase => field.Name.ToCamelCase(), | ||
EnumNamingStrategy.PascalCase => field.Name.ToPascalCase(), | ||
EnumNamingStrategy.SnakeCase => field.Name.ToSnakeCase(), | ||
EnumNamingStrategy.UpperSnakeCase => field.Name.ToUpperSnakeCase(), | ||
EnumNamingStrategy.KebabCase => field.Name.ToKebabCase(), | ||
EnumNamingStrategy.UpperKebabCase => field.Name.ToUpperKebabCase(), | ||
EnumNamingStrategy.ComponentModelDescriptionAttribute => GetComponentModelDescription(ctx, field), | ||
EnumNamingStrategy.SerializationEnumMemberAttribute => GetEnumMemberValue(ctx, field), | ||
_ => throw new ArgumentOutOfRangeException($"{nameof(namingStrategy)} has an unknown value {namingStrategy}"), | ||
}; | ||
} | ||
|
||
private static string GetEnumMemberValue(MappingBuilderContext ctx, IFieldSymbol field) | ||
{ | ||
var name = ctx.AttributeAccessor.AccessFirstOrDefault<EnumMemberAttribute>(field)?.Value; | ||
if (name != null) | ||
return name; | ||
|
||
ctx.ReportDiagnostic( | ||
DiagnosticDescriptors.EnumNamingAttributeMissing, | ||
nameof(EnumMemberAttribute), | ||
field.Name, | ||
field.ConstantValue ?? "<unknown>" | ||
); | ||
return GetMemberName(ctx, field, EnumNamingStrategy.MemberName); | ||
} | ||
|
||
private static string GetComponentModelDescription(MappingBuilderContext ctx, IFieldSymbol field) | ||
{ | ||
var name = ctx | ||
.AttributeAccessor.AccessFirstOrDefault<DescriptionAttribute, ComponentModelDescriptionAttributeConfiguration>(field) | ||
?.Description; | ||
if (name != null) | ||
return name; | ||
|
||
ctx.ReportDiagnostic( | ||
DiagnosticDescriptors.EnumNamingAttributeMissing, | ||
nameof(DescriptionAttribute), | ||
field.Name, | ||
field.ConstantValue ?? "<unknown>" | ||
); | ||
return GetMemberName(ctx, field, EnumNamingStrategy.MemberName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.