Skip to content

[Backport 8.2] Implement single or many types such as AggregateOrder #6500

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

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
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
14 changes: 0 additions & 14 deletions src/Elastic.Clients.Elasticsearch/Common/DateTimeUtil.cs

This file was deleted.

11 changes: 8 additions & 3 deletions src/Elastic.Clients.Elasticsearch/Common/Infer/Field/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ namespace Elastic.Clients.Elasticsearch;

[JsonConverter(typeof(FieldConverter))]
[DebuggerDisplay("{" + nameof(DebugDisplay) + ",nq}")]
public sealed class Field : IEquatable<Field>, IUrlParameter, IDictionaryKey
public sealed class Field : IEquatable<Field>, IUrlParameter
{
private readonly object _comparisonValue;
private readonly Type _type;

// Pseudo and metadata fields

public static Field IdField = new("_id");
public static Field ScoreField = new("_score");
public static Field KeyField = new("_key");
public static Field CountField = new("_count");

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

public Field(string name, double boost) : this(name, boost, null) { }
Expand Down Expand Up @@ -183,8 +190,6 @@ public override bool Equals(object obj)
}
}

string IDictionaryKey.Key(IElasticsearchClientSettings settings) => GetStringCore(settings);

public static bool operator ==(Field x, Field y) => Equals(x, y);

public static bool operator !=(Field x, Field y) => !Equals(x, y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Elastic.Transport;

namespace Elastic.Clients.Elasticsearch;

Expand All @@ -14,6 +15,10 @@ internal sealed class FieldConverter : JsonConverter<Field>

public FieldConverter(IElasticsearchClientSettings settings) => _settings = settings;

public override void WriteAsPropertyName(Utf8JsonWriter writer, Field value, JsonSerializerOptions options) => writer.WritePropertyName(((IUrlParameter)value).GetString(_settings));

public override Field ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetString();

public override Field? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
switch (reader.TokenType)
Expand Down
35 changes: 0 additions & 35 deletions src/Elastic.Clients.Elasticsearch/Common/Infer/Id/Id.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,39 +109,4 @@ public override int GetHashCode()

public static bool operator !=(Id left, Id right) => !Equals(left, right);
}

internal sealed class IdConverter : JsonConverter<Id>
{
private readonly IElasticsearchClientSettings _settings;

public IdConverter(IElasticsearchClientSettings settings) => _settings = settings;

public override Id? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType == JsonTokenType.Number
? new Id(reader.GetInt64())
: new Id(reader.GetString());

public override void Write(Utf8JsonWriter writer, Id value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}

if (value.Document is not null)
{
var documentId = _settings.Inferrer.Id(value.Document.GetType(), value.Document);
writer.WriteStringValue(documentId);
}
else if (value.LongValue.HasValue)
{
writer.WriteNumberValue(value.LongValue.Value);
}
else
{
writer.WriteStringValue(value.StringValue);
}
}
}
}
50 changes: 50 additions & 0 deletions src/Elastic.Clients.Elasticsearch/Common/Infer/Id/IdConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Elastic.Transport;

namespace Elastic.Clients.Elasticsearch
{
internal sealed class IdConverter : JsonConverter<Id>
{
private readonly IElasticsearchClientSettings _settings;

public IdConverter(IElasticsearchClientSettings settings) => _settings = settings;

public override void WriteAsPropertyName(Utf8JsonWriter writer, Id value, JsonSerializerOptions options) => writer.WritePropertyName(((IUrlParameter)value).GetString(_settings));

public override Id ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetString();

public override Id? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
reader.TokenType == JsonTokenType.Number
? new Id(reader.GetInt64())
: new Id(reader.GetString());

public override void Write(Utf8JsonWriter writer, Id value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}

if (value.Document is not null)
{
var documentId = _settings.Inferrer.Id(value.Document.GetType(), value.Document);
writer.WriteStringValue(documentId);
}
else if (value.LongValue.HasValue)
{
writer.WriteNumberValue(value.LongValue.Value);
}
else
{
writer.WriteStringValue(value.StringValue);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Elastic.Transport;

namespace Elastic.Clients.Elasticsearch
{
Expand All @@ -17,6 +18,10 @@ internal class IndexNameConverter : JsonConverter<IndexName?>

public IndexNameConverter(IElasticsearchClientSettings settings) => _settings = settings;

public override void WriteAsPropertyName(Utf8JsonWriter writer, IndexName value, JsonSerializerOptions options) => writer.WritePropertyName(((IUrlParameter)value).GetString(_settings));

public override IndexName ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetString();

public override IndexName? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Elastic.Clients.Elasticsearch
{
[DebuggerDisplay("{" + nameof(DebugDisplay) + ",nq}")]
[JsonConverter(typeof(PropertyNameConverter))]
public sealed class PropertyName : IEquatable<PropertyName>, IUrlParameter, IDictionaryKey
public sealed class PropertyName : IEquatable<PropertyName>, IUrlParameter
{
private readonly object _comparisonValue;
private readonly Type _type;
Expand Down Expand Up @@ -51,8 +51,6 @@ public PropertyName(PropertyInfo property)
private string PropertyDebug => Property == null ? null : $"PropertyInfo: {Property.Name}";
private static int TypeHashCode { get; } = typeof(PropertyName).GetHashCode();

string IDictionaryKey.Key(IElasticsearchClientSettings settings) => GetInferredString(settings);

public bool Equals(PropertyName other) => EqualsMarker(other);

string IUrlParameter.GetString(ITransportConfiguration? settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,6 @@

namespace Elastic.Clients.Elasticsearch
{
//public readonly partial struct PropertyName : IDictionaryKey
//{
// public string Key => Value;
//}

//// This is an incomplete stub implementation and should really be a struct
//public partial class Indices : IUrlParameter
//{
// public static readonly Indices All = new("_all");

// internal Indices(IndexName index) => _indexNameList.Add(index);

// public Indices(IEnumerable<IndexName> indices)
// {
// indices.ThrowIfEmpty(nameof(indices));
// _indexNameList.AddRange(indices);
// }

// public Indices(IEnumerable<string> indices)
// {
// indices.ThrowIfEmpty(nameof(indices));
// _indexNameList.AddRange(indices.Select(s => (IndexName)s));
// }

// public IReadOnlyCollection<IndexName> Values => _indexNameList.ToArray();

// public static Indices Parse(string names) => names.IsNullOrEmptyCommaSeparatedList(out var list) ? null : new Indices(list);

// public static Indices Single(string index) => new Indices((IndexName)index);

// public static implicit operator Indices(string names) => Parse(names);

// string IUrlParameter.GetString(ITransportConfiguration? settings)
// {
// if (settings is not IElasticsearchClientSettings elasticsearchClientSettings)
// throw new Exception(
// "Tried to pass index names on query sting but it could not be resolved because no Elastic.Clients.Elasticsearch settings are available.");

// var indices = _indexNameList.Select(i => i.GetString(settings)).Distinct();

// return string.Join(",", indices);
// }
//}

//public partial struct IndicesList : IUrlParameter
//{
// //public static readonly IndicesList All = new("_all");

// private readonly List<IndexName> _indices = new();

// internal IndicesList(IndexName index) => _indices.Add(index);

// public IndicesList(IEnumerable<IndexName> indices)
// {
// indices.ThrowIfEmpty(nameof(indices));

// // De-duplicating during creation avoids cost when accessing the values.
// foreach (var index in indices)
// if (!_indices.Contains(index))
// _indices.Add(index);
// }

// public IndicesList(string[] indices)
// {
// indices.ThrowIfEmpty(nameof(indices));

// foreach (var index in indices)
// if (!_indices.Contains(index))
// _indices.Add(index);
// }

// public IReadOnlyCollection<IndexName> Values => _indices;

// public static IndicesList Parse(string names) => names.IsNullOrEmptyCommaSeparatedList(out var list) ? null : new IndicesList(list);

// public static implicit operator IndicesList(string names) => Parse(names);
//}

//public partial struct IndicesList { string IUrlParameter.GetString(ITransportConfiguration? settings) => ""; }


public abstract partial class PlainRequestBase<TParameters>
{
///<summary>Include the stack trace of returned errors.</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
IncludeFields = true,
Converters =
{
new KeyValuePairConverterFactory(settings),
new SourceConverterFactory(settings),
new ReadOnlyIndexNameDictionaryConverterFactory(settings),
new CalendarIntervalConverter(),
Expand All @@ -55,11 +56,11 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
new SelfDeserializableConverterFactory(settings),
new SelfTwoWaySerializableConverterFactory(settings),
new IndicesJsonConverter(settings),
new DictionaryConverter(settings),
new PropertyNameConverter(settings),
new IsADictionaryConverter(),
new ResponseItemConverterFactory(),
new UnionConverter()
new UnionConverter(),
new SingleOrManyConverterFactory(),
},
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public DefaultSourceSerializer(IElasticsearchClientSettings settings, JsonSerial
Converters =
{
new JsonStringEnumConverter(),
new DictionaryConverter(settings),
new UnionConverter(),
new IdConverter(settings),
new RelationNameConverter(settings),
Expand Down
Loading