-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathEntityInstanceId.cs
More file actions
88 lines (75 loc) · 2.91 KB
/
Copy pathEntityInstanceId.cs
File metadata and controls
88 lines (75 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Microsoft.DurableTask.Entities;
/// <summary>
/// Represents the ID of an entity.
/// </summary>
[JsonConverter(typeof(EntityInstanceId.JsonConverter))]
public readonly record struct EntityInstanceId
{
const int MaxInstanceIdLength = 100;
/// <summary>
/// Initializes a new instance of the <see cref="EntityInstanceId"/> struct.
/// </summary>
/// <param name="name">The entity name.</param>
/// <param name="key">The entity key.</param>
public EntityInstanceId(string name, string key)
{
Check.NotNullOrEmpty(name);
if (name.Contains('@'))
{
throw new ArgumentException("entity names may not contain `@` characters.", nameof(name));
}
Check.NotNull(key);
this.Name = name.ToLowerInvariant();
this.Key = key;
int length = this.ToString().Length;
if (length > MaxInstanceIdLength)
{
throw new ArgumentException($"entity instance ids may not exceed 100 characters in length, actual length {length}.");
}
}
/// <summary>
/// Gets the entity name. Entity names are normalized to lower case.
/// </summary>
public string Name { get; }
/// <summary>
/// Gets the entity key.
/// </summary>
public string Key { get; }
/// <summary>
/// Constructs a <see cref="EntityInstanceId"/> from a string containing the instance ID.
/// </summary>
/// <param name="instanceId">The string representation of the entity ID.</param>
/// <returns>the constructed entity instance ID.</returns>
public static EntityInstanceId FromString(string instanceId)
{
Check.NotNullOrEmpty(instanceId);
var pos = instanceId.IndexOf('@', 1);
if (pos <= 0 || instanceId[0] != '@')
{
throw new ArgumentException($"Instance ID '{instanceId}' is not a valid entity ID.", nameof(instanceId));
}
var entityName = instanceId.Substring(1, pos - 1);
var entityKey = instanceId.Substring(pos + 1);
return new EntityInstanceId(entityName, entityKey);
}
/// <inheritdoc/>
public override string ToString() => $"@{this.Name}@{this.Key}";
/// <summary>
/// We override the default json conversion so we can use a more compact string representation for entity instance ids.
/// </summary>
class JsonConverter : JsonConverter<EntityInstanceId>
{
public override EntityInstanceId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return EntityInstanceId.FromString(reader.GetString()!);
}
public override void Write(Utf8JsonWriter writer, EntityInstanceId value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString()!);
}
}
}