|
2 | 2 | // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. |
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | | -namespace Elastic.Clients.Elasticsearch |
| 5 | +using System; |
| 6 | +using System.Diagnostics; |
| 7 | +using System.Globalization; |
| 8 | +using System.Text.Json; |
| 9 | +using System.Text.Json.Serialization; |
| 10 | +using Elastic.Transport; |
| 11 | + |
| 12 | +namespace Elastic.Clients.Elasticsearch; |
| 13 | + |
| 14 | +[JsonConverter(typeof(TaskIdConverter))] |
| 15 | +[DebuggerDisplay("{DebugDisplay,nq}")] |
| 16 | +public partial class TaskId : IUrlParameter, IEquatable<TaskId> |
6 | 17 | { |
7 | | - //[DebuggerDisplay("{DebugDisplay,nq}")] |
8 | | - //public partial class TaskId : IUrlParameter, IEquatable<TaskId> |
9 | | - //{ |
10 | | - // /// <summary> |
11 | | - // /// A task id exists in the form [node_id]:[task_id] |
12 | | - // /// </summary> |
13 | | - // /// <param name="taskId">the task identifier</param> |
14 | | - // public TaskId(string taskId) |
15 | | - // { |
16 | | - // if (string.IsNullOrWhiteSpace(taskId)) |
17 | | - // throw new ArgumentException("TaskId can not be an empty string", nameof(taskId)); |
| 18 | + /// <summary> |
| 19 | + /// A task id exists in the form [node_id]:[task_id] |
| 20 | + /// </summary> |
| 21 | + /// <param name="taskId">the task identifier</param> |
| 22 | + public TaskId(string taskId) |
| 23 | + { |
| 24 | + if (string.IsNullOrWhiteSpace(taskId)) |
| 25 | + throw new ArgumentException("TaskId can not be an empty string", nameof(taskId)); |
18 | 26 |
|
19 | | - // var tokens = taskId.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); |
20 | | - // if (tokens.Length != 2) |
21 | | - // throw new ArgumentException($"TaskId:{taskId} not in expected format of <node_id>:<task_id>", nameof(taskId)); |
| 27 | + var tokens = taskId.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries); |
| 28 | + if (tokens.Length != 2) |
| 29 | + throw new ArgumentException($"TaskId:{taskId} not in expected format of <node_id>:<task_id>", nameof(taskId)); |
22 | 30 |
|
23 | | - // NodeId = tokens[0]; |
24 | | - // FullyQualifiedId = taskId; |
| 31 | + NodeId = tokens[0]; |
| 32 | + FullyQualifiedId = taskId; |
25 | 33 |
|
26 | | - // if (!long.TryParse(tokens[1].Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out var t) || t < -1 || t == 0) |
27 | | - // throw new ArgumentException($"TaskId task component:{tokens[1]} could not be parsed to long or is out of range", nameof(taskId)); |
| 34 | + if (!long.TryParse(tokens[1].Trim(), NumberStyles.Any, CultureInfo.InvariantCulture, out var t) || t < -1 || t == 0) |
| 35 | + throw new ArgumentException($"TaskId task component:{tokens[1]} could not be parsed to long or is out of range", nameof(taskId)); |
28 | 36 |
|
29 | | - // TaskNumber = t; |
30 | | - // } |
| 37 | + TaskNumber = t; |
| 38 | + } |
31 | 39 |
|
32 | | - // public string FullyQualifiedId { get; } |
33 | | - // public string NodeId { get; } |
34 | | - // public long TaskNumber { get; } |
| 40 | + public string FullyQualifiedId { get; } |
| 41 | + public string NodeId { get; } |
| 42 | + public long TaskNumber { get; } |
35 | 43 |
|
36 | | - // private string DebugDisplay => FullyQualifiedId; |
| 44 | + private string DebugDisplay => FullyQualifiedId; |
37 | 45 |
|
38 | | - // public bool Equals(TaskId other) => EqualsString(other?.FullyQualifiedId); |
| 46 | + public bool Equals(TaskId other) => EqualsString(other?.FullyQualifiedId); |
39 | 47 |
|
40 | | - // public string GetString(ITransportConfiguration settings) => FullyQualifiedId; |
| 48 | + public string GetString(ITransportConfiguration settings) => FullyQualifiedId; |
41 | 49 |
|
42 | | - // public override string ToString() => FullyQualifiedId; |
| 50 | + public override string ToString() => FullyQualifiedId; |
43 | 51 |
|
44 | | - // public static implicit operator TaskId(string taskId) => taskId.IsNullOrEmpty() ? null : new TaskId(taskId); |
| 52 | + public static implicit operator TaskId(string taskId) => taskId.IsNullOrEmpty() ? null : new TaskId(taskId); |
45 | 53 |
|
46 | | - // public static bool operator ==(TaskId left, TaskId right) => Equals(left, right); |
| 54 | + public static bool operator ==(TaskId left, TaskId right) => Equals(left, right); |
47 | 55 |
|
48 | | - // public static bool operator !=(TaskId left, TaskId right) => !Equals(left, right); |
| 56 | + public static bool operator !=(TaskId left, TaskId right) => !Equals(left, right); |
49 | 57 |
|
50 | | - // public override bool Equals(object obj) => |
51 | | - // obj != null && obj is string s ? EqualsString(s) : obj is TaskId i && EqualsString(i.FullyQualifiedId); |
| 58 | + public override bool Equals(object obj) => |
| 59 | + obj != null && obj is string s ? EqualsString(s) : obj is TaskId i && EqualsString(i.FullyQualifiedId); |
52 | 60 |
|
53 | | - // private bool EqualsString(string other) => !other.IsNullOrEmpty() && other == FullyQualifiedId; |
| 61 | + private bool EqualsString(string other) => !other.IsNullOrEmpty() && other == FullyQualifiedId; |
54 | 62 |
|
55 | | - // public override int GetHashCode() |
56 | | - // { |
57 | | - // unchecked |
58 | | - // { |
59 | | - // return (NodeId.GetHashCode() * 397) ^ TaskNumber.GetHashCode(); |
60 | | - // } |
61 | | - // } |
62 | | - //} |
| 63 | + public override int GetHashCode() |
| 64 | + { |
| 65 | + unchecked |
| 66 | + { |
| 67 | + return (NodeId.GetHashCode() * 397) ^ TaskNumber.GetHashCode(); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +internal sealed class TaskIdConverter : JsonConverter<TaskId> |
| 73 | +{ |
| 74 | + public override TaskId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 75 | + { |
| 76 | + if (reader.TokenType == JsonTokenType.Null) |
| 77 | + return null; |
| 78 | + |
| 79 | + if (reader.TokenType == JsonTokenType.String) |
| 80 | + { |
| 81 | + var taskId = reader.GetString(); |
| 82 | + return new TaskId(taskId); |
| 83 | + } |
| 84 | + |
| 85 | + throw new JsonException("Unexpected JSON token"); |
| 86 | + } |
63 | 87 |
|
| 88 | + public override void Write(Utf8JsonWriter writer, TaskId value, JsonSerializerOptions options) => throw new NotImplementedException(); |
64 | 89 | } |
0 commit comments