| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Expressions.Helpers; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | |
| | | 5 | | // ReSharper disable once CheckNamespace |
| | | 6 | | namespace Elsa.Extensions; |
| | | 7 | | |
| | | 8 | | public static class DictionaryExtensions |
| | | 9 | | { |
| | | 10 | | extension(IDictionary<string, object> dictionary) |
| | | 11 | | { |
| | 54441 | 12 | | public bool TryGetValue<T>(string key, out T value) => dictionary.TryGetValue<string, T>(key, out value); |
| | 18854 | 13 | | public bool TryGetValue<T>(IEnumerable<string> keys, out T value) => dictionary.TryGetValue<string, T>(keys, out |
| | | 14 | | } |
| | | 15 | | |
| | 8838 | 16 | | public static bool TryGetValue<T>(this IDictionary<object, object> dictionary, string key, out T value) => dictionar |
| | | 17 | | |
| | | 18 | | public static bool TryGetValue<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key, out T value) |
| | | 19 | | { |
| | 8093 | 20 | | if (!dictionary.TryGetValue(key, out var item)) |
| | | 21 | | { |
| | 8093 | 22 | | value = default!; |
| | 8093 | 23 | | return false; |
| | | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | value = item; |
| | 0 | 27 | | return true; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | extension<TKey>(IDictionary<TKey, object> dictionary) |
| | | 31 | | { |
| | | 32 | | public bool TryGetValue<T>(TKey key, out T value) |
| | | 33 | | { |
| | 72990 | 34 | | if (!dictionary.TryGetValue(key, out var item)) |
| | | 35 | | { |
| | 55771 | 36 | | value = default!; |
| | 55771 | 37 | | return false; |
| | | 38 | | } |
| | | 39 | | |
| | 17219 | 40 | | var result = TryConvertValue<T>(item); |
| | 17219 | 41 | | value = result.IsSuccess ? (T)result.Value! : default!; |
| | 17219 | 42 | | return result.IsSuccess; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public bool TryGetValue<T>(IEnumerable<TKey> keys, out T value) |
| | | 46 | | { |
| | 97614 | 47 | | foreach (var key in keys) |
| | | 48 | | { |
| | 32538 | 49 | | if (!dictionary.TryGetValue(key, out var item)) |
| | | 50 | | continue; |
| | | 51 | | |
| | 5170 | 52 | | var result = TryConvertValue<T>(item); |
| | 5170 | 53 | | value = result.IsSuccess ? (T)result.Value : default!; |
| | 5170 | 54 | | return result.IsSuccess; |
| | | 55 | | } |
| | | 56 | | |
| | 13684 | 57 | | value = default!; |
| | 13684 | 58 | | return false; |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | public static T? GetValue<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key) => ConvertValue<T>(dictionary[key |
| | 0 | 63 | | public static T? GetValue<T>(this IDictionary<string, object> dictionary, string key) => ConvertValue<T>(dictionary[ |
| | 8093 | 64 | | public static T? GetValueOrDefault<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key, Func<T?> defaultValueFac |
| | | 65 | | |
| | | 66 | | extension<TKey>(IDictionary<TKey, object> dictionary) |
| | | 67 | | { |
| | 3455 | 68 | | public T? GetValueOrDefault<T>(TKey key, Func<T?> defaultValueFactory) => dictionary.TryGetValue<TKey, T>(key, o |
| | 0 | 69 | | public T? GetValueOrDefault<T>(TKey key) => dictionary.GetValueOrDefault<TKey, T>(key, () => default); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | extension(IDictionary<string, object> dictionary) |
| | | 73 | | { |
| | 52341 | 74 | | public T? GetValueOrDefault<T>(string key, Func<T?> defaultValueFactory) => dictionary.TryGetValue<T>(key, out v |
| | 18854 | 75 | | public T? GetValueOrDefault<T>(IEnumerable<string> keys, Func<T?> defaultValueFactory) => dictionary.TryGetValue |
| | 270 | 76 | | public T? GetValueOrDefault<T>(string key) => dictionary.GetValueOrDefault<T>(key, () => default); |
| | 44 | 77 | | public object? GetValueOrDefault(string key) => dictionary.GetValueOrDefault<object>(key, () => null); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | public static T GetOrAdd<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key, Func<T> valueFactory) |
| | | 81 | | { |
| | 2144 | 82 | | if(dictionary.TryGetValue(key, out var value)) |
| | 290 | 83 | | return value; |
| | | 84 | | |
| | 1854 | 85 | | value = valueFactory()!; |
| | 1854 | 86 | | dictionary.Add(key, value); |
| | 1854 | 87 | | return value; |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | public static T GetOrAdd<TKey, T>(this IDictionary<TKey, object> dictionary, TKey key, Func<T> valueFactory) |
| | | 91 | | { |
| | 5794 | 92 | | if (dictionary.TryGetValue<TKey, T>(key, out var value)) |
| | 3881 | 93 | | return value!; |
| | | 94 | | |
| | 1913 | 95 | | value = valueFactory()!; |
| | 1913 | 96 | | dictionary.Add(key, value); |
| | 1913 | 97 | | return value; |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | extension(IDictionary<string, object> dictionary) |
| | | 101 | | { |
| | 0 | 102 | | public IDictionary<string, object> AddInput<T>(T value) where T : notnull => dictionary.AddInput(typeof(T).Name, |
| | | 103 | | |
| | | 104 | | public IDictionary<string, object> AddInput(string key, object value) |
| | | 105 | | { |
| | 0 | 106 | | dictionary.Add(key, value); |
| | 0 | 107 | | return dictionary; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Merges the specified dictionary with the other dictionary. |
| | | 112 | | /// When a key exists in both dictionaries, the value in the other dictionary will overwrite the value in the sp |
| | | 113 | | /// </summary> |
| | | 114 | | public void Merge(IDictionary<string, object> other) |
| | | 115 | | { |
| | 9278 | 116 | | foreach (var (key, value) in other) |
| | 287 | 117 | | dictionary[key] = value; |
| | 4352 | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | private static T? ConvertValue<T>(object? value) => value.ConvertTo<T>(); |
| | | 122 | | |
| | | 123 | | private static Result TryConvertValue<T>(object? value) |
| | | 124 | | { |
| | 22389 | 125 | | return value.TryConvertTo<T>(); |
| | | 126 | | } |
| | | 127 | | } |