Skip to content

Commit

Permalink
Merge pull request #74 from dameng324/master
Browse files Browse the repository at this point in the history
add `TryParseFast` and fix `<see cref=`
  • Loading branch information
EngRajabi authored Mar 29, 2024
2 parents dd00eb7 + 99ba736 commit c320d0d
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 24 deletions.
87 changes: 63 additions & 24 deletions Supernova.Enum.Generators/EnumSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public sealed class {SourceGeneratorHelper.AttributeName}Attribute : Attribute
namespace {SourceGeneratorHelper.NameSpace}
{{
/// <summary>
/// Provides extension methods for operations related to the {symbol.Name} enumeration.
/// Provides extension methods for operations related to the <see cref=""global::{symbol.FullName()}"" /> enumeration.
/// </summary>
{symbol.DeclaredAccessibility.ToString("G").ToLower()} static class {symbol.Name}EnumExtensions
{{");
Expand Down Expand Up @@ -147,6 +147,10 @@ namespace {SourceGeneratorHelper.NameSpace}

//GetLength
GetLengthFast(sourceBuilder, symbol, e);


//TryParse
TryParseFast(sourceBuilder, e, symbol);

sourceBuilder.Append(@"
}
Expand All @@ -163,11 +167,11 @@ private static void ToDisplay(StringBuilder sourceBuilder, ISymbol symbol, EnumD
{
sourceBuilder.Append($@"
/// <summary>
/// Converts the <see cref=""{symbol.Name}"" /> enumeration value to its display string.
/// Converts the <see cref=""global::{symbol.FullName()}"" /> enumeration value to its display string.
/// </summary>
/// <param name=""states"">The <see cref=""{symbol.Name}"" /> enumeration value.</param>
/// <param name=""states"">The <see cref=""global::{symbol.FullName()}"" /> enumeration value.</param>
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
/// <returns>The display string of the <see cref=""{symbol.Name}"" /> value.</returns>
/// <returns>The display string of the <see cref=""global::{symbol.FullName()}"" /> value.</returns>
public static string {SourceGeneratorHelper.ExtensionMethodNameToDisplay}(this {symbol.FullName()} states, string defaultValue = null)
{{
return states switch
Expand Down Expand Up @@ -195,11 +199,11 @@ private static void ToDescription(StringBuilder sourceBuilder, ISymbol symbol, E
{
sourceBuilder.Append($@"
/// <summary>
/// Gets the description of the <see cref=""{symbol.Name}"" /> enumeration value.
/// Gets the description of the <see cref=""global::{symbol.FullName()}"" /> enumeration value.
/// </summary>
/// <param name=""states"">The <see cref=""{symbol.Name}"" /> enumeration value.</param>
/// <param name=""states"">The <see cref=""global::{symbol.FullName()}"" /> enumeration value.</param>
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
/// <returns>The description of the <see cref=""{symbol.Name}"" /> value.</returns>
/// <returns>The description of the <see cref=""global::{symbol.FullName()}"" /> value.</returns>
public static string {SourceGeneratorHelper.ExtensionMethodNameToDescription}(this {symbol.FullName()} states, string defaultValue = null)
{{
return states switch
Expand All @@ -226,10 +230,10 @@ private static void IsDefinedString(StringBuilder sourceBuilder, EnumDeclaration
{
sourceBuilder.Append($@"
/// <summary>
/// Checks if the specified string represents a defined <see cref=""{symbol.Name}"" /> value.
/// Checks if the specified string represents a defined <see cref=""global::{symbol.FullName()}"" /> value.
/// </summary>
/// <param name=""states"">The string representing a <see cref=""{symbol.Name}"" /> value.</param>
/// <returns>True if the string represents a defined <see cref=""{symbol.Name}"" /> value; otherwise, false.</returns>
/// <param name=""states"">The string representing a <see cref=""global::{symbol.FullName()}"" /> value.</param>
/// <returns>True if the string represents a defined <see cref=""global::{symbol.FullName()}"" /> value; otherwise, false.</returns>
public static bool {SourceGeneratorHelper.ExtensionMethodNameIsDefined}(string states)
{{
return states switch
Expand All @@ -248,10 +252,10 @@ private static void IsDefinedEnum(StringBuilder sourceBuilder, ISymbol symbol, E
{
sourceBuilder.Append($@"
/// <summary>
/// Checks if the specified <see cref=""{symbol.Name}"" /> value is defined.
/// Checks if the specified <see cref=""global::{symbol.FullName()}"" /> value is defined.
/// </summary>
/// <param name=""states"">The <see cref=""{symbol.Name}"" /> value to check.</param>
/// <returns>True if the <see cref=""{symbol.Name}"" /> value is defined; otherwise, false.</returns>
/// <param name=""states"">The <see cref=""global::{symbol.FullName()}"" /> value to check.</param>
/// <returns>True if the <see cref=""global::{symbol.FullName()}"" /> value is defined; otherwise, false.</returns>
public static bool {SourceGeneratorHelper.ExtensionMethodNameIsDefined}({symbol.FullName()} states)
{{
return states switch
Expand All @@ -270,11 +274,11 @@ private static void ToStringFast(StringBuilder sourceBuilder, ISymbol symbol, En
{
sourceBuilder.Append($@"
/// <summary>
/// Converts the <see cref=""{symbol.Name}"" /> enumeration value to its string representation.
/// Converts the <see cref=""global::{symbol.FullName()}"" /> enumeration value to its string representation.
/// </summary>
/// <param name=""states"">The <see cref=""{symbol.Name}"" /> enumeration value.</param>
/// <param name=""states"">The <see cref=""global::{symbol.FullName()}"" /> enumeration value.</param>
/// <param name=""defaultValue"">The default value to return if the enumeration value is not recognized.</param>
/// <returns>The string representation of the <see cref=""{symbol.Name}"" /> value.</returns>
/// <returns>The string representation of the <see cref=""global::{symbol.FullName()}"" /> value.</returns>
public static string {SourceGeneratorHelper.ExtensionMethodNameToString}(this {symbol.FullName()} states, string defaultValue = null)
{{
return states switch
Expand All @@ -294,7 +298,7 @@ private static void DisplayNamesDictionary(StringBuilder sourceBuilder, ISymbol
{
sourceBuilder.Append($@"
/// <summary>
/// Provides a dictionary that maps <see cref=""{symbol.Name}"" /> values to their corresponding display names.
/// Provides a dictionary that maps <see cref=""global::{symbol.FullName()}"" /> values to their corresponding display names.
/// </summary>
public static readonly ImmutableDictionary<{symbol.FullName()}, string> {SourceGeneratorHelper.PropertyDisplayNamesDictionary} = new Dictionary<{symbol.FullName()}, string>
{{
Expand All @@ -319,7 +323,7 @@ private static void DisplayDescriptionsDictionary(StringBuilder sourceBuilder, I
{
sourceBuilder.Append($@"
/// <summary>
/// Provides a dictionary that maps <see cref=""{symbol.Name}"" /> values to their corresponding descriptions.
/// Provides a dictionary that maps <see cref=""global::{symbol.FullName()}"" /> values to their corresponding descriptions.
/// </summary>
public static readonly ImmutableDictionary<{symbol.FullName()}, string> {SourceGeneratorHelper.PropertyDisplayDescriptionsDictionary} = new Dictionary<{symbol.FullName()}, string>
{{
Expand All @@ -343,9 +347,9 @@ private static void GetValuesFast(StringBuilder sourceBuilder, ISymbol symbol, E
{
sourceBuilder.Append($@"
/// <summary>
/// Retrieves an array of all <see cref=""{symbol.Name}"" /> enumeration values.
/// Retrieves an array of all <see cref=""global::{symbol.FullName()}"" /> enumeration values.
/// </summary>
/// <returns>An array containing all <see cref=""{symbol.Name}"" /> enumeration values.</returns>
/// <returns>An array containing all <see cref=""global::{symbol.FullName()}"" /> enumeration values.</returns>
public static {symbol.FullName()}[] {SourceGeneratorHelper.ExtensionMethodNameGetValues}()
{{
return new[]
Expand All @@ -363,9 +367,9 @@ private static void GetNamesFast(StringBuilder sourceBuilder, ISymbol symbol, En
{
sourceBuilder.Append($@"
/// <summary>
/// Retrieves an array of strings containing the names of all <see cref=""{symbol.Name}"" /> enumeration values.
/// Retrieves an array of strings containing the names of all <see cref=""global::{symbol.FullName()}"" /> enumeration values.
/// </summary>
/// <returns>An array of strings containing the names of all <see cref=""{symbol.Name}"" /> enumeration values.</returns>
/// <returns>An array of strings containing the names of all <see cref=""global::{symbol.FullName()}"" /> enumeration values.</returns>
public static string[] {SourceGeneratorHelper.ExtensionMethodNameGetNames}()
{{
return new[]
Expand All @@ -383,9 +387,9 @@ private static void GetLengthFast(StringBuilder sourceBuilder, ISymbol symbol, E
{
sourceBuilder.Append($@"
/// <summary>
/// Gets the length of the <see cref=""{symbol.Name}"" /> enumeration.
/// Gets the length of the <see cref=""global::{symbol.FullName()}"" /> enumeration.
/// </summary>
/// <returns>The length of the <see cref=""{symbol.Name}"" /> enumeration.</returns>
/// <returns>The length of the <see cref=""global::{symbol.FullName()}"" /> enumeration.</returns>
public static int {SourceGeneratorHelper.ExtensionMethodNameGetLength}()
{{
return {e.Members.Count};
Expand All @@ -395,4 +399,39 @@ private static void GetLengthFast(StringBuilder sourceBuilder, ISymbol symbol, E
}
");
}

private static void TryParseFast(StringBuilder sourceBuilder, EnumDeclarationSyntax e, ISymbol symbol)
{
sourceBuilder.Append($@"
/// <summary>
/// Try parse a string to <see cref=""global::{symbol.FullName()}"" /> value.
/// </summary>
/// <param name=""states"">The string representing a <see cref=""global::{symbol.FullName()}"" /> value.</param>
/// <param name=""result"">The enum <see cref=""global::{symbol.FullName()}"" /> parse result.</param>
/// <returns>True if the string is parsed successfully; otherwise, false.</returns>
public static bool {SourceGeneratorHelper.ExtensionMethodNameTryParse}(string states, out {symbol.FullName()} result)
{{
switch (states)
{{
");
foreach (var member in e.Members.Select(x => x.Identifier.ValueText))
sourceBuilder.AppendLine(
$$"""
case "{{member}}":
{
result = {{symbol.FullName()}}.{{member}};
return true;
}
""");
sourceBuilder.Append(
"""
default: {
result = default;
return false;
}
}
}
"""
);
}
}
1 change: 1 addition & 0 deletions Supernova.Enum.Generators/SourceGeneratorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static class SourceGeneratorHelper
public const string ExtensionMethodNameGetValues = "GetValuesFast";
public const string ExtensionMethodNameGetNames = "GetNamesFast";
public const string ExtensionMethodNameGetLength = "GetLengthFast";
public const string ExtensionMethodNameTryParse = "TryParseFast";
public const string PropertyDisplayNamesDictionary = "DisplayNamesDictionary";
public const string PropertyDisplayDescriptionsDictionary = "DisplayDescriptionsDictionary";

Expand Down
1 change: 1 addition & 0 deletions Supernova.Enum.Generators/Supernova.Enum.Generators.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
fix doc warning
add GeneratedCodeAttribute attrbiute
</PackageReleaseNotes>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions test/Console.Test.Benchmark/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ public int FastGetLength()
{
return UserTypeEnumExtensions.GetLengthFast();
}
[Benchmark]
public bool NativeTryParse()
{
return Enum.TryParse<UserType>("Men", out _);
}
[Benchmark]
public bool FastTryParse()
{
return UserTypeEnumExtensions.TryParseFast("Men",out _);
}
}

public static class Ext
Expand Down
11 changes: 11 additions & 0 deletions test/UnitTests/EnumGeneratorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,16 @@ public void TestEnumGetLength()
Assert.AreEqual(Enum.GetValues<UserTypeTest>().Length, length);
}

[TestMethod]
public void TestEnumTryParse()
{
var menString = "Men";
bool result = UserTypeTestEnumExtensions.TryParseFast(menString, out var enumValue);

Assert.IsTrue(result);
Assert.AreEqual(enumValue,UserTypeTest.Men);
}


private UserTypeTest GetUndefinedEnumValue() => (UserTypeTest)(-1);
}

0 comments on commit c320d0d

Please sign in to comment.