-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathTypeExtensions.cs
More file actions
98 lines (90 loc) · 4.1 KB
/
Copy pathTypeExtensions.cs
File metadata and controls
98 lines (90 loc) · 4.1 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
89
90
91
92
93
94
95
96
97
98
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
namespace Microsoft.DurableTask;
/// <summary>
/// Extensions for <see cref="Type" />.
/// </summary>
static class TypeExtensions
{
/// <summary>
/// Gets the task name for a type.
/// </summary>
/// <param name="type">The type to get a task name for.</param>
/// <returns>The task name.</returns>
public static TaskName GetTaskName(this Type type)
{
// IMPORTANT: This logic needs to be kept consistent with the source generator logic.
Check.NotNull(type);
return Attribute.GetCustomAttribute(type, typeof(DurableTaskAttribute)) switch
{
DurableTaskAttribute { Name.Name: not null and not "" } attr => attr.Name,
_ => new TaskName(type.Name),
};
}
/// <summary>
/// Gets the durable task version for a type.
/// </summary>
/// <param name="type">The type to get the durable task version for.</param>
/// <returns>The durable task version.</returns>
/// <remarks>
/// When the <see cref="DurableTaskAttribute.Version"/> declares multiple comma-separated versions, this
/// returns only the first declared version. Prefer <see cref="GetDurableTaskVersions"/> when all declared
/// versions are needed (for example, when registering a type under every version it supports).
/// </remarks>
internal static TaskVersion GetDurableTaskVersion(this Type type)
{
// IMPORTANT: This logic needs to be kept consistent with the source generator logic.
Check.NotNull(type);
IReadOnlyList<TaskVersion> versions = type.GetDurableTaskVersions();
return versions.Count > 0 ? versions[0] : default;
}
/// <summary>
/// Gets every durable task version declared for a type via <see cref="DurableTaskAttribute.Version"/>.
/// </summary>
/// <param name="type">The type to get the durable task versions for.</param>
/// <returns>
/// The distinct (case-insensitive) set of declared versions in declaration order. An unversioned type
/// (no attribute, or an empty/unset <see cref="DurableTaskAttribute.Version"/>) yields a single
/// <see cref="TaskVersion.Unversioned"/> entry so callers can always register at least once.
/// </returns>
/// <exception cref="ArgumentException">
/// Thrown when any comma-separated entry is whitespace-only. This mirrors the source generator's
/// <c>DURABLE3005</c> diagnostic so the reflection-based registration path fails closed for types whose
/// attribute the generator did not see.
/// </exception>
internal static IReadOnlyList<TaskVersion> GetDurableTaskVersions(this Type type)
{
// IMPORTANT: This logic needs to be kept consistent with the source generator logic.
Check.NotNull(type);
if (Attribute.GetCustomAttribute(type, typeof(DurableTaskAttribute)) is not DurableTaskAttribute attr
|| string.IsNullOrEmpty(attr.Version))
{
return new[] { TaskVersion.Unversioned };
}
List<TaskVersion> versions = new();
foreach (string segment in attr.Version!.Split(','))
{
if (segment.Length == 0)
{
// Truly-empty entry (e.g. a trailing or doubled comma). Skip silently.
continue;
}
string trimmed = segment.Trim();
if (trimmed.Length == 0)
{
// Whitespace-only entry. Fail closed, consistent with the TaskVersion constructor and the
// source generator's DURABLE3005 diagnostic.
throw new ArgumentException(
"A [DurableTask] Version entry must not be whitespace-only. Provide non-empty version " +
"values or omit the Version argument to declare an unversioned task.",
nameof(type));
}
TaskVersion version = new(trimmed);
if (!versions.Contains(version))
{
versions.Add(version);
}
}
return versions.Count > 0 ? versions : new[] { TaskVersion.Unversioned };
}
}