< Summary

Information
Class: Elsa.Api.Client.Extensions.DictionaryExtensions
Assembly: Elsa.Api.Client
File(s): /home/runner/work/elsa-core/elsa-core/src/clients/Elsa.Api.Client/Extensions/DictionaryExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 66
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 22
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryGetValue(...)0%110100%
TryGetValue(...)0%7280%
ToCaseInsensitiveDictionary(...)0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/clients/Elsa.Api.Client/Extensions/DictionaryExtensions.cs

#LineLine coverage
 1using System.Text.Json;
 2
 3namespace Elsa.Api.Client.Extensions;
 4
 5/// <summary>
 6/// Provides extension methods for <see cref="JsonElement"/>.
 7/// </summary>
 8public static class DictionaryExtensions
 9{
 10    /// <summary>
 11    /// Returns the value of the specified property if it exists, otherwise the default value.
 12    /// </summary>
 13    public static T TryGetValue<T>(this IDictionary<string, object> dictionary, string key, Func<T>? defaultValue = null
 14    {
 015        var caseInsensitiveDictionary = ToCaseInsensitiveDictionary(dictionary);
 16
 017        if (caseInsensitiveDictionary.TryGetValue(key, out var value) && value is not JsonElement { ValueKind: JsonValue
 18        {
 019            var convertedValue = value.ConvertTo<T>(new ObjectConverterOptions(serializerOptions));
 20
 021            if (convertedValue != null)
 022                return convertedValue;
 23        }
 24
 025        if (defaultValue == null)
 026            return default!;
 27
 028        var defaultVal = defaultValue()!;
 029        dictionary[key] = defaultVal;
 030        return defaultVal;
 31    }
 32
 33    /// <summary>
 34    /// Returns the value of the specified property if it exists, otherwise the default value.
 35    /// </summary>
 36    public static object? TryGetValue(this IDictionary<string, object> dictionary, string key, Func<object>? defaultValu
 37    {
 038        var caseInsensitiveDictionary = ToCaseInsensitiveDictionary(dictionary);
 39
 040        if (caseInsensitiveDictionary.TryGetValue(key, out var value) && value is not JsonElement { ValueKind: JsonValue
 041            return value;
 42
 043        if (defaultValue == null)
 044            return null;
 45
 046        var defaultVal = defaultValue()!;
 047        dictionary[key] = defaultVal;
 048        return defaultVal;
 49    }
 50
 51    // A method that de-duplicates the keys in the dictionary, taking into account that there may already be multiple ke
 52    private static Dictionary<string, object> ToCaseInsensitiveDictionary(this IDictionary<string, object> dictionary)
 53    {
 054        var caseInsensitiveDictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
 55
 056        foreach (var key in dictionary.Keys)
 57        {
 058            if (caseInsensitiveDictionary.ContainsKey(key))
 59                continue;
 60
 061            caseInsensitiveDictionary[key] = dictionary[key]!;
 62        }
 63
 064        return caseInsensitiveDictionary;
 65    }
 66}