| | | 1 | | using Elsa.Expressions.Helpers; |
| | | 2 | | using Elsa.Expressions.Models; |
| | | 3 | | |
| | | 4 | | // ReSharper disable once CheckNamespace |
| | | 5 | | namespace Elsa.Extensions; |
| | | 6 | | |
| | | 7 | | public static class DictionaryExtensions |
| | | 8 | | { |
| | | 9 | | extension(IDictionary<string, object> dictionary) |
| | | 10 | | { |
| | 48373 | 11 | | public bool TryGetValue<T>(string key, out T value) => dictionary.TryGetValue<string, T>(key, out value); |
| | 14829 | 12 | | public bool TryGetValue<T>(IEnumerable<string> keys, out T value) => dictionary.TryGetValue<string, T>(keys, out |
| | | 13 | | } |
| | | 14 | | |
| | 8393 | 15 | | public static bool TryGetValue<T>(this IDictionary<object, object> dictionary, string key, out T value) => dictionar |
| | | 16 | | |
| | | 17 | | public static bool TryGetValue<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key, out T value) |
| | | 18 | | { |
| | 6648 | 19 | | if (!dictionary.TryGetValue(key, out var item)) |
| | | 20 | | { |
| | 6648 | 21 | | value = default!; |
| | 6648 | 22 | | return false; |
| | | 23 | | } |
| | | 24 | | |
| | 0 | 25 | | value = item; |
| | 0 | 26 | | return true; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | extension<TKey>(IDictionary<TKey, object> dictionary) |
| | | 30 | | { |
| | | 31 | | public bool TryGetValue<T>(TKey key, out T value) |
| | | 32 | | { |
| | 65393 | 33 | | if (!dictionary.TryGetValue(key, out var item)) |
| | | 34 | | { |
| | 49695 | 35 | | value = default!; |
| | 49695 | 36 | | return false; |
| | | 37 | | } |
| | | 38 | | |
| | 15698 | 39 | | var result = TryConvertValue<T>(item); |
| | 15698 | 40 | | value = result.Success ? (T)result.Value! : default!; |
| | 15698 | 41 | | return result.Success; |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public bool TryGetValue<T>(IEnumerable<TKey> keys, out T value) |
| | | 45 | | { |
| | 76284 | 46 | | foreach (var key in keys) |
| | | 47 | | { |
| | 25428 | 48 | | if (dictionary.TryGetValue(key, out var item)) |
| | | 49 | | { |
| | 4230 | 50 | | var result = TryConvertValue<T>(item); |
| | 4230 | 51 | | value = result.Success ? (T)result.Value! : default!; |
| | 4230 | 52 | | return result.Success; |
| | | 53 | | } |
| | | 54 | | } |
| | | 55 | | |
| | 10599 | 56 | | value = default!; |
| | 10599 | 57 | | return false; |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | public static T? GetValue<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key) => ConvertValue<T>(dictionary[key |
| | 0 | 62 | | public static T? GetValue<T>(this IDictionary<string, object> dictionary, string key) => ConvertValue<T>(dictionary[ |
| | 6648 | 63 | | public static T? GetValueOrDefault<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key, Func<T?> defaultValueFac |
| | | 64 | | |
| | | 65 | | extension<TKey>(IDictionary<TKey, object> dictionary) |
| | | 66 | | { |
| | 3133 | 67 | | public T? GetValueOrDefault<T>(TKey key, Func<T?> defaultValueFactory) => TryGetValue<TKey, T>(dictionary, key, |
| | 0 | 68 | | public T? GetValueOrDefault<T>(TKey key) => GetValueOrDefault<TKey, T>(dictionary, key, () => default); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | extension(IDictionary<string, object> dictionary) |
| | | 72 | | { |
| | 46809 | 73 | | public T? GetValueOrDefault<T>(string key, Func<T?> defaultValueFactory) => TryGetValue<T>(dictionary, key, out |
| | 14829 | 74 | | public T? GetValueOrDefault<T>(IEnumerable<string> keys, Func<T?> defaultValueFactory) => TryGetValue<T>(diction |
| | 234 | 75 | | public T? GetValueOrDefault<T>(string key) => GetValueOrDefault<T>(dictionary, key, () => default); |
| | 31 | 76 | | public object? GetValueOrDefault(string key) => GetValueOrDefault<object>(dictionary, key, () => null); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public static T GetOrAdd<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key, Func<T> valueFactory) |
| | | 80 | | { |
| | 2067 | 81 | | if(dictionary.TryGetValue(key, out T? value)) |
| | 251 | 82 | | return value; |
| | | 83 | | |
| | 1816 | 84 | | value = valueFactory()!; |
| | 1816 | 85 | | dictionary.Add(key, value); |
| | 1816 | 86 | | return value; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public static T GetOrAdd<TKey, T>(this IDictionary<TKey, object> dictionary, TKey key, Func<T> valueFactory) |
| | | 90 | | { |
| | 5062 | 91 | | if (dictionary.TryGetValue<TKey, T>(key, out var value)) |
| | 3345 | 92 | | return value!; |
| | | 93 | | |
| | 1717 | 94 | | value = valueFactory()!; |
| | 1717 | 95 | | dictionary.Add(key, value); |
| | 1717 | 96 | | return value; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | extension(IDictionary<string, object> dictionary) |
| | | 100 | | { |
| | 0 | 101 | | public IDictionary<string, object> AddInput<T>(T value) where T : notnull => dictionary.AddInput(typeof(T).Name, |
| | | 102 | | |
| | | 103 | | public IDictionary<string, object> AddInput(string key, object value) |
| | | 104 | | { |
| | 0 | 105 | | dictionary.Add(key, value); |
| | 0 | 106 | | return dictionary; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Merges the specified dictionary with the other dictionary. |
| | | 111 | | /// When a key exists in both dictionaries, the value in the other dictionary will overwrite the value in the sp |
| | | 112 | | /// </summary> |
| | | 113 | | public void Merge(IDictionary<string, object> other) |
| | | 114 | | { |
| | 8464 | 115 | | foreach (var (key, value) in other) |
| | 274 | 116 | | dictionary[key] = value; |
| | 3958 | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | private static T? ConvertValue<T>(object? value) => value.ConvertTo<T>(); |
| | | 121 | | |
| | | 122 | | private static Result TryConvertValue<T>(object? value) |
| | | 123 | | { |
| | 19928 | 124 | | return value.TryConvertTo<T>(); |
| | | 125 | | } |
| | | 126 | | } |