Skip to content

Commit 47500c0

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Implement single or many types such as AggregateOrder (#6497)
* Generating full IList implementation for simplified arrays * Dictionary key JSON converters * Complete object initializer for SingleOrMany types
1 parent aae7aa7 commit 47500c0

32 files changed

+587
-449
lines changed

src/Elastic.Clients.Elasticsearch/Common/DateTimeUtil.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/Elastic.Clients.Elasticsearch/Common/Infer/Field/Field.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,18 @@ namespace Elastic.Clients.Elasticsearch;
1414

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

22+
// Pseudo and metadata fields
23+
24+
public static Field IdField = new("_id");
25+
public static Field ScoreField = new("_score");
26+
public static Field KeyField = new("_key");
27+
public static Field CountField = new("_count");
28+
2229
public Field(string name) : this(name, null, null) { }
2330

2431
public Field(string name, double boost) : this(name, boost, null) { }
@@ -183,8 +190,6 @@ public override bool Equals(object obj)
183190
}
184191
}
185192

186-
string IDictionaryKey.Key(IElasticsearchClientSettings settings) => GetStringCore(settings);
187-
188193
public static bool operator ==(Field x, Field y) => Equals(x, y);
189194

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

src/Elastic.Clients.Elasticsearch/Common/Infer/Field/FieldConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Text.Json;
77
using System.Text.Json.Serialization;
8+
using Elastic.Transport;
89

910
namespace Elastic.Clients.Elasticsearch;
1011

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

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

18+
public override void WriteAsPropertyName(Utf8JsonWriter writer, Field value, JsonSerializerOptions options) => writer.WritePropertyName(((IUrlParameter)value).GetString(_settings));
19+
20+
public override Field ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetString();
21+
1722
public override Field? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
1823
{
1924
switch (reader.TokenType)

src/Elastic.Clients.Elasticsearch/Common/Infer/Id/Id.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -109,39 +109,4 @@ public override int GetHashCode()
109109

110110
public static bool operator !=(Id left, Id right) => !Equals(left, right);
111111
}
112-
113-
internal sealed class IdConverter : JsonConverter<Id>
114-
{
115-
private readonly IElasticsearchClientSettings _settings;
116-
117-
public IdConverter(IElasticsearchClientSettings settings) => _settings = settings;
118-
119-
public override Id? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
120-
reader.TokenType == JsonTokenType.Number
121-
? new Id(reader.GetInt64())
122-
: new Id(reader.GetString());
123-
124-
public override void Write(Utf8JsonWriter writer, Id value, JsonSerializerOptions options)
125-
{
126-
if (value is null)
127-
{
128-
writer.WriteNullValue();
129-
return;
130-
}
131-
132-
if (value.Document is not null)
133-
{
134-
var documentId = _settings.Inferrer.Id(value.Document.GetType(), value.Document);
135-
writer.WriteStringValue(documentId);
136-
}
137-
else if (value.LongValue.HasValue)
138-
{
139-
writer.WriteNumberValue(value.LongValue.Value);
140-
}
141-
else
142-
{
143-
writer.WriteStringValue(value.StringValue);
144-
}
145-
}
146-
}
147112
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Text.Json;
7+
using System.Text.Json.Serialization;
8+
using Elastic.Transport;
9+
10+
namespace Elastic.Clients.Elasticsearch
11+
{
12+
internal sealed class IdConverter : JsonConverter<Id>
13+
{
14+
private readonly IElasticsearchClientSettings _settings;
15+
16+
public IdConverter(IElasticsearchClientSettings settings) => _settings = settings;
17+
18+
public override void WriteAsPropertyName(Utf8JsonWriter writer, Id value, JsonSerializerOptions options) => writer.WritePropertyName(((IUrlParameter)value).GetString(_settings));
19+
20+
public override Id ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetString();
21+
22+
public override Id? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
23+
reader.TokenType == JsonTokenType.Number
24+
? new Id(reader.GetInt64())
25+
: new Id(reader.GetString());
26+
27+
public override void Write(Utf8JsonWriter writer, Id value, JsonSerializerOptions options)
28+
{
29+
if (value is null)
30+
{
31+
writer.WriteNullValue();
32+
return;
33+
}
34+
35+
if (value.Document is not null)
36+
{
37+
var documentId = _settings.Inferrer.Id(value.Document.GetType(), value.Document);
38+
writer.WriteStringValue(documentId);
39+
}
40+
else if (value.LongValue.HasValue)
41+
{
42+
writer.WriteNumberValue(value.LongValue.Value);
43+
}
44+
else
45+
{
46+
writer.WriteStringValue(value.StringValue);
47+
}
48+
}
49+
}
50+
}

src/Elastic.Clients.Elasticsearch/Common/Infer/IndexName/IndexNameConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Text.Json;
77
using System.Text.Json.Serialization;
8+
using Elastic.Transport;
89

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

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

21+
public override void WriteAsPropertyName(Utf8JsonWriter writer, IndexName value, JsonSerializerOptions options) => writer.WritePropertyName(((IUrlParameter)value).GetString(_settings));
22+
23+
public override IndexName ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => reader.GetString();
24+
2025
public override IndexName? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
2126
{
2227
if (reader.TokenType != JsonTokenType.String)

src/Elastic.Clients.Elasticsearch/Common/Infer/PropertyName/PropertyName.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Elastic.Clients.Elasticsearch
1313
{
1414
[DebuggerDisplay("{" + nameof(DebugDisplay) + ",nq}")]
1515
[JsonConverter(typeof(PropertyNameConverter))]
16-
public sealed class PropertyName : IEquatable<PropertyName>, IUrlParameter, IDictionaryKey
16+
public sealed class PropertyName : IEquatable<PropertyName>, IUrlParameter
1717
{
1818
private readonly object _comparisonValue;
1919
private readonly Type _type;
@@ -51,8 +51,6 @@ public PropertyName(PropertyInfo property)
5151
private string PropertyDebug => Property == null ? null : $"PropertyInfo: {Property.Name}";
5252
private static int TypeHashCode { get; } = typeof(PropertyName).GetHashCode();
5353

54-
string IDictionaryKey.Key(IElasticsearchClientSettings settings) => GetInferredString(settings);
55-
5654
public bool Equals(PropertyName other) => EqualsMarker(other);
5755

5856
string IUrlParameter.GetString(ITransportConfiguration? settings)

src/Elastic.Clients.Elasticsearch/Common/Request/PlainRequestBase.cs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,6 @@
77

88
namespace Elastic.Clients.Elasticsearch
99
{
10-
//public readonly partial struct PropertyName : IDictionaryKey
11-
//{
12-
// public string Key => Value;
13-
//}
14-
15-
//// This is an incomplete stub implementation and should really be a struct
16-
//public partial class Indices : IUrlParameter
17-
//{
18-
// public static readonly Indices All = new("_all");
19-
20-
// internal Indices(IndexName index) => _indexNameList.Add(index);
21-
22-
// public Indices(IEnumerable<IndexName> indices)
23-
// {
24-
// indices.ThrowIfEmpty(nameof(indices));
25-
// _indexNameList.AddRange(indices);
26-
// }
27-
28-
// public Indices(IEnumerable<string> indices)
29-
// {
30-
// indices.ThrowIfEmpty(nameof(indices));
31-
// _indexNameList.AddRange(indices.Select(s => (IndexName)s));
32-
// }
33-
34-
// public IReadOnlyCollection<IndexName> Values => _indexNameList.ToArray();
35-
36-
// public static Indices Parse(string names) => names.IsNullOrEmptyCommaSeparatedList(out var list) ? null : new Indices(list);
37-
38-
// public static Indices Single(string index) => new Indices((IndexName)index);
39-
40-
// public static implicit operator Indices(string names) => Parse(names);
41-
42-
// string IUrlParameter.GetString(ITransportConfiguration? settings)
43-
// {
44-
// if (settings is not IElasticsearchClientSettings elasticsearchClientSettings)
45-
// throw new Exception(
46-
// "Tried to pass index names on query sting but it could not be resolved because no Elastic.Clients.Elasticsearch settings are available.");
47-
48-
// var indices = _indexNameList.Select(i => i.GetString(settings)).Distinct();
49-
50-
// return string.Join(",", indices);
51-
// }
52-
//}
53-
54-
//public partial struct IndicesList : IUrlParameter
55-
//{
56-
// //public static readonly IndicesList All = new("_all");
57-
58-
// private readonly List<IndexName> _indices = new();
59-
60-
// internal IndicesList(IndexName index) => _indices.Add(index);
61-
62-
// public IndicesList(IEnumerable<IndexName> indices)
63-
// {
64-
// indices.ThrowIfEmpty(nameof(indices));
65-
66-
// // De-duplicating during creation avoids cost when accessing the values.
67-
// foreach (var index in indices)
68-
// if (!_indices.Contains(index))
69-
// _indices.Add(index);
70-
// }
71-
72-
// public IndicesList(string[] indices)
73-
// {
74-
// indices.ThrowIfEmpty(nameof(indices));
75-
76-
// foreach (var index in indices)
77-
// if (!_indices.Contains(index))
78-
// _indices.Add(index);
79-
// }
80-
81-
// public IReadOnlyCollection<IndexName> Values => _indices;
82-
83-
// public static IndicesList Parse(string names) => names.IsNullOrEmptyCommaSeparatedList(out var list) ? null : new IndicesList(list);
84-
85-
// public static implicit operator IndicesList(string names) => Parse(names);
86-
//}
87-
88-
//public partial struct IndicesList { string IUrlParameter.GetString(ITransportConfiguration? settings) => ""; }
89-
90-
9110
public abstract partial class PlainRequestBase<TParameters>
9211
{
9312
///<summary>Include the stack trace of returned errors.</summary>

src/Elastic.Clients.Elasticsearch/Serialization/DefaultRequestResponseSerializer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
3737
IncludeFields = true,
3838
Converters =
3939
{
40+
new KeyValuePairConverterFactory(settings),
4041
new SourceConverterFactory(settings),
4142
new ReadOnlyIndexNameDictionaryConverterFactory(settings),
4243
new CalendarIntervalConverter(),
@@ -55,11 +56,11 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
5556
new SelfDeserializableConverterFactory(settings),
5657
new SelfTwoWaySerializableConverterFactory(settings),
5758
new IndicesJsonConverter(settings),
58-
new DictionaryConverter(settings),
5959
new PropertyNameConverter(settings),
6060
new IsADictionaryConverter(),
6161
new ResponseItemConverterFactory(),
62-
new UnionConverter()
62+
new UnionConverter(),
63+
new SingleOrManyConverterFactory(),
6364
},
6465
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
6566
};

src/Elastic.Clients.Elasticsearch/Serialization/DefaultSourceSerializer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public DefaultSourceSerializer(IElasticsearchClientSettings settings, JsonSerial
1616
Converters =
1717
{
1818
new JsonStringEnumConverter(),
19-
new DictionaryConverter(settings),
2019
new UnionConverter(),
2120
new IdConverter(settings),
2221
new RelationNameConverter(settings),

0 commit comments

Comments
 (0)