< 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: 127
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.Common.Models;
 2using Elsa.Expressions.Helpers;
 3using Elsa.Expressions.Models;
 4
 5// ReSharper disable once CheckNamespace
 6namespace Elsa.Extensions;
 7
 8public static class DictionaryExtensions
 9{
 10    extension(IDictionary<string, object> dictionary)
 11    {
 5444112        public bool TryGetValue<T>(string key, out T value) => dictionary.TryGetValue<string, T>(key, out value);
 1885413        public bool TryGetValue<T>(IEnumerable<string> keys, out T value) => dictionary.TryGetValue<string, T>(keys, out
 14    }
 15
 883816    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    {
 809320        if (!dictionary.TryGetValue(key, out var item))
 21        {
 809322            value = default!;
 809323            return false;
 24        }
 25
 026        value = item;
 027        return true;
 28    }
 29
 30    extension<TKey>(IDictionary<TKey, object> dictionary)
 31    {
 32        public bool TryGetValue<T>(TKey key, out T value)
 33        {
 7299034            if (!dictionary.TryGetValue(key, out var item))
 35            {
 5577136                value = default!;
 5577137                return false;
 38            }
 39
 1721940            var result = TryConvertValue<T>(item);
 1721941            value = result.IsSuccess ? (T)result.Value! : default!;
 1721942            return result.IsSuccess;
 43        }
 44
 45        public bool TryGetValue<T>(IEnumerable<TKey> keys, out T value)
 46        {
 9761447            foreach (var key in keys)
 48            {
 3253849                if (!dictionary.TryGetValue(key, out var item))
 50                    continue;
 51
 517052                var result = TryConvertValue<T>(item);
 517053                value = result.IsSuccess ? (T)result.Value : default!;
 517054                return result.IsSuccess;
 55            }
 56
 1368457            value = default!;
 1368458            return false;
 59        }
 60    }
 61
 062    public static T? GetValue<TKey, T>(this IDictionary<TKey, T> dictionary, TKey key) => ConvertValue<T>(dictionary[key
 063    public static T? GetValue<T>(this IDictionary<string, object> dictionary, string key) => ConvertValue<T>(dictionary[
 809364    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    {
 345568        public T? GetValueOrDefault<T>(TKey key, Func<T?> defaultValueFactory) => dictionary.TryGetValue<TKey, T>(key, o
 069        public T? GetValueOrDefault<T>(TKey key) => dictionary.GetValueOrDefault<TKey, T>(key, () => default);
 70    }
 71
 72    extension(IDictionary<string, object> dictionary)
 73    {
 5234174        public T? GetValueOrDefault<T>(string key, Func<T?> defaultValueFactory) => dictionary.TryGetValue<T>(key, out v
 1885475        public T? GetValueOrDefault<T>(IEnumerable<string> keys, Func<T?> defaultValueFactory) => dictionary.TryGetValue
 27076        public T? GetValueOrDefault<T>(string key) => dictionary.GetValueOrDefault<T>(key, () => default);
 4477        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    {
 214482        if(dictionary.TryGetValue(key, out var value))
 29083           return value;
 84
 185485        value = valueFactory()!;
 185486        dictionary.Add(key, value);
 185487        return value;
 88    }
 89
 90    public static T GetOrAdd<TKey, T>(this IDictionary<TKey, object> dictionary, TKey key, Func<T> valueFactory)
 91    {
 579492        if (dictionary.TryGetValue<TKey, T>(key, out var value))
 388193            return value!;
 94
 191395        value = valueFactory()!;
 191396        dictionary.Add(key, value);
 191397        return value;
 98    }
 99
 100    extension(IDictionary<string, object> dictionary)
 101    {
 0102        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        {
 0106            dictionary.Add(key, value);
 0107            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        {
 9278116            foreach (var (key, value) in other)
 287117                dictionary[key] = value;
 4352118        }
 119    }
 120
 0121    private static T? ConvertValue<T>(object? value) => value.ConvertTo<T>();
 122
 123    private static Result TryConvertValue<T>(object? value)
 124    {
 22389125        return value.TryConvertTo<T>();
 126    }
 127}

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)