Skip to content

[Backport 8.1] Responding to latest spec fixes #6492

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
Jun 29, 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
21 changes: 17 additions & 4 deletions src/Elastic.Clients.Elasticsearch/Common/Infer/Field/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Elastic.Clients.Elasticsearch;

[JsonConverter(typeof(FieldConverter))]
[DebuggerDisplay("{" + nameof(DebugDisplay) + ",nq}")]
public sealed class Field : IEquatable<Field>, IUrlParameter
public sealed class Field : IEquatable<Field>, IUrlParameter, IDictionaryKey
{
private readonly object _comparisonValue;
private readonly Type _type;
Expand Down Expand Up @@ -95,15 +95,26 @@ public bool Equals(Field other) => _type != null
? other != null && _type == other._type && _comparisonValue.Equals(other._comparisonValue)
: other != null && _comparisonValue.Equals(other._comparisonValue);

string IUrlParameter.GetString(ITransportConfiguration? settings)
string IUrlParameter.GetString(ITransportConfiguration settings)
{
if (!(settings is IElasticsearchClientSettings ElasticsearchSettings))
if (settings is not IElasticsearchClientSettings elasticsearchSettings)
{
throw new ArgumentNullException(nameof(settings),
$"Can not resolve {nameof(Field)} if no {nameof(IElasticsearchClientSettings)} is provided");
}

return ElasticsearchSettings.Inferrer.Field(this);
return GetStringCore(elasticsearchSettings);
}

private string GetStringCore(IElasticsearchClientSettings settings)
{
if (settings is null)
{
throw new ArgumentNullException(nameof(settings),
$"Can not resolve {nameof(Field)} if no {nameof(IElasticsearchClientSettings)} is provided");
}

return settings.Inferrer.Field(this);
}

public override string ToString() => DebugDisplay;
Expand Down Expand Up @@ -172,6 +183,8 @@ 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
28 changes: 14 additions & 14 deletions src/Elastic.Clients.Elasticsearch/Serialization/UnionConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public override JsonConverter CreateConverter(
}

private class DerivedUnionConverterInner<TType, TItem1, TItem2> : JsonConverter<TType>
where TType : Union<TItem1, TItem2>
{
public override TType? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
Expand All @@ -72,7 +73,7 @@ private class DerivedUnionConverterInner<TType, TItem1, TItem2> : JsonConverter<
{
var itemOne = JsonSerializer.Deserialize<TItem1>(ref readerCopy, options);

if (itemOne is TItem1)
if (itemOne is not null)
{
reader = readerCopy;
return (TType)Activator.CreateInstance(typeof(TType), itemOne);
Expand All @@ -87,7 +88,7 @@ private class DerivedUnionConverterInner<TType, TItem1, TItem2> : JsonConverter<
{
var itemTwo = JsonSerializer.Deserialize<TItem2>(ref reader, options);

if (itemTwo is TItem2)
if (itemTwo is not null)
{
return (TType)Activator.CreateInstance(typeof(TType), itemTwo);
}
Expand All @@ -109,20 +110,19 @@ public override void Write(Utf8JsonWriter writer, TType value,
return;
}

//if (value.Item1 is not null)
//{
// JsonSerializer.Serialize(writer, value.Item1, value.Item1.GetType(), options);
// return;
//}
if (value.Item1 is not null)
{
JsonSerializer.Serialize(writer, value.Item1, value.Item1.GetType(), options);
return;
}

//if (value.Item2 is not null)
//{
// JsonSerializer.Serialize(writer, value.Item2, value.Item2.GetType(), options);
// return;
//}
if (value.Item2 is not null)
{
JsonSerializer.Serialize(writer, value.Item2, value.Item2.GetType(), options);
return;
}

throw new JsonException("TODO");
//throw new JsonException("Invalid union type.");
throw new JsonException("Invalid union type.");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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.

namespace Elastic.Clients.Elasticsearch.Aggregations;

public partial class AggregateOrder
{
public static AggregateOrder KeyDescending => new(new System.Collections.Generic.Dictionary<Field, SortOrder>
{
{ "_key", SortOrder.Desc }
});

public static AggregateOrder KeyAscending => new(new System.Collections.Generic.Dictionary<Field, SortOrder>
{
{ "_key", SortOrder.Asc }
});

public static AggregateOrder CountDescending => new(new System.Collections.Generic.Dictionary<Field, SortOrder>
{
{ "_count", SortOrder.Desc }
});

public static AggregateOrder CountAscending => new(new System.Collections.Generic.Dictionary<Field, SortOrder>
{
{ "_count", SortOrder.Asc }
});
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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.
//
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

#nullable restore
namespace Elastic.Clients.Elasticsearch.Aggregations
{
public partial class AggregateOrder : Union<Dictionary<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>?, IReadOnlyCollection<Dictionary<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>?>
{
public AggregateOrder(Dictionary<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>? item) : base(item)
{
}

public AggregateOrder(IReadOnlyCollection<Dictionary<Elastic.Clients.Elasticsearch.Field, Elastic.Clients.Elasticsearch.SortOrder>>? item) : base(item)
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public override DateHistogramAggregation Read(ref Utf8JsonReader reader, Type ty

if (reader.ValueTextEquals("order"))
{
var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Aggregations.HistogramOrder?>(ref reader, options);
var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Aggregations.AggregateOrder?>(ref reader, options);
if (value is not null)
{
agg.Order = value;
Expand Down Expand Up @@ -319,7 +319,7 @@ public DateHistogramAggregation(string name) : base(name)

[JsonInclude]
[JsonPropertyName("order")]
public Elastic.Clients.Elasticsearch.Aggregations.HistogramOrder? Order { get; set; }
public Elastic.Clients.Elasticsearch.Aggregations.AggregateOrder? Order { get; set; }

[JsonInclude]
[JsonPropertyName("params")]
Expand Down Expand Up @@ -369,11 +369,7 @@ public DateHistogramAggregationDescriptor() : base()

private Elastic.Clients.Elasticsearch.Duration? OffsetValue { get; set; }

private Elastic.Clients.Elasticsearch.Aggregations.HistogramOrder? OrderValue { get; set; }

private HistogramOrderDescriptor OrderDescriptor { get; set; }

private Action<HistogramOrderDescriptor> OrderDescriptorAction { get; set; }
private Elastic.Clients.Elasticsearch.Aggregations.AggregateOrder? OrderValue { get; set; }

private Dictionary<string, object>? ParamsValue { get; set; }

Expand Down Expand Up @@ -481,30 +477,12 @@ public DateHistogramAggregationDescriptor<TDocument> Offset(Elastic.Clients.Elas
return Self;
}

public DateHistogramAggregationDescriptor<TDocument> Order(Elastic.Clients.Elasticsearch.Aggregations.HistogramOrder? order)
public DateHistogramAggregationDescriptor<TDocument> Order(Elastic.Clients.Elasticsearch.Aggregations.AggregateOrder? order)
{
OrderDescriptor = null;
OrderDescriptorAction = null;
OrderValue = order;
return Self;
}

public DateHistogramAggregationDescriptor<TDocument> Order(HistogramOrderDescriptor descriptor)
{
OrderValue = null;
OrderDescriptorAction = null;
OrderDescriptor = descriptor;
return Self;
}

public DateHistogramAggregationDescriptor<TDocument> Order(Action<HistogramOrderDescriptor> configure)
{
OrderValue = null;
OrderDescriptor = null;
OrderDescriptorAction = configure;
return Self;
}

public DateHistogramAggregationDescriptor<TDocument> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
{
ParamsValue = selector?.Invoke(new FluentDictionary<string, object>());
Expand Down Expand Up @@ -580,17 +558,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
JsonSerializer.Serialize(writer, OffsetValue, options);
}

if (OrderDescriptor is not null)
{
writer.WritePropertyName("order");
JsonSerializer.Serialize(writer, OrderDescriptor, options);
}
else if (OrderDescriptorAction is not null)
{
writer.WritePropertyName("order");
JsonSerializer.Serialize(writer, new HistogramOrderDescriptor(OrderDescriptorAction), options);
}
else if (OrderValue is not null)
if (OrderValue is not null)
{
writer.WritePropertyName("order");
JsonSerializer.Serialize(writer, OrderValue, options);
Expand Down Expand Up @@ -670,11 +638,7 @@ public DateHistogramAggregationDescriptor() : base()

private Elastic.Clients.Elasticsearch.Duration? OffsetValue { get; set; }

private Elastic.Clients.Elasticsearch.Aggregations.HistogramOrder? OrderValue { get; set; }

private HistogramOrderDescriptor OrderDescriptor { get; set; }

private Action<HistogramOrderDescriptor> OrderDescriptorAction { get; set; }
private Elastic.Clients.Elasticsearch.Aggregations.AggregateOrder? OrderValue { get; set; }

private Dictionary<string, object>? ParamsValue { get; set; }

Expand Down Expand Up @@ -788,30 +752,12 @@ public DateHistogramAggregationDescriptor Offset(Elastic.Clients.Elasticsearch.D
return Self;
}

public DateHistogramAggregationDescriptor Order(Elastic.Clients.Elasticsearch.Aggregations.HistogramOrder? order)
public DateHistogramAggregationDescriptor Order(Elastic.Clients.Elasticsearch.Aggregations.AggregateOrder? order)
{
OrderDescriptor = null;
OrderDescriptorAction = null;
OrderValue = order;
return Self;
}

public DateHistogramAggregationDescriptor Order(HistogramOrderDescriptor descriptor)
{
OrderValue = null;
OrderDescriptorAction = null;
OrderDescriptor = descriptor;
return Self;
}

public DateHistogramAggregationDescriptor Order(Action<HistogramOrderDescriptor> configure)
{
OrderValue = null;
OrderDescriptor = null;
OrderDescriptorAction = configure;
return Self;
}

public DateHistogramAggregationDescriptor Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> selector)
{
ParamsValue = selector?.Invoke(new FluentDictionary<string, object>());
Expand Down Expand Up @@ -887,17 +833,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
JsonSerializer.Serialize(writer, OffsetValue, options);
}

if (OrderDescriptor is not null)
{
writer.WritePropertyName("order");
JsonSerializer.Serialize(writer, OrderDescriptor, options);
}
else if (OrderDescriptorAction is not null)
{
writer.WritePropertyName("order");
JsonSerializer.Serialize(writer, new HistogramOrderDescriptor(OrderDescriptorAction), options);
}
else if (OrderValue is not null)
if (OrderValue is not null)
{
writer.WritePropertyName("order");
JsonSerializer.Serialize(writer, OrderValue, options);
Expand Down
Loading