Skip to content

Improve handcrafted types #8395

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -23,6 +23,9 @@
<ItemGroup>
<PackageReference Include="Elastic.Transport" Version="0.4.26" />
</ItemGroup>
<ItemGroup>
<PackageReference Condition="'$(TargetFramework)'=='net462' or '$(TargetFramework)'=='netstandard2.0'" Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Tests" Key="$(ExposedPublicKey)" />
<InternalsVisibleTo Include="Tests.Domain" Key="$(ExposedPublicKey)" />
Original file line number Diff line number Diff line change
@@ -23,6 +23,9 @@
<ItemGroup>
<PackageReference Include="Elastic.Transport" Version="0.4.26" />
</ItemGroup>
<ItemGroup>
<PackageReference Condition="'$(TargetFramework)'=='net462' or '$(TargetFramework)'=='netstandard2.0'" Include="Microsoft.Bcl.HashCode" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Tests" Key="$(ExposedPublicKey)" />
<InternalsVisibleTo Include="Tests.Domain" Key="$(ExposedPublicKey)" />
Original file line number Diff line number Diff line change
@@ -28,7 +28,8 @@ namespace Elastic.Clients.Elasticsearch;
/// therefore cannot be specifically typed.
/// </summary>
[JsonConverter(typeof(FieldValueConverter))]
public readonly struct FieldValue : IEquatable<FieldValue>
public readonly struct FieldValue :
IEquatable<FieldValue>
{
internal FieldValue(ValueKind kind, object? value)
{
@@ -42,7 +43,7 @@ internal FieldValue(ValueKind kind, object? value)
public readonly ValueKind Kind { get; }

/// <summary>
/// The value contained within within this <see cref="FieldValue"/>.
/// The value contained within this <see cref="FieldValue"/>.
/// </summary>
public readonly object? Value { get; }

77 changes: 39 additions & 38 deletions src/Elastic.Clients.Elasticsearch/_Shared/Core/Infer/Field/Field.cs
Original file line number Diff line number Diff line change
@@ -4,7 +4,6 @@

using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
@@ -15,7 +14,6 @@
#if ELASTICSEARCH_SERVERLESS
namespace Elastic.Clients.Elasticsearch.Serverless;
#else

namespace Elastic.Clients.Elasticsearch;
#endif

@@ -25,6 +23,10 @@ public sealed class Field :
IEquatable<Field>,
IUrlParameter
{
#if !NET && !NETSTANDARD2_1_OR_GREATER
private static readonly char[] BoostSplit = ['^'];
#endif

private readonly object _comparisonValue;
private readonly Type? _type;

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

#region Constructors

public Field(string name) : this(name, null, null)
{
}

public Field(string name, double boost) : this(name, boost, null)
{
}

public Field(string name, string format) : this(name, null, format)
{
}

public Field(string name, double? boost, string? format)
public Field(string name, double? boost = null, string? format = null)
{
if (string.IsNullOrEmpty(name))
throw new ArgumentException($"{name} can not be null or empty.", nameof(name));
throw new ArgumentException("Field name can not be null or empty.", nameof(name));

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

public Field(string name, double boost) :
this(name, boost, null)
{
}

public Field(string name, string format) :
this(name, null, format)
{
}

public Field(Expression expression, double? boost = null, string? format = null)
{
Expression = expression ?? throw new ArgumentNullException(nameof(expression));
@@ -117,21 +117,21 @@ public Field(PropertyInfo property, double? boost = null, string? format = null)

#region Factory Methods

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

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

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

#endregion Factory Methods

#region Conversion Operators

public static implicit operator Field?(string? name) => FromString(name);
public static implicit operator Field(string name) => FromString(name);

public static implicit operator Field?(Expression? expression) => FromExpression(expression);
public static implicit operator Field(Expression expression) => FromExpression(expression);

public static implicit operator Field?(PropertyInfo? property) => FromProperty(property);
public static implicit operator Field(PropertyInfo property) => FromProperty(property);

#endregion Conversion Operators

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

public override int GetHashCode()
{
unchecked
{
var hashCode = _comparisonValue?.GetHashCode() ?? 0;
hashCode = (hashCode * 397) ^ (_type?.GetHashCode() ?? 0);
return hashCode;
}
}
public override int GetHashCode() => HashCode.Combine(_comparisonValue, _type);

#endregion Equality

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

#endregion Debugging

[return: NotNullIfNotNull(nameof(name))]
private static string? ParseFieldName(string? name, out double? boost)
private static string ParseFieldName(string name, out double? boost)
{
boost = null;
if (name is null)
return null;

var caretIndex = name.IndexOf('^');
if (caretIndex == -1)
#if NET || NETSTANDARD2_1_OR_GREATER
if (!name.Contains('^', StringComparison.Ordinal))
return name;
#else
if (name.IndexOf('^') == -1)
return name;
#endif

var parts = name.Split(new[] { '^' }, 2, StringSplitOptions.RemoveEmptyEntries);
#if NET || NETSTANDARD2_1_OR_GREATER
var parts = name.Split('^', 2, StringSplitOptions.RemoveEmptyEntries);
#else
var parts = name.Split(BoostSplit, 2, StringSplitOptions.RemoveEmptyEntries);
#endif
name = parts[0];
boost = double.Parse(parts[1], CultureInfo.InvariantCulture);

if (string.IsNullOrWhiteSpace(name))
throw new ArgumentException("Field name can not be null or empty.", nameof(name));

return name;
}
}
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.