| | | 1 | | using System.Text.Json; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Api.Client.Extensions; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides extension methods for <see cref="JsonElement"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class DictionaryExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Returns the value of the specified property if it exists, otherwise the default value. |
| | | 12 | | /// </summary> |
| | | 13 | | public static T TryGetValue<T>(this IDictionary<string, object> dictionary, string key, Func<T>? defaultValue = null |
| | | 14 | | { |
| | 0 | 15 | | var caseInsensitiveDictionary = ToCaseInsensitiveDictionary(dictionary); |
| | | 16 | | |
| | 0 | 17 | | if (caseInsensitiveDictionary.TryGetValue(key, out var value) && value is not JsonElement { ValueKind: JsonValue |
| | | 18 | | { |
| | 0 | 19 | | var convertedValue = value.ConvertTo<T>(new ObjectConverterOptions(serializerOptions)); |
| | | 20 | | |
| | 0 | 21 | | if (convertedValue != null) |
| | 0 | 22 | | return convertedValue; |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | if (defaultValue == null) |
| | 0 | 26 | | return default!; |
| | | 27 | | |
| | 0 | 28 | | var defaultVal = defaultValue()!; |
| | 0 | 29 | | dictionary[key] = defaultVal; |
| | 0 | 30 | | return defaultVal; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Returns the value of the specified property if it exists, otherwise the default value. |
| | | 35 | | /// </summary> |
| | | 36 | | public static object? TryGetValue(this IDictionary<string, object> dictionary, string key, Func<object>? defaultValu |
| | | 37 | | { |
| | 0 | 38 | | var caseInsensitiveDictionary = ToCaseInsensitiveDictionary(dictionary); |
| | | 39 | | |
| | 0 | 40 | | if (caseInsensitiveDictionary.TryGetValue(key, out var value) && value is not JsonElement { ValueKind: JsonValue |
| | 0 | 41 | | return value; |
| | | 42 | | |
| | 0 | 43 | | if (defaultValue == null) |
| | 0 | 44 | | return null; |
| | | 45 | | |
| | 0 | 46 | | var defaultVal = defaultValue()!; |
| | 0 | 47 | | dictionary[key] = defaultVal; |
| | 0 | 48 | | return defaultVal; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | // A method that de-duplicates the keys in the dictionary, taking into account that there may already be multiple ke |
| | | 52 | | private static Dictionary<string, object> ToCaseInsensitiveDictionary(this IDictionary<string, object> dictionary) |
| | | 53 | | { |
| | 0 | 54 | | var caseInsensitiveDictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); |
| | | 55 | | |
| | 0 | 56 | | foreach (var key in dictionary.Keys) |
| | | 57 | | { |
| | 0 | 58 | | if (caseInsensitiveDictionary.ContainsKey(key)) |
| | | 59 | | continue; |
| | | 60 | | |
| | 0 | 61 | | caseInsensitiveDictionary[key] = dictionary[key]!; |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | return caseInsensitiveDictionary; |
| | | 65 | | } |
| | | 66 | | } |