forked from modelcontextprotocol/csharp-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressToken.cs
More file actions
96 lines (80 loc) · 3.4 KB
/
ProgressToken.cs
File metadata and controls
96 lines (80 loc) · 3.4 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
using System.ComponentModel;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace ModelContextProtocol.Protocol;
/// <summary>
/// Represents a progress token, which can be either a string or an integer.
/// </summary>
[JsonConverter(typeof(Converter))]
public readonly struct ProgressToken : IEquatable<ProgressToken>
{
/// <summary>The token, either a string or a boxed long or null.</summary>
private readonly object? _token;
/// <summary>Initializes a new instance of the <see cref="ProgressToken"/> with a specified value.</summary>
/// <param name="value">The required ID value.</param>
public ProgressToken(string value)
{
Throw.IfNull(value);
_token = value;
}
/// <summary>Initializes a new instance of the <see cref="ProgressToken"/> with a specified value.</summary>
/// <param name="value">The required ID value.</param>
public ProgressToken(long value)
{
// Box the long. Progress tokens are almost always strings in practice, so this should be rare.
_token = value;
}
/// <summary>Gets the underlying object for this token.</summary>
/// <remarks>This will either be a <see cref="string"/>, a boxed <see cref="long"/>, or <see langword="null"/>.</remarks>
public object? Token => _token;
/// <inheritdoc />
public override string? ToString() =>
_token is string stringValue ? stringValue :
_token is long longValue ? longValue.ToString(CultureInfo.InvariantCulture) :
null;
/// <inheritdoc />
public bool Equals(ProgressToken other) => Equals(_token, other._token);
/// <inheritdoc />
public override bool Equals(object? obj) => obj is ProgressToken other && Equals(other);
/// <inheritdoc />
public override int GetHashCode() => _token?.GetHashCode() ?? 0;
/// <inheritdoc />
public static bool operator ==(ProgressToken left, ProgressToken right) => left.Equals(right);
/// <inheritdoc />
public static bool operator !=(ProgressToken left, ProgressToken right) => !left.Equals(right);
/// <summary>
/// Provides a <see cref="JsonConverter"/> for <see cref="ProgressToken"/> that handles both string and number values.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public sealed class Converter : JsonConverter<ProgressToken>
{
/// <inheritdoc />
public override ProgressToken Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TokenType switch
{
JsonTokenType.String => new(reader.GetString()!),
JsonTokenType.Number => new(reader.GetInt64()),
_ => throw new JsonException("progressToken must be a string or an integer"),
};
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, ProgressToken value, JsonSerializerOptions options)
{
Throw.IfNull(writer);
switch (value._token)
{
case string str:
writer.WriteStringValue(str);
return;
case long longValue:
writer.WriteNumberValue(longValue);
return;
case null:
writer.WriteStringValue(string.Empty);
return;
}
}
}
}