< 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    {
 4837311        public bool TryGetValue<T>(string key, out T value) => dictionary.TryGetValue<string, T>(key, out value);
 1482912        public bool TryGetValue<T>(IEnumerable<string> keys, out T value) => dictionary.TryGetValue<string, T>(keys, out
 13    }
 14
 839315    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    {
 664819        if (!dictionary.TryGetValue(key, out var item))
 20        {
 664821            value = default!;
 664822            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        {
 6539333            if (!dictionary.TryGetValue(key, out var item))
 34            {
 4969535                value = default!;
 4969536                return false;
 37            }
 38
 1569839            var result = TryConvertValue<T>(item);
 1569840            value = result.Success ? (T)result.Value! : default!;
 1569841            return result.Success;
 42        }
 43
 44        public bool TryGetValue<T>(IEnumerable<TKey> keys, out T value)
 45        {
 7628446            foreach (var key in keys)
 47            {
 2542848                if (dictionary.TryGetValue(key, out var item))
 49                {
 423050                    var result = TryConvertValue<T>(item);
 423051                    value = result.Success ? (T)result.Value! : default!;
 423052                    return result.Success;
 53                }
 54            }
 55
 1059956            value = default!;
 1059957            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[
 664863    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    {
 313367        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    {
 4680973        public T? GetValueOrDefault<T>(string key, Func<T?> defaultValueFactory) => TryGetValue<T>(dictionary, key, out 
 1482974        public T? GetValueOrDefault<T>(IEnumerable<string> keys, Func<T?> defaultValueFactory) => TryGetValue<T>(diction
 23475        public T? GetValueOrDefault<T>(string key) => GetValueOrDefault<T>(dictionary, key, () => default);
 3176        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    {
 206781        if(dictionary.TryGetValue(key, out T? value))
 25182           return value;
 83
 181684        value = valueFactory()!;
 181685        dictionary.Add(key, value);
 181686        return value;
 87    }
 88
 89    public static T GetOrAdd<TKey, T>(this IDictionary<TKey, object> dictionary, TKey key, Func<T> valueFactory)
 90    {
 506291        if (dictionary.TryGetValue<TKey, T>(key, out var value))
 334592            return value!;
 93
 171794        value = valueFactory()!;
 171795        dictionary.Add(key, value);
 171796        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        {
 8464115            foreach (var (key, value) in other)
 274116                dictionary[key] = value;
 3958117        }
 118    }
 119
 0120    private static T? ConvertValue<T>(object? value) => value.ConvertTo<T>();
 121
 122    private static Result TryConvertValue<T>(object? value)
 123    {
 19928124        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)