Skip to content

Commit 7b649db

Browse files
committed
Implement IParsable<T> for url parameters
1 parent 4e18b84 commit 7b649db

File tree

18 files changed

+633
-57
lines changed

18 files changed

+633
-57
lines changed

global.json

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
{
22
"sdk": {
3-
"version": "8.0.400",
3+
"version": "9.0.100",
44
"rollForward": "latestFeature",
55
"allowPrerelease": false
6-
},
7-
"version": "8.8.0-alpha.1",
8-
"doc_current": "main",
9-
"doc_branch": "main"
6+
}
107
}

src/Elastic.Clients.Elasticsearch/_Shared/Core/DateTime/Duration.cs

+42-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
68
using System.Globalization;
79
using System.Text.Json;
810
using System.Text.Json.Serialization;
@@ -15,7 +17,13 @@ namespace Elastic.Clients.Elasticsearch;
1517
/// Represents a duration value.
1618
/// </summary>
1719
[JsonConverter(typeof(DurationConverter))]
18-
public sealed class Duration : IComparable<Duration>, IEquatable<Duration>, IUrlParameter
20+
public sealed class Duration :
21+
IComparable<Duration>,
22+
IEquatable<Duration>,
23+
IUrlParameter
24+
#if NET7_0_OR_GREATER
25+
, IParsable<Duration>
26+
#endif
1927
{
2028
private const double MicrosecondsInATick = 0.1; // 10 ticks = 1 microsecond
2129
private const double MillisecondsInADay = MillisecondsInAnHour * 24;
@@ -390,6 +398,39 @@ private static string ExponentFormat(double d)
390398
var exponent = (int)((bits >> 52) & 0x7ffL);
391399
return new string('#', Math.Max(2, exponent));
392400
}
401+
402+
#region IParsable
403+
404+
#if NET7_0_OR_GREATER
405+
406+
public static Duration Parse(string s, IFormatProvider? provider) =>
407+
TryParse(s, provider, out var result) ? result : throw new FormatException();
408+
409+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider,
410+
[NotNullWhen(true)] out Duration? result)
411+
{
412+
if (s is null)
413+
{
414+
result = null;
415+
return false;
416+
}
417+
418+
try
419+
{
420+
result = new Duration(s);
421+
}
422+
catch
423+
{
424+
result = null;
425+
return false;
426+
}
427+
428+
return true;
429+
}
430+
431+
#endif
432+
433+
#endregion IParsable
393434
}
394435

395436
internal sealed class DurationConverter : JsonConverter<Duration>

src/Elastic.Clients.Elasticsearch/_Shared/Core/Fluent/Descriptor.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public abstract class Descriptor<TDescriptor> : Descriptor
5858
protected TDescriptor Self => _self;
5959

6060
[MethodImpl(MethodImplOptions.AggressiveInlining)]
61-
protected TDescriptor Assign<TValue>(TValue value, Action<TDescriptor, TValue> assign) => FluentAssign.Assign(_self, value, assign);
61+
protected TDescriptor Assign<TValue>(TValue value, Action<TDescriptor, TValue> assign)
62+
{
63+
assign(_self, value);
64+
return _self;
65+
}
6266
}
6367

6468
public abstract class SerializableDescriptor<TDescriptor> : Descriptor<TDescriptor>, ISelfSerializable

src/Elastic.Clients.Elasticsearch/_Shared/Core/Fluent/Fluent.cs

-19
This file was deleted.

src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Field/Field.cs

+27
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ namespace Elastic.Clients.Elasticsearch;
2424
public sealed class Field :
2525
IEquatable<Field>,
2626
IUrlParameter
27+
#if NET7_0_OR_GREATER
28+
, IParsable<Field>
29+
#endif
2730
{
2831
private readonly object _comparisonValue;
2932
private readonly Type? _type;
@@ -197,6 +200,30 @@ public override int GetHashCode()
197200

198201
#endregion Equality
199202

203+
#region IParsable
204+
205+
#if NET7_0_OR_GREATER
206+
207+
public static Field Parse(string s, IFormatProvider? provider) =>
208+
TryParse(s, provider, out var result) ? result : throw new FormatException();
209+
210+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider,
211+
[NotNullWhen(true)] out Field? result)
212+
{
213+
if (s is null)
214+
{
215+
result = null;
216+
return false;
217+
}
218+
219+
result = new Field(s);
220+
return true;
221+
}
222+
223+
#endif
224+
225+
#endregion IParsable
226+
200227
#region IUrlParameter
201228

202229
string IUrlParameter.GetString(ITransportConfiguration settings)

src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Fields/Fields.cs

+46
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9+
using System.Diagnostics.CodeAnalysis;
910
using System.Linq;
1011
using System.Linq.Expressions;
1112
using System.Reflection;
@@ -19,6 +20,9 @@ public sealed class Fields :
1920
IEquatable<Fields>,
2021
IEnumerable<Field>,
2122
IUrlParameter
23+
#if NET7_0_OR_GREATER
24+
, IParsable<Fields>
25+
#endif
2226
{
2327
internal readonly List<Field> ListOfFields;
2428

@@ -183,6 +187,48 @@ public override bool Equals(object? obj) =>
183187

184188
#endregion IEnumerable
185189

190+
#region IParsable
191+
192+
#if NET7_0_OR_GREATER
193+
194+
public static Fields Parse(string s, IFormatProvider? provider) =>
195+
TryParse(s, provider, out var result) ? result : throw new FormatException();
196+
197+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider,
198+
[NotNullWhen(true)] out Fields? result)
199+
{
200+
if (s is null)
201+
{
202+
result = null;
203+
return false;
204+
}
205+
206+
if (s.IsNullOrEmptyCommaSeparatedList(out var list))
207+
{
208+
result = new Fields();
209+
return true;
210+
}
211+
212+
var fields = new List<Field>();
213+
foreach (var item in list)
214+
{
215+
if (!Field.TryParse(item, provider, out var field))
216+
{
217+
result = null;
218+
return false;
219+
}
220+
221+
fields.Add(field);
222+
}
223+
224+
result = new Fields(fields);
225+
return true;
226+
}
227+
228+
#endif
229+
230+
#endregion IParsable
231+
186232
#region IUrlParameter
187233

188234
string IUrlParameter.GetString(ITransportConfiguration? settings)

src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Id/Id.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,23 @@
44

55
using System;
66
using System.Diagnostics;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Globalization;
89
using System.Text.Json;
910
using System.Text.Json.Serialization;
11+
1012
using Elastic.Transport;
1113

1214
namespace Elastic.Clients.Elasticsearch;
1315

1416
[DebuggerDisplay("{DebugDisplay,nq}")]
1517
[JsonConverter(typeof(IdConverter))]
16-
public class Id : IEquatable<Id>, IUrlParameter
18+
public class Id :
19+
IEquatable<Id>,
20+
IUrlParameter
21+
#if NET7_0_OR_GREATER
22+
, IParsable<Id>
23+
#endif
1724
{
1825
public Id(string id)
1926
{
@@ -81,12 +88,16 @@ public override bool Equals(object obj)
8188
{
8289
case Id r:
8390
return Equals(r);
91+
8492
case string s:
8593
return Equals(s);
94+
8695
case int l:
8796
return Equals(l);
97+
8898
case long l:
8999
return Equals(l);
100+
90101
case Guid g:
91102
return Equals(g);
92103
}
@@ -108,4 +119,28 @@ public override int GetHashCode()
108119
public static bool operator ==(Id left, Id right) => Equals(left, right);
109120

110121
public static bool operator !=(Id left, Id right) => !Equals(left, right);
122+
123+
#region IParsable
124+
125+
#if NET7_0_OR_GREATER
126+
127+
public static Id Parse(string s, IFormatProvider? provider) =>
128+
TryParse(s, provider, out var result) ? result : throw new FormatException();
129+
130+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider,
131+
[NotNullWhen(true)] out Id? result)
132+
{
133+
if (s is null)
134+
{
135+
result = null;
136+
return false;
137+
}
138+
139+
result = new Id(s);
140+
return true;
141+
}
142+
143+
#endif
144+
145+
#endregion IParsable
111146
}

src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Id/Ids.cs

+56-2
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,42 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Diagnostics;
8+
using System.Diagnostics.CodeAnalysis;
89
using System.Linq;
910
using System.Text.Json.Serialization;
11+
1012
using Elastic.Transport;
1113

1214
namespace Elastic.Clients.Elasticsearch;
1315

1416
[DebuggerDisplay("{DebugDisplay,nq}")]
1517
[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
1724
{
1825
private readonly IList<Id> _ids;
1926

2027
public Ids(IEnumerable<Id> ids) => _ids = ids.ToList();
2128

2229
public Ids(IList<Id> ids) => _ids = ids;
2330

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();
2532

2633
public Ids(string value)
2734
{
2835
if (!value.IsNullOrEmptyCommaSeparatedList(out var arr))
2936
_ids = arr.Select(i => new Id(i)).ToList();
3037
}
3138

39+
public Ids()
40+
{
41+
_ids = [];
42+
}
43+
3244
internal IList<Id> IdsToSerialize => _ids;
3345

3446
private string DebugDisplay => ((IUrlParameter)this).GetString(null);
@@ -82,4 +94,46 @@ public override int GetHashCode()
8294
public static bool operator ==(Ids left, Ids right) => Equals(left, right);
8395

8496
public static bool operator !=(Ids left, Ids right) => !Equals(left, right);
97+
98+
#region IParsable
99+
100+
#if NET7_0_OR_GREATER
101+
102+
public static Ids Parse(string s, IFormatProvider? provider) =>
103+
TryParse(s, provider, out var result) ? result : throw new FormatException();
104+
105+
public static bool TryParse([NotNullWhen(true)] string? s, IFormatProvider? provider,
106+
[NotNullWhen(true)] out Ids? result)
107+
{
108+
if (s is null)
109+
{
110+
result = null;
111+
return false;
112+
}
113+
114+
if (s.IsNullOrEmptyCommaSeparatedList(out var list))
115+
{
116+
result = new Ids();
117+
return true;
118+
}
119+
120+
var ids = new List<Id>();
121+
foreach (var item in list)
122+
{
123+
if (!Id.TryParse(item, provider, out var id))
124+
{
125+
result = null;
126+
return false;
127+
}
128+
129+
ids.Add(id);
130+
}
131+
132+
result = new Ids(ids);
133+
return true;
134+
}
135+
136+
#endif
137+
138+
#endregion IParsable
85139
}

0 commit comments

Comments
 (0)