• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / MediatR.CommandQuery / 12567339125

01 Jan 2025 04:52AM UTC coverage: 60.229% (-0.1%) from 60.328%
12567339125

push

github

pwelter34
switch to Testcontainers

402 of 761 branches covered (52.83%)

Branch coverage included in aggregate %.

1282 of 2035 relevant lines covered (63.0%)

19.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.09
/src/MediatR.CommandQuery/Converters/EntityFilterConverter.cs
1
using System.Text.Json;
2
using System.Text.Json.Serialization;
3

4
using MediatR.CommandQuery.Queries;
5

6
namespace MediatR.CommandQuery.Converters;
7

8
/// <summary>
9
/// <see cref="JsonConverter{T}"/> for <see cref="EntityFilter"/>
10
/// </summary>
11
public sealed class EntityFilterConverter : JsonConverter<EntityFilter>
12
{
13
    private static readonly JsonEncodedText Name = JsonEncodedText.Encode("name");
2✔
14
    private static readonly JsonEncodedText Operator = JsonEncodedText.Encode("operator");
2✔
15
    private static readonly JsonEncodedText Value = JsonEncodedText.Encode("value");
2✔
16
    private static readonly JsonEncodedText Logic = JsonEncodedText.Encode("logic");
2✔
17
    private static readonly JsonEncodedText Filters = JsonEncodedText.Encode("filters");
2✔
18

19
    /// <inheritdoc />
20
    public override EntityFilter Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21
    {
22
        var filter = new EntityFilter();
20✔
23

24
        if (reader.TokenType != JsonTokenType.StartObject)
20!
25
            throw new JsonException("Unexcepted end when reading JSON.");
×
26

27
        while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
74✔
28
            ReadValue(ref reader, filter, options);
54✔
29

30
        if (reader.TokenType != JsonTokenType.EndObject)
20!
31
            throw new JsonException("Unexcepted end when reading JSON.");
×
32

33
        return filter;
20✔
34
    }
35

36
    /// <inheritdoc />
37
    public override void Write(Utf8JsonWriter writer, EntityFilter value, JsonSerializerOptions options)
38
    {
39
        writer.WriteStartObject();
8✔
40
        WriteEntityFilter(writer, value, options);
8✔
41
        writer.WriteEndObject();
8✔
42
    }
8✔
43

44

45
    internal static void ReadValue(ref Utf8JsonReader reader, EntityFilter value, JsonSerializerOptions options)
46
    {
47
        if (TryReadStringProperty(ref reader, Name, out var propertyValue))
54✔
48
        {
49
            value.Name = propertyValue;
16✔
50
        }
51
        else if (TryReadStringProperty(ref reader, Operator, out propertyValue))
38✔
52
        {
53
            value.Operator = propertyValue;
16✔
54
        }
55
        else if (TryReadStringProperty(ref reader, Logic, out propertyValue))
22✔
56
        {
57
            value.Logic = propertyValue;
4✔
58
        }
59
        else if (TryReadObjectProperty(ref reader, Value, out var objectValue))
18✔
60
        {
61
            value.Value = objectValue;
14✔
62
        }
63
        else if (reader.ValueTextEquals(Filters.EncodedUtf8Bytes))
4✔
64
        {
65
            reader.Read();
4✔
66
            value.Filters = JsonSerializer.Deserialize<List<EntityFilter>>(ref reader, options);
4✔
67
        }
68
    }
4✔
69

70
    internal static bool TryReadStringProperty(ref Utf8JsonReader reader, JsonEncodedText propertyName, out string? value)
71
    {
72
        if (!reader.ValueTextEquals(propertyName.EncodedUtf8Bytes))
114✔
73
        {
74
            value = default;
78✔
75
            return false;
78✔
76
        }
77

78
        reader.Read();
36✔
79
        value = reader.GetString()!;
36✔
80
        return true;
36✔
81
    }
82

83
    internal static bool TryReadObjectProperty(ref Utf8JsonReader reader, JsonEncodedText propertyName, out object? value)
84
    {
85
        if (!reader.ValueTextEquals(propertyName.EncodedUtf8Bytes))
18✔
86
        {
87
            value = default;
4✔
88
            return false;
4✔
89
        }
90

91
        reader.Read();
14✔
92
        switch (reader.TokenType)
14!
93
        {
94
            case JsonTokenType.String:
95
                value = reader.GetString();
8✔
96
                return true;
8✔
97
            case JsonTokenType.Number:
98
                value = reader.GetDouble();
2✔
99
                return true;
2✔
100
            case JsonTokenType.True:
101
            case JsonTokenType.False:
102
                value = reader.GetBoolean();
4✔
103
                return true;
4✔
104
            case JsonTokenType.Null:
105
                value = default;
×
106
                return true;
×
107
            default:
108
                value = default;
×
109
                throw new JsonException("Unexcepted end when reading JSON.");
×
110
        }
111
    }
112

113
    internal static void WriteEntityFilter(Utf8JsonWriter writer, EntityFilter value, JsonSerializerOptions options)
114
    {
115
        if (value.Logic != null)
8✔
116
        {
117
            writer.WriteString(Logic, value.Logic);
2✔
118
        }
119

120
        if (value.Name != null)
8✔
121
        {
122
            writer.WriteString(Name, value.Name);
6✔
123
        }
124

125
        if (value.Operator != null)
8✔
126
        {
127
            writer.WriteString(Operator, value.Operator);
6✔
128
        }
129

130
        if (value.Value != null)
8✔
131
        {
132
            writer.WritePropertyName(Value);
4✔
133
            JsonSerializer.Serialize(writer, value.Value, value.Value?.GetType() ?? typeof(object), options);
4!
134
        }
135

136
        if (value.Filters != null)
8✔
137
        {
138
            writer.WritePropertyName(Filters);
2✔
139
            writer.WriteStartArray();
2✔
140
            foreach (var filter in value.Filters)
12✔
141
                JsonSerializer.Serialize(writer, filter, typeof(EntityFilter), options);
4✔
142

143
            writer.WriteEndArray();
2✔
144
        }
145
    }
8✔
146
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc