< Summary - Envilder .NET SDK

Information
Class: Envilder.MapFileParser
Assembly: Envilder
File(s): /home/runner/work/envilder/envilder/src/sdks/dotnet/Application/MapFileParser.cs
Tag: 561_41292037308
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 66
Line coverage: 100%
Branch coverage
92%
Covered branches: 13
Total branches: 14
Branch coverage: 92.8%
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(...)92.86%1414100%

File(s)

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

#LineLine coverage
 1namespace Envilder;
 2
 3using Envilder.Domain;
 4using System;
 5using System.Collections.Generic;
 6using System.Text.Json;
 7using System.Text.Json.Serialization;
 8
 9/// <summary>
 10/// Parses a JSON map file into a <see cref="ParsedMapFile"/> containing
 11/// provider configuration (<c>$config</c>) and environment variable mappings.
 12/// </summary>
 13public class MapFileParser
 14{
 15  private const string ConfigKey = "$config";
 16
 117  private static readonly JsonSerializerOptions SerializerOptions = new()
 118  {
 119    PropertyNameCaseInsensitive = true,
 120    Converters = { new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) },
 121  };
 22
 23  /// <summary>
 24  /// Parses raw JSON into a <see cref="ParsedMapFile"/>.
 25  /// The optional <c>$config</c> object is extracted as <see cref="MapFileConfig"/>;
 26  /// all other top-level string properties become secret mappings.
 27  /// </summary>
 28  /// <param name="json">Raw JSON content of the map file.</param>
 29  /// <returns>A <see cref="ParsedMapFile"/> ready for secret resolution.</returns>
 30  public ParsedMapFile Parse(string json)
 31  {
 132    using var document = JsonDocument.Parse(json);
 133    var mappings = new Dictionary<string, string>();
 134    var config = new MapFileConfig();
 35
 136    foreach (var property in document.RootElement.EnumerateObject())
 37    {
 138      if (property.Name.StartsWith("$"))
 39      {
 140        if (property.Name == ConfigKey && property.Value.ValueKind == JsonValueKind.Object)
 41        {
 142          config = JsonSerializer.Deserialize<MapFileConfig>(property.Value.GetRawText(), SerializerOptions)
 143            ?? new();
 44        }
 45
 146        continue;
 47      }
 48
 49      // Before the non-string filter, so the name rule covers every mapping
 50      // key the file declares, matching the CLI and the published schema.
 151      if (!EnvironmentVariableName.IsValid(property.Name))
 52      {
 153        throw new FormatException(EnvironmentVariableName.InvalidMessage(property.Name));
 54      }
 55
 156      if (property.Value.ValueKind != JsonValueKind.String)
 57      {
 58        continue;
 59      }
 60
 161      mappings[property.Name] = property.Value.GetString()!;
 62    }
 63
 164    return new(config, mappings);
 165  }
 66}

Methods/Properties

.cctor()
Parse(string)