-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for enum naming strategy mappings
- Loading branch information
Showing
10 changed files
with
303 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Globalization; | ||
using System.Text.RegularExpressions; | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
|
||
namespace Riok.Mapperly.Helpers; | ||
|
||
public static class EnumNamingStrategyHelper | ||
{ | ||
public static Dictionary<IFieldSymbol, string> BuildCustomNameStrategyMappings( | ||
this ITypeSymbol enumSymbol, | ||
EnumNamingStrategy namingStrategy | ||
) | ||
{ | ||
var customNameMappings = new Dictionary<IFieldSymbol, string>(SymbolEqualityComparer.Default); | ||
|
||
var values = enumSymbol.GetMembers().OfType<IFieldSymbol>(); | ||
foreach (var value in values) | ||
{ | ||
var valueString = ConvertEnumValueNameToString(value.Name, namingStrategy); | ||
customNameMappings.Add(value, valueString); | ||
} | ||
|
||
return customNameMappings; | ||
} | ||
|
||
private static string ConvertEnumValueNameToString(string enumValueName, EnumNamingStrategy namingStrategy) | ||
{ | ||
return namingStrategy switch | ||
{ | ||
EnumNamingStrategy.PascalCase => enumValueName, | ||
EnumNamingStrategy.SnakeCase => JoinWordsFrom("_", enumValueName), | ||
EnumNamingStrategy.KebabCase => JoinWordsFrom("-", enumValueName), | ||
_ => throw new ArgumentOutOfRangeException($"{nameof(namingStrategy)} has an unknown value {namingStrategy}"), | ||
}; | ||
} | ||
|
||
private static string[] SplitEnumValueNameIntoWords(string enumValueName) | ||
{ | ||
// Matches (or): | ||
// [A-Z][a-z]+ | ||
// - A word starting with an uppercase letter, followed by lowercase letters ("Word" or "Word123") | ||
// [A-Z]+(?![a-z]) | ||
// - One or more uppercase letters, with no lowercase letters after ("ID" or "C1") | ||
// \d+ | ||
// - One or more digits ("123") | ||
// "Pascal1CaseID2_3" = "Pascal1" + "Case" + "ID2" + "3" | ||
#pragma warning disable MA0009 | ||
var words = Regex.Matches(enumValueName, @"[A-Z][a-z]+|[A-Z]+(?![a-z])|\d+"); | ||
#pragma warning restore MA0009 | ||
return (from Match word in words select word.Value).ToArray(); | ||
} | ||
|
||
private static string JoinWordsFrom(string separator, string enumValueName) | ||
{ | ||
var lowerCaseWords = SplitEnumValueNameIntoWords(enumValueName).Select(w => w.ToLower(CultureInfo.InvariantCulture)); | ||
return string.Join(separator, lowerCaseWords); | ||
} | ||
} |
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
Oops, something went wrong.