-
Notifications
You must be signed in to change notification settings - Fork 53
Revise EntityInstanceId implementation and add checking #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sebastianburckhardt
merged 5 commits into
feature/entities
from
core-entities/entity-instance-id-normalization
Oct 9, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c02eb5
revise EntityInstanceId implementation and checking
sebastianburckhardt 6627f9f
address PR feedback: add switch to enable/disable entity support and …
sebastianburckhardt 2575fbc
one more validation check for entity names
sebastianburckhardt f6a2299
remove checks that were supposed to be removed but got re-added durin…
sebastianburckhardt 1afff32
address PR feedback
sebastianburckhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,80 @@ | ||
| // 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> | ||
| /// <param name="Name">The name of the entity.</param> | ||
| /// <param name="Key">The key for this entity.</param> | ||
| public readonly record struct EntityInstanceId(string Name, string Key) | ||
| [JsonConverter(typeof(EntityInstanceId.JsonConverter))] | ||
| public readonly record struct EntityInstanceId | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="EntityInstanceId"/> class. | ||
| /// </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; | ||
| } | ||
|
|
||
| /// <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()!); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: put static methods before instance.