mirror of
https://github.com/Kareadita/Kavita.git
synced 2026-06-05 06:15:25 -04:00
OpenID Connect support (#3975)
Co-authored-by: DieselTech <30128380+DieselTech@users.noreply.github.com> Co-authored-by: majora2007 <josephmajora@gmail.com>
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Reflection;
|
||||
|
||||
namespace API.Extensions;
|
||||
|
||||
public static class EnumExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Extension on Enum.TryParse which also tried matching on the description attribute
|
||||
/// </summary>
|
||||
/// <returns>if a match was found</returns>
|
||||
/// <remarks>First tries Enum.TryParse then fall back to the more expensive operation</remarks>
|
||||
public static bool TryParse<TEnum>(string? value, out TEnum result) where TEnum : struct, Enum
|
||||
{
|
||||
result = default;
|
||||
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (Enum.TryParse(value, out result))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (var field in typeof(TEnum).GetFields(BindingFlags.Public | BindingFlags.Static))
|
||||
{
|
||||
var description = field.GetCustomAttribute<DescriptionAttribute>()?.Description;
|
||||
|
||||
if (!string.IsNullOrEmpty(description) &&
|
||||
string.Equals(description, value, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
result = (TEnum)field.GetValue(null)!;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user