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

loresoft / MediatR.CommandQuery / 8410320720

24 Mar 2024 03:15PM UTC coverage: 58.562%. Remained the same
8410320720

push

github

pwelter34
Update EntityQueryEndpointBase.cs

230 of 438 branches covered (52.51%)

Branch coverage included in aggregate %.

560 of 911 relevant lines covered (61.47%)

19.53 hits per line

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

0.0
/src/MediatR.CommandQuery.Cosmos/CosmosKey.cs
1
using System;
2
using System.Diagnostics.CodeAnalysis;
3

4
using Cosmos.Abstracts;
5
using Cosmos.Abstracts.Extensions;
6

7
using Microsoft.Azure.Cosmos;
8

9
namespace MediatR.CommandQuery.Cosmos;
10

11
public static class CosmosKey
12
{
13
    public static bool TryDecode(string cosmosKey, [NotNullWhen(true)] out string? id, out PartitionKey partitionKey)
14
    {
15
        if (string.IsNullOrWhiteSpace(cosmosKey))
×
16
        {
17
            id = null;
×
18
            partitionKey = default;
×
19
            return false;
×
20
        }
21

22
        var parts = cosmosKey.Split('~');
×
23
        if (parts.Length == 1)
×
24
        {
25
            id = parts[0];
×
26
            partitionKey = default;
×
27
            return id.HasValue();
×
28
        }
29

30
        if (parts.Length >= 2)
×
31
        {
32
            id = parts[0];
×
33
            partitionKey = new PartitionKey(parts[1]);
×
34
            return id.HasValue();
×
35
        }
36

37
        id = null;
×
38
        partitionKey = default;
×
39
        return false;
×
40
    }
41

42

43
    public static string Encode(string id, string partitionKey)
44
    {
45
        if (id.IsNullOrWhiteSpace())
×
46
            throw new ArgumentNullException(nameof(id));
×
47

48
        return partitionKey.HasValue() ? $"{id}~{partitionKey}" : id;
×
49
    }
50

51
    public static string Encode(string id, PartitionKey partitionKey)
52
    {
53
        if (id.IsNullOrWhiteSpace())
×
54
            throw new ArgumentNullException(nameof(id));
×
55

56
        if (partitionKey == default)
×
57
            return id;
×
58

59
        var json = partitionKey.ToString();
×
60
        var keys = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>(json);
×
61
        if (keys == null || keys.Length < 1)
×
62
            return id;
×
63

64
        return partitionKey != default ? $"{id}~{keys[0]}" : id;
×
65
    }
66

67
    public static string ToCosmosKey(this ICosmosEntity cosmosEntity)
68
    {
69
        if (cosmosEntity == null)
×
70
            throw new ArgumentNullException(nameof(cosmosEntity));
×
71

72
        var partitionKey = cosmosEntity.GetPartitionKey();
×
73

74
        return Encode(cosmosEntity.Id, partitionKey);
×
75
    }
76
}
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

© 2025 Coveralls, Inc