< Summary - Envilder .NET SDK

Information
Class: Envilder.Application.MapFileParser
Assembly: Envilder
File(s): /home/runner/work/envilder/envilder/src/sdks/dotnet/Application/MapFileParser.cs
Tag: 299_25910610327
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 58
Line coverage: 100%
Branch coverage
91%
Covered branches: 11
Total branches: 12
Branch coverage: 91.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
Parse(...)91.67%1212100%

File(s)

/home/runner/work/envilder/envilder/src/sdks/dotnet/Application/MapFileParser.cs

#LineLine coverage
 1namespace Envilder.Application;
 2
 3using global::Envilder.Domain;
 4using System.Collections.Generic;
 5using System.Text.Json;
 6using System.Text.Json.Serialization;
 7
 8/// <summary>
 9/// Parses a JSON map file into a <see cref="ParsedMapFile"/> containing
 10/// provider configuration (<c>$config</c>) and environment variable mappings.
 11/// </summary>
 12public class MapFileParser
 13{
 14  private const string ConfigKey = "$config";
 15
 116  private static readonly JsonSerializerOptions SerializerOptions = new()
 117  {
 118    PropertyNameCaseInsensitive = true,
 119    Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
 120  };
 21
 22  /// <summary>
 23  /// Parses raw JSON into a <see cref="ParsedMapFile"/>.
 24  /// The optional <c>$config</c> object is extracted as <see cref="MapFileConfig"/>;
 25  /// all other top-level string properties become secret mappings.
 26  /// </summary>
 27  /// <param name="json">Raw JSON content of the map file.</param>
 28  /// <returns>A <see cref="ParsedMapFile"/> ready for secret resolution.</returns>
 29  public ParsedMapFile Parse(string json)
 30  {
 131    using var document = JsonDocument.Parse(json);
 132    var mappings = new Dictionary<string, string>();
 133    var config = new MapFileConfig();
 34
 135    foreach (var property in document.RootElement.EnumerateObject())
 36    {
 137      if (property.Name.StartsWith("$"))
 38      {
 139        if (property.Name == ConfigKey && property.Value.ValueKind == JsonValueKind.Object)
 40        {
 141          config = JsonSerializer.Deserialize<MapFileConfig>(property.Value.GetRawText(), SerializerOptions)
 142            ?? new();
 43        }
 44
 145        continue;
 46      }
 47
 148      if (property.Value.ValueKind != JsonValueKind.String)
 49      {
 50        continue;
 51      }
 52
 153      mappings[property.Name] = property.Value.GetString()!;
 54    }
 55
 156    return new(config, mappings);
 157  }
 58}

Methods/Properties

.cctor()
Parse(string)