Skip to content

Commit d73af6c

Browse files
committed
Improve handcrafted types
1 parent 824a6b8 commit d73af6c

File tree

6 files changed

+161
-99
lines changed

6 files changed

+161
-99
lines changed

src/Elastic.Clients.Elasticsearch.Serverless/Elastic.Clients.Elasticsearch.Serverless.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<ItemGroup>
2424
<PackageReference Include="Elastic.Transport" Version="0.4.26" />
2525
</ItemGroup>
26+
<ItemGroup>
27+
<PackageReference Condition="'$(TargetFramework)'=='net462' or '$(TargetFramework)'=='netstandard2.0'" Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
28+
</ItemGroup>
2629
<ItemGroup>
2730
<InternalsVisibleTo Include="Tests" Key="$(ExposedPublicKey)" />
2831
<InternalsVisibleTo Include="Tests.Domain" Key="$(ExposedPublicKey)" />

src/Elastic.Clients.Elasticsearch/Elastic.Clients.Elasticsearch.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<ItemGroup>
2424
<PackageReference Include="Elastic.Transport" Version="0.4.26" />
2525
</ItemGroup>
26+
<ItemGroup>
27+
<PackageReference Condition="'$(TargetFramework)'=='net462' or '$(TargetFramework)'=='netstandard2.0'" Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
28+
</ItemGroup>
2629
<ItemGroup>
2730
<InternalsVisibleTo Include="Tests" Key="$(ExposedPublicKey)" />
2831
<InternalsVisibleTo Include="Tests.Domain" Key="$(ExposedPublicKey)" />

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ namespace Elastic.Clients.Elasticsearch;
2828
/// therefore cannot be specifically typed.
2929
/// </summary>
3030
[JsonConverter(typeof(FieldValueConverter))]
31-
public readonly struct FieldValue : IEquatable<FieldValue>
31+
public readonly struct FieldValue :
32+
IEquatable<FieldValue>
3233
{
3334
internal FieldValue(ValueKind kind, object? value)
3435
{
@@ -42,7 +43,7 @@ internal FieldValue(ValueKind kind, object? value)
4243
public readonly ValueKind Kind { get; }
4344

4445
/// <summary>
45-
/// The value contained within within this <see cref="FieldValue"/>.
46+
/// The value contained within this <see cref="FieldValue"/>.
4647
/// </summary>
4748
public readonly object? Value { get; }
4849

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

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using System;
66
using System.Diagnostics;
7-
using System.Diagnostics.CodeAnalysis;
87
using System.Globalization;
98
using System.Linq.Expressions;
109
using System.Reflection;
@@ -15,7 +14,6 @@
1514
#if ELASTICSEARCH_SERVERLESS
1615
namespace Elastic.Clients.Elasticsearch.Serverless;
1716
#else
18-
1917
namespace Elastic.Clients.Elasticsearch;
2018
#endif
2119

@@ -25,6 +23,10 @@ public sealed class Field :
2523
IEquatable<Field>,
2624
IUrlParameter
2725
{
26+
#if !NET && !NETSTANDARD2_1_OR_GREATER
27+
private static readonly char[] BoostSplit = ['^'];
28+
#endif
29+
2830
private readonly object _comparisonValue;
2931
private readonly Type? _type;
3032

@@ -67,22 +69,10 @@ public sealed class Field :
6769

6870
#region Constructors
6971

70-
public Field(string name) : this(name, null, null)
71-
{
72-
}
73-
74-
public Field(string name, double boost) : this(name, boost, null)
75-
{
76-
}
77-
78-
public Field(string name, string format) : this(name, null, format)
79-
{
80-
}
81-
82-
public Field(string name, double? boost, string? format)
72+
public Field(string name, double? boost = null, string? format = null)
8373
{
8474
if (string.IsNullOrEmpty(name))
85-
throw new ArgumentException($"{name} can not be null or empty.", nameof(name));
75+
throw new ArgumentException("Field name can not be null or empty.", nameof(name));
8676

8777
Name = ParseFieldName(name, out var b);
8878
Boost = b ?? boost;
@@ -91,6 +81,16 @@ public Field(string name, double? boost, string? format)
9181
_comparisonValue = Name;
9282
}
9383

84+
public Field(string name, double boost) :
85+
this(name, boost, null)
86+
{
87+
}
88+
89+
public Field(string name, string format) :
90+
this(name, null, format)
91+
{
92+
}
93+
9494
public Field(Expression expression, double? boost = null, string? format = null)
9595
{
9696
Expression = expression ?? throw new ArgumentNullException(nameof(expression));
@@ -117,21 +117,21 @@ public Field(PropertyInfo property, double? boost = null, string? format = null)
117117

118118
#region Factory Methods
119119

120-
public static Field? FromString(string? name) => string.IsNullOrEmpty(name) ? null : new Field(name);
120+
public static Field FromString(string name) => new(name);
121121

122-
public static Field? FromExpression(Expression? expression) => expression is null ? null : new Field(expression);
122+
public static Field FromExpression(Expression expression) => new(expression);
123123

124-
public static Field? FromProperty(PropertyInfo? property) => property is null ? null : new Field(property);
124+
public static Field FromProperty(PropertyInfo property) => new(property);
125125

126126
#endregion Factory Methods
127127

128128
#region Conversion Operators
129129

130-
public static implicit operator Field?(string? name) => FromString(name);
130+
public static implicit operator Field(string name) => FromString(name);
131131

132-
public static implicit operator Field?(Expression? expression) => FromExpression(expression);
132+
public static implicit operator Field(Expression expression) => FromExpression(expression);
133133

134-
public static implicit operator Field?(PropertyInfo? property) => FromProperty(property);
134+
public static implicit operator Field(PropertyInfo property) => FromProperty(property);
135135

136136
#endregion Conversion Operators
137137

@@ -203,15 +203,7 @@ public override bool Equals(object? obj) =>
203203
_ => false
204204
};
205205

206-
public override int GetHashCode()
207-
{
208-
unchecked
209-
{
210-
var hashCode = _comparisonValue?.GetHashCode() ?? 0;
211-
hashCode = (hashCode * 397) ^ (_type?.GetHashCode() ?? 0);
212-
return hashCode;
213-
}
214-
}
206+
public override int GetHashCode() => HashCode.Combine(_comparisonValue, _type);
215207

216208
#endregion Equality
217209

@@ -243,20 +235,29 @@ public override string ToString() =>
243235

244236
#endregion Debugging
245237

246-
[return: NotNullIfNotNull(nameof(name))]
247-
private static string? ParseFieldName(string? name, out double? boost)
238+
private static string ParseFieldName(string name, out double? boost)
248239
{
249240
boost = null;
250-
if (name is null)
251-
return null;
252241

253-
var caretIndex = name.IndexOf('^');
254-
if (caretIndex == -1)
242+
#if NET || NETSTANDARD2_1_OR_GREATER
243+
if (!name.Contains('^', StringComparison.Ordinal))
255244
return name;
245+
#else
246+
if (name.IndexOf('^') == -1)
247+
return name;
248+
#endif
256249

257-
var parts = name.Split(new[] { '^' }, 2, StringSplitOptions.RemoveEmptyEntries);
250+
#if NET || NETSTANDARD2_1_OR_GREATER
251+
var parts = name.Split('^', 2, StringSplitOptions.RemoveEmptyEntries);
252+
#else
253+
var parts = name.Split(BoostSplit, 2, StringSplitOptions.RemoveEmptyEntries);
254+
#endif
258255
name = parts[0];
259256
boost = double.Parse(parts[1], CultureInfo.InvariantCulture);
257+
258+
if (string.IsNullOrWhiteSpace(name))
259+
throw new ArgumentException("Field name can not be null or empty.", nameof(name));
260+
260261
return name;
261262
}
262263
}

0 commit comments

Comments
 (0)