< Summary

Information
Class: Elsa.Diagnostics.ConsoleLogs.Contracts.ConsoleStreamJsonConverter
Assembly: Elsa.Diagnostics.ConsoleLogs
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.ConsoleLogs/Contracts/ConsoleStreamJsonConverter.cs
Line coverage
37%
Covered lines: 10
Uncovered lines: 17
Coverable lines: 27
Total lines: 57
Line coverage: 37%
Branch coverage
43%
Covered branches: 13
Total branches: 30
Branch coverage: 43.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Read(...)16.66%9657.14%
Write(...)0%4260%
ReadString(...)85.71%151485.71%
ReadNumber(...)0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Diagnostics.ConsoleLogs/Contracts/ConsoleStreamJsonConverter.cs

#LineLine coverage
 1using System.Text.Json;
 2using System.Text.Json.Serialization;
 3using ConsoleLogStreaming.Core.Models;
 4
 5namespace Elsa.Diagnostics.ConsoleLogs.Contracts;
 6
 7/// <summary>
 8/// Converts the public console stream filter contract values.
 9/// </summary>
 10public sealed class ConsoleStreamJsonConverter : JsonConverter<ConsoleStream?>
 11{
 12    public override ConsoleStream? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 13    {
 414        return reader.TokenType switch
 415        {
 016            JsonTokenType.Null => null,
 417            JsonTokenType.String => ReadString(reader.GetString()),
 018            JsonTokenType.Number => ReadNumber(ref reader),
 019            _ => throw new JsonException("The console log stream filter must be 'stdout', 'stderr', or null.")
 420        };
 21    }
 22
 23    public override void Write(Utf8JsonWriter writer, ConsoleStream? value, JsonSerializerOptions options)
 24    {
 025        if (value == null)
 26        {
 027            writer.WriteNullValue();
 028            return;
 29        }
 30
 031        writer.WriteStringValue(value.Value switch
 032        {
 033            ConsoleStream.Stdout => "stdout",
 034            ConsoleStream.Stderr => "stderr",
 035            _ => throw new JsonException($"Unsupported console stream value '{value}'.")
 036        });
 037    }
 38
 39    private static ConsoleStream? ReadString(string? value)
 40    {
 441        return value?.Trim().ToLowerInvariant() switch
 442        {
 243            null or "" or "all" => null,
 144            "stdout" => ConsoleStream.Stdout,
 145            "stderr" => ConsoleStream.Stderr,
 046            _ => throw new JsonException("The console log stream filter must be 'stdout', 'stderr', or null.")
 447        };
 48    }
 49
 50    private static ConsoleStream ReadNumber(ref Utf8JsonReader reader)
 51    {
 052        if (!reader.TryGetInt32(out var value) || !Enum.IsDefined(typeof(ConsoleStream), value))
 053            throw new JsonException("The console log stream filter has an unsupported numeric value.");
 54
 055        return (ConsoleStream)value;
 56    }
 57}