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

loresoft / MediatR.CommandQuery / 9307610999

30 May 2024 06:43PM UTC coverage: 57.741%. Remained the same
9307610999

Pull #513

github

web-flow
Merge e90664f48 into 7184530a4
Pull Request #513: Bump Microsoft.NET.Test.Sdk from 17.9.0 to 17.10.0

348 of 708 branches covered (49.15%)

Branch coverage included in aggregate %.

1032 of 1682 relevant lines covered (61.36%)

19.52 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.Diagnostics.CodeAnalysis;
2

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

6
using Microsoft.Azure.Cosmos;
7

8
namespace MediatR.CommandQuery.Cosmos;
9

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

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

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

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

41

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

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

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

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

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

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

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

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

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