< Summary

Information
Class: Elsa.Extensions.DictionaryExtensions
Assembly: Elsa.Workflows.Core
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/DictionaryExtensions.cs
Line coverage
81%
Covered lines: 39
Uncovered lines: 9
Coverable lines: 48
Total lines: 126
Line coverage: 81.2%
Branch coverage
80%
Covered branches: 24
Total branches: 30
Branch coverage: 80%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryGetValue(...)100%11100%
TryGetValue(...)100%11100%
TryGetValue(...)100%11100%
TryGetValue(...)50%2260%
TryGetValue(...)75%44100%
TryGetValue(...)83.33%66100%
GetValue(...)100%210%
GetValue(...)100%210%
GetValueOrDefault(...)50%22100%
GetValueOrDefault(...)100%22100%
GetValueOrDefault(...)0%620%
GetValueOrDefault(...)100%22100%
GetValueOrDefault(...)100%22100%
GetValueOrDefault(...)100%22100%
GetValueOrDefault(...)100%11100%
GetOrAdd(...)100%22100%
GetOrAdd(...)100%22100%
AddInput(...)100%210%
AddInput(...)100%210%
Merge(...)100%22100%
ConvertValue(...)100%210%
TryConvertValue(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Core/Extensions/DictionaryExtensions.cs

#LineLine coverage
 1using Elsa.Expressions.Helpers;
 2using Elsa.Expressions.Models;
 3
 4// ReSharper disable once CheckNamespace
 5namespace Elsa.Extensions;
 6
 7public static class DictionaryExtensions
 8{
 9    extension(IDictionary<string, object> dictionary)
 10    {
 5430811        public bool TryGetValue<T>(string key, out T value) => dictionary.TryGetValue<string, T>(key, out value);
 1809212        public bool TryGetValue<T>(IEnumerable<string> keys, out T value) => dictionary.TryGetValue<string, T>(keys, out
 13    }
 14
 882315    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    {
 808619        if (!dictionary.TryGetValue(key, out var item))
 20        {
 808621            value = default!;
 808622            return false;
 23        }
 24
 025        value = item;
 026        return true;
 27    }
 28
 29    extension<TKey>(IDictionary<TKey, object> dictionary)
 30    {
 31        public bool TryGetValue<T>(TKey key, out T value)
 32        {
 7285633            if (!dictionary.TryGetValue(key, out var item))
 34            {
 5567235                value = default!;
 5567236                return false;
 37            }
 38
 1718439            var result = TryConvertValue<T>(item);
 1718440            value = result.Success ? (T)result.Value! : default!;
 1718441            return result.Success;
 42        }
 43
 44        public bool TryGetValue<T>(IEnumerable<TKey> keys, out T value)
 45        {
 9262246            foreach (var key in keys)
 47            {
 3087448                if (dictionary.TryGetValue(key, out var item))
 49                {
 531050                    var result = TryConvertValue<T>(item);
 531051                    value = result.Success ? (T)result.Value! : default!;
 531052                    return result.Success;
 53                }
 54            }
 55
 1278256            value = default!;
 1278257            return false;
 58        }
 59    }
 60
 061    public static T? GetValue<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key) => ConvertValue<T>(dictionary[key
 062    public static T? GetValue<T>(this IDictionary<string, object> dictionary, string key) => ConvertValue<T>(dictionary[
 808663    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    {
 344667        public T? GetValueOrDefault<T>(TKey key, Func<T?> defaultValueFactory) => TryGetValue<TKey, T>(dictionary, key, 
 068        public T? GetValueOrDefault<T>(TKey key) => GetValueOrDefault<TKey, T>(dictionary, key, () => default);
 69    }
 70
 71    extension(IDictionary<string, object> dictionary)
 72    {
 5221673        public T? GetValueOrDefault<T>(string key, Func<T?> defaultValueFactory) => TryGetValue<T>(dictionary, key, out 
 1809274        public T? GetValueOrDefault<T>(IEnumerable<string> keys, Func<T?> defaultValueFactory) => TryGetValue<T>(diction
 25875        public T? GetValueOrDefault<T>(string key) => GetValueOrDefault<T>(dictionary, key, () => default);
 4976        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    {
 211181        if(dictionary.TryGetValue(key, out T? value))
 27282           return value;
 83
 183984        value = valueFactory()!;
 183985        dictionary.Add(key, value);
 183986        return value;
 87    }
 88
 89    public static T GetOrAdd<TKey, T>(this IDictionary<TKey, object> dictionary, TKey key, Func<T> valueFactory)
 90    {
 578191        if (dictionary.TryGetValue<TKey, T>(key, out var value))
 387192            return value!;
 93
 191094        value = valueFactory()!;
 191095        dictionary.Add(key, value);
 191096        return value;
 97    }
 98
 99    extension(IDictionary<string, object> dictionary)
 100    {
 0101        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        {
 0105            dictionary.Add(key, value);
 0106            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        {
 9256115            foreach (var (key, value) in other)
 287116                dictionary[key] = value;
 4341117        }
 118    }
 119
 0120    private static T? ConvertValue<T>(object? value) => value.ConvertTo<T>();
 121
 122    private static Result TryConvertValue<T>(object? value)
 123    {
 22494124        return value.TryConvertTo<T>();
 125    }
 126}

Methods/Properties

TryGetValue(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.String,T&)
TryGetValue(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IEnumerable`1<System.String>,T&)
TryGetValue(System.Collections.Generic.IDictionary`2<System.Object,System.Object>,System.String,T&)
TryGetValue(System.Collections.Generic.IDictionary`2<TKey,T>,TKey,T&)
TryGetValue(System.Collections.Generic.IDictionary`2<TKey,System.Object>,TKey,T&)
TryGetValue(System.Collections.Generic.IDictionary`2<TKey,System.Object>,System.Collections.Generic.IEnumerable`1<TKey>,T&)
GetValue(System.Collections.Generic.IDictionary`2<TKey,T>,TKey)
GetValue(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.String)
GetValueOrDefault(System.Collections.Generic.IDictionary`2<TKey,T>,TKey,System.Func`1<T>)
GetValueOrDefault(System.Collections.Generic.IDictionary`2<TKey,System.Object>,TKey,System.Func`1<T>)
GetValueOrDefault(System.Collections.Generic.IDictionary`2<TKey,System.Object>,TKey)
GetValueOrDefault(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.String,System.Func`1<T>)
GetValueOrDefault(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IEnumerable`1<System.String>,System.Func`1<T>)
GetValueOrDefault(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.String)
GetValueOrDefault(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.String)
GetOrAdd(System.Collections.Generic.IDictionary`2<TKey,T>,TKey,System.Func`1<T>)
GetOrAdd(System.Collections.Generic.IDictionary`2<TKey,System.Object>,TKey,System.Func`1<T>)
AddInput(System.Collections.Generic.IDictionary`2<System.String,System.Object>,T)
AddInput(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.String,System.Object)
Merge(System.Collections.Generic.IDictionary`2<System.String,System.Object>,System.Collections.Generic.IDictionary`2<System.String,System.Object>)
ConvertValue(System.Object)
TryConvertValue(System.Object)