< Summary - Envilder .NET SDK

Information
Class: Envilder.MapFileParser
Assembly: Envilder
File(s): /home/runner/work/envilder/envilder/src/sdks/dotnet/Application/MapFileParser.cs
Tag: 352_26963168797
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 57
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;
 2
 3using System.Collections.Generic;
 4using System.Text.Json;
 5using System.Text.Json.Serialization;
 6
 7/// <summary>
 8/// Parses a JSON map file into a <see cref="ParsedMapFile"/> containing
 9/// provider configuration (<c>$config</c>) and environment variable mappings.
 10/// </summary>
 11public class MapFileParser
 12{
 13  private const string ConfigKey = "$config";
 14
 115  private static readonly JsonSerializerOptions SerializerOptions = new()
 116  {
 117    PropertyNameCaseInsensitive = true,
 118    Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
 119  };
 20
 21  /// <summary>
 22  /// Parses raw JSON into a <see cref="ParsedMapFile"/>.
 23  /// The optional <c>$config</c> object is extracted as <see cref="MapFileConfig"/>;
 24  /// all other top-level string properties become secret mappings.
 25  /// </summary>
 26  /// <param name="json">Raw JSON content of the map file.</param>
 27  /// <returns>A <see cref="ParsedMapFile"/> ready for secret resolution.</returns>
 28  public ParsedMapFile Parse(string json)
 29  {
 130    using var document = JsonDocument.Parse(json);
 131    var mappings = new Dictionary<string, string>();
 132    var config = new MapFileConfig();
 33
 134    foreach (var property in document.RootElement.EnumerateObject())
 35    {
 136      if (property.Name.StartsWith("$"))
 37      {
 138        if (property.Name == ConfigKey && property.Value.ValueKind == JsonValueKind.Object)
 39        {
 140          config = JsonSerializer.Deserialize<MapFileConfig>(property.Value.GetRawText(), SerializerOptions)
 141            ?? new();
 42        }
 43
 144        continue;
 45      }
 46
 147      if (property.Value.ValueKind != JsonValueKind.String)
 48      {
 49        continue;
 50      }
 51
 152      mappings[property.Name] = property.Value.GetString()!;
 53    }
 54
 155    return new(config, mappings);
 156  }
 57}

Methods/Properties

.cctor()
Parse(string)