< Summary

Information
Class: Elsa.Extensions.ConfigurationExtensions
Assembly: Elsa.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/ConfigurationExtensions.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 45
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetSectionAsJson(...)100%210%
ConvertToJsonElement(...)100%210%
ToObject(...)0%4260%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Common/Extensions/ConfigurationExtensions.cs

#LineLine coverage
 1using System.Text.Json;
 2using Microsoft.Extensions.Configuration;
 3
 4// ReSharper disable once CheckNamespace
 5namespace Elsa.Extensions;
 6
 7public static class ConfigurationExtensions
 8{
 9    public static string GetSectionAsJson(this IConfiguration configuration, string sectionKey, JsonSerializerOptions? o
 10    {
 011        var section = configuration.GetSection(sectionKey);
 012        var element = ConvertToJsonElement(section);
 013        return JsonSerializer.Serialize(element, options);
 14    }
 15
 16    private static JsonElement ConvertToJsonElement(IConfigurationSection section)
 17    {
 018        using var doc = JsonDocument.Parse(JsonSerializer.Serialize(ToObject(section)));
 019        return doc.RootElement.Clone();
 020    }
 21
 22    private static object? ToObject(IConfigurationSection section)
 23    {
 024        var children = section.GetChildren().ToList();
 025        if (!children.Any())
 026            return section.Value;
 27
 028        if (children.All(c => int.TryParse(c.Key, out _)))
 29        {
 30            // Treat as array
 031            return children
 032                .OrderBy(c => int.Parse(c.Key))
 033                .Select(ToObject)
 034                .ToList();
 35        }
 36
 037        var dict = new Dictionary<string, object?>();
 038        foreach (var child in children)
 39        {
 040            dict[child.Key] = ToObject(child);
 41        }
 42
 043        return dict;
 44    }
 45}