|
2 | 2 | // Licensed under the MIT license.
|
3 | 3 | using System;
|
4 | 4 | using System.Collections.Concurrent;
|
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Collections.ObjectModel; |
5 | 7 | using System.Diagnostics.CodeAnalysis;
|
6 | 8 | using System.Reflection;
|
7 | 9 | using Microsoft.OpenApi.Attributes;
|
| 10 | +using Microsoft.OpenApi.Models; |
| 11 | +using Microsoft.OpenApi.Reader; |
8 | 12 |
|
9 | 13 | namespace Microsoft.OpenApi.Extensions
|
10 | 14 | {
|
11 | 15 | /// <summary>
|
12 | 16 | /// String extension methods.
|
13 | 17 | /// </summary>
|
14 |
| - public static class StringExtensions |
| 18 | + internal static class StringExtensions |
15 | 19 | {
|
16 |
| - private static readonly ConcurrentDictionary<Type, ConcurrentDictionary<string, object>> EnumDisplayCache = new(); |
| 20 | + private static readonly ConcurrentDictionary<Type, ReadOnlyDictionary<string, object>> EnumDisplayCache = new(); |
17 | 21 |
|
18 |
| - /// <summary> |
19 |
| - /// Gets the enum value based on the given enum type and display name. |
20 |
| - /// </summary> |
21 |
| - /// <param name="displayName">The display name.</param> |
22 |
| - public static T GetEnumFromDisplayName<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(this string displayName) |
| 22 | + internal static bool TryGetEnumFromDisplayName<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(this string displayName, ParsingContext parsingContext, out T result) where T : Enum |
| 23 | + { |
| 24 | + if (TryGetEnumFromDisplayName(displayName, out result)) |
| 25 | + { |
| 26 | + return true; |
| 27 | + } |
| 28 | + |
| 29 | + parsingContext.Diagnostic.Errors.Add(new OpenApiError(parsingContext.GetLocation(), $"Enum value {displayName} is not recognized.")); |
| 30 | + return false; |
| 31 | + |
| 32 | + } |
| 33 | + internal static bool TryGetEnumFromDisplayName<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(this string displayName, out T result) where T : Enum |
23 | 34 | {
|
24 | 35 | var type = typeof(T);
|
25 |
| - if (!type.IsEnum) |
26 |
| - return default; |
27 | 36 |
|
28 |
| - var displayMap = EnumDisplayCache.GetOrAdd(type, _ => new ConcurrentDictionary<string, object>(StringComparer.OrdinalIgnoreCase)); |
| 37 | + var displayMap = EnumDisplayCache.GetOrAdd(type, GetEnumValues<T>); |
29 | 38 |
|
30 | 39 | if (displayMap.TryGetValue(displayName, out var cachedValue))
|
31 |
| - return (T)cachedValue; |
32 |
| - |
| 40 | + { |
| 41 | + result = (T)cachedValue; |
| 42 | + return true; |
| 43 | + } |
33 | 44 |
|
34 |
| - foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static)) |
| 45 | + result = default; |
| 46 | + return false; |
| 47 | + } |
| 48 | + private static ReadOnlyDictionary<string, object> GetEnumValues<T>(Type enumType) where T : Enum |
| 49 | + { |
| 50 | + var result = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); |
| 51 | + foreach (var field in enumType.GetFields(BindingFlags.Public | BindingFlags.Static)) |
35 | 52 | {
|
36 |
| - var displayAttribute = field.GetCustomAttribute<DisplayAttribute>(); |
37 |
| - if (displayAttribute != null && displayAttribute.Name.Equals(displayName, StringComparison.OrdinalIgnoreCase)) |
| 53 | + if (field.GetCustomAttribute<DisplayAttribute>() is {} displayAttribute) |
38 | 54 | {
|
39 | 55 | var enumValue = (T)field.GetValue(null);
|
40 |
| - displayMap.TryAdd(displayName, enumValue); |
41 |
| - return enumValue; |
| 56 | + result.Add(displayAttribute.Name, enumValue); |
42 | 57 | }
|
43 | 58 | }
|
44 |
| - |
45 |
| - return default; |
| 59 | + return new ReadOnlyDictionary<string, object>(result); |
46 | 60 | }
|
47 | 61 | internal static string ToFirstCharacterLowerCase(this string input)
|
48 | 62 | => string.IsNullOrEmpty(input) ? string.Empty : char.ToLowerInvariant(input[0]) + input.Substring(1);
|
|
0 commit comments