|
5 | 5 | using System;
|
6 | 6 | using System.Collections.Generic;
|
7 | 7 | using System.Diagnostics;
|
| 8 | +using System.Diagnostics.CodeAnalysis; |
8 | 9 | using System.Linq;
|
9 | 10 | using System.Text.Json.Serialization;
|
| 11 | + |
10 | 12 | using Elastic.Transport;
|
11 | 13 |
|
12 | 14 | namespace Elastic.Clients.Elasticsearch;
|
13 | 15 |
|
14 | 16 | [DebuggerDisplay("{DebugDisplay,nq}")]
|
15 | 17 | [JsonConverter(typeof(IdsConverter))]
|
16 |
| -public partial class Ids : IUrlParameter, IEquatable<Ids> |
| 18 | +public class Ids : |
| 19 | + IUrlParameter, |
| 20 | + IEquatable<Ids> |
| 21 | +#if NET7_0_OR_GREATER |
| 22 | + , IParsable<Ids> |
| 23 | +#endif |
17 | 24 | {
|
18 | 25 | private readonly IList<Id> _ids;
|
19 | 26 |
|
20 | 27 | public Ids(IEnumerable<Id> ids) => _ids = ids.ToList();
|
21 | 28 |
|
22 | 29 | public Ids(IList<Id> ids) => _ids = ids;
|
23 | 30 |
|
24 |
| - public Ids(IEnumerable<string> ids) => _ids = ids.Select(i => new Id(i)).ToList(); |
| 31 | + public Ids(IEnumerable<string> ids) => _ids = ids.Select(i => new Id(i)).ToList(); |
25 | 32 |
|
26 | 33 | public Ids(string value)
|
27 | 34 | {
|
28 | 35 | if (!value.IsNullOrEmptyCommaSeparatedList(out var arr))
|
29 | 36 | _ids = arr.Select(i => new Id(i)).ToList();
|
30 | 37 | }
|
31 | 38 |
|
| 39 | + public Ids() |
| 40 | + { |
| 41 | + _ids = []; |
| 42 | + } |
| 43 | + |
32 | 44 | internal IList<Id> IdsToSerialize => _ids;
|
33 | 45 |
|
34 | 46 | private string DebugDisplay => ((IUrlParameter)this).GetString(null);
|
@@ -82,4 +94,42 @@ public override int GetHashCode()
|
82 | 94 | public static bool operator ==(Ids left, Ids right) => Equals(left, right);
|
83 | 95 |
|
84 | 96 | public static bool operator !=(Ids left, Ids right) => !Equals(left, right);
|
| 97 | + |
| 98 | + #region IParsable |
| 99 | + |
| 100 | + public static Ids Parse(string s, IFormatProvider? provider) => |
| 101 | + TryParse(s, provider, out var result) ? result : throw new FormatException(); |
| 102 | + |
| 103 | + public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider, |
| 104 | + [NotNullWhen(true)] out Ids? result) |
| 105 | + { |
| 106 | + if (s is null) |
| 107 | + { |
| 108 | + result = null; |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + if (s.IsNullOrEmptyCommaSeparatedList(out var list)) |
| 113 | + { |
| 114 | + result = new Ids(); |
| 115 | + return true; |
| 116 | + } |
| 117 | + |
| 118 | + var ids = new List<Id>(); |
| 119 | + foreach (var item in list) |
| 120 | + { |
| 121 | + if (!Id.TryParse(item, provider, out var id)) |
| 122 | + { |
| 123 | + result = null; |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + ids.Add(id); |
| 128 | + } |
| 129 | + |
| 130 | + result = new Ids(ids); |
| 131 | + return true; |
| 132 | + } |
| 133 | + |
| 134 | + #endregion IParsable |
85 | 135 | }
|
0 commit comments