|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | +using System.Runtime.Serialization; |
| 4 | + |
| 5 | +#if NET5_0_OR_GREATER |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +#endif |
| 8 | + |
| 9 | +namespace Microsoft.Kiota.Abstractions.Helpers |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Helper methods for enums |
| 13 | + /// </summary> |
| 14 | + public static class EnumHelpers |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Gets the enum value from the raw value |
| 18 | + /// </summary> |
| 19 | + /// <typeparam name="T">Enum type</typeparam> |
| 20 | + /// <param name="rawValue">Raw value</param> |
| 21 | + /// <returns></returns> |
| 22 | +#if NET5_0_OR_GREATER |
| 23 | + public static T? GetEnumValue<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(string rawValue) where T : struct, Enum |
| 24 | +#else |
| 25 | + public static T? GetEnumValue<T>(string rawValue) where T : struct, Enum |
| 26 | +#endif |
| 27 | + { |
| 28 | + if(string.IsNullOrEmpty(rawValue)) return null; |
| 29 | + |
| 30 | + rawValue = ToEnumRawName<T>(rawValue!); |
| 31 | + if(typeof(T).IsDefined(typeof(FlagsAttribute))) |
| 32 | + { |
| 33 | + ReadOnlySpan<char> valueSpan = rawValue.AsSpan(); |
| 34 | + int value = 0; |
| 35 | + while(valueSpan.Length > 0) |
| 36 | + { |
| 37 | + int commaIndex = valueSpan.IndexOf(','); |
| 38 | + ReadOnlySpan<char> valueNameSpan = commaIndex < 0 ? valueSpan : valueSpan.Slice(0, commaIndex); |
| 39 | + valueNameSpan = ToEnumRawName<T>(valueNameSpan); |
| 40 | +#if NET6_0_OR_GREATER |
| 41 | + if(Enum.TryParse<T>(valueNameSpan, true, out var result)) |
| 42 | +#else |
| 43 | + if(Enum.TryParse<T>(valueNameSpan.ToString(), true, out var result)) |
| 44 | +#endif |
| 45 | + value |= (int)(object)result; |
| 46 | + valueSpan = commaIndex < 0 ? ReadOnlySpan<char>.Empty : valueSpan.Slice(commaIndex + 1); |
| 47 | + } |
| 48 | + return (T)(object)value; |
| 49 | + } |
| 50 | + else |
| 51 | + return Enum.TryParse<T>(rawValue, true, out var result) ? result : null; |
| 52 | + } |
| 53 | + |
| 54 | +#if NET5_0_OR_GREATER |
| 55 | + private static string ToEnumRawName<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(string value) where T : struct, Enum |
| 56 | +#else |
| 57 | + private static string ToEnumRawName<T>(string value) where T : struct, Enum |
| 58 | +#endif |
| 59 | + { |
| 60 | + return TryGetFieldValueName(typeof(T), value, out var val) ? val : value; |
| 61 | + } |
| 62 | + |
| 63 | +#if NET5_0_OR_GREATER |
| 64 | + private static ReadOnlySpan<char> ToEnumRawName<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(ReadOnlySpan<char> span) where T : struct, Enum |
| 65 | +#else |
| 66 | + private static ReadOnlySpan<char> ToEnumRawName<T>(ReadOnlySpan<char> span) where T : struct, Enum |
| 67 | +#endif |
| 68 | + { |
| 69 | + return TryGetFieldValueName(typeof(T), span.ToString(), out var val) ? val.AsSpan() : span; |
| 70 | + } |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Gets the enum value from the raw value for the given type |
| 74 | + /// </summary> |
| 75 | + /// <param name="type">Enum type</param> |
| 76 | + /// <param name="rawValue">Raw value</param> |
| 77 | + /// <returns></returns> |
| 78 | +#if NET5_0_OR_GREATER |
| 79 | + public static object? GetEnumValue([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type? type, string rawValue) |
| 80 | +#else |
| 81 | + public static object? GetEnumValue(Type? type, string rawValue) |
| 82 | +#endif |
| 83 | + { |
| 84 | + object? result; |
| 85 | + if(type == null) |
| 86 | + { |
| 87 | + return null; |
| 88 | + } |
| 89 | + if(type.IsDefined(typeof(FlagsAttribute))) |
| 90 | + { |
| 91 | + int intValue = 0; |
| 92 | + while(rawValue.Length > 0) |
| 93 | + { |
| 94 | + int commaIndex = rawValue.IndexOf(','); |
| 95 | + var valueName = commaIndex < 0 ? rawValue : rawValue.Substring(0, commaIndex); |
| 96 | + if(TryGetFieldValueName(type, valueName, out var value)) |
| 97 | + { |
| 98 | + valueName = value; |
| 99 | + } |
| 100 | +#if NET5_0_OR_GREATER |
| 101 | + if(Enum.TryParse(type, valueName, true, out var enumPartResult)) |
| 102 | + intValue |= (int)enumPartResult!; |
| 103 | +#else |
| 104 | + try |
| 105 | + { |
| 106 | + intValue |= (int)Enum.Parse(type, valueName, true); |
| 107 | + } |
| 108 | + catch { } |
| 109 | +#endif |
| 110 | + |
| 111 | + rawValue = commaIndex < 0 ? string.Empty : rawValue.Substring(commaIndex + 1); |
| 112 | + } |
| 113 | + result = intValue > 0 ? Enum.Parse(type, intValue.ToString(), true) : null; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + if(TryGetFieldValueName(type, rawValue, out var value)) |
| 118 | + { |
| 119 | + rawValue = value; |
| 120 | + } |
| 121 | + |
| 122 | +#if NET5_0_OR_GREATER |
| 123 | + Enum.TryParse(type, rawValue, true, out object? enumResult); |
| 124 | + result = enumResult; |
| 125 | +#else |
| 126 | + try |
| 127 | + { |
| 128 | + result = Enum.Parse(type, rawValue, true); |
| 129 | + } |
| 130 | + catch |
| 131 | + { |
| 132 | + result = null; |
| 133 | + } |
| 134 | +#endif |
| 135 | + } |
| 136 | + return result; |
| 137 | + |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | +#if NET5_0_OR_GREATER |
| 142 | + private static bool TryGetFieldValueName([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type type, string rawValue, out string valueName) |
| 143 | +#else |
| 144 | + private static bool TryGetFieldValueName(Type type, string rawValue, out string valueName) |
| 145 | +#endif |
| 146 | + { |
| 147 | + valueName = string.Empty; |
| 148 | + foreach(var field in type.GetFields()) |
| 149 | + { |
| 150 | + if(field.GetCustomAttribute<EnumMemberAttribute>() is { } attr && rawValue.Equals(attr.Value, StringComparison.Ordinal)) |
| 151 | + { |
| 152 | + valueName = field.Name; |
| 153 | + return true; |
| 154 | + } |
| 155 | + } |
| 156 | + return false; |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments