Skip to content

Commit

Permalink
Helper for parsing enums from string
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinM85 committed Jun 28, 2024
1 parent 989149d commit 97e5d0f
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions src/Helpers/EnumHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static class EnumHelpers
{
if(string.IsNullOrEmpty(rawValue)) return null;

var type = typeof(T);
rawValue = ToEnumRawName<T>(rawValue!);
if(typeof(T).IsDefined(typeof(FlagsAttribute)))
{
Expand Down Expand Up @@ -58,12 +57,7 @@ public static class EnumHelpers
private static string ToEnumRawName<T>(string value) where T : struct, Enum
#endif
{
if(TryGetFieldValueName(typeof(T), value, out var val))
{
value = val;
}

return value;
return TryGetFieldValueName(typeof(T), value, out var val) ? val : value;
}

#if NET5_0_OR_GREATER
Expand All @@ -72,13 +66,7 @@ private static string ToEnumRawName<T>(string value) where T : struct, Enum
private static ReadOnlySpan<char> ToEnumRawName<T>(ReadOnlySpan<char> span) where T : struct, Enum
#endif
{
var value = span.ToString();
if(TryGetFieldValueName(typeof(T), value, out var val))
{
value = val;
}

return value.AsSpan();
return TryGetFieldValueName(typeof(T), span.ToString(), out var val) ? val.AsSpan() : span;
}

/// <summary>
Expand Down

0 comments on commit 97e5d0f

Please sign in to comment.