Skip to content

Commit cca12b1

Browse files
Regenerate and refactoring to improve descriptor APIs (#6157) (#6158)
Co-authored-by: Steve Gordon <[email protected]>
1 parent 54141f0 commit cca12b1

File tree

222 files changed

+24586
-4955
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

222 files changed

+24586
-4955
lines changed

build/scripts/scripts.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<Content Include="..\..\.github\workflows\make-release-notes.yml"><Link>make-release-notes.yml</Link></Content>
3535
</ItemGroup>
3636
<ItemGroup>
37+
<PackageReference Include="FSharp.Core" Version="6.0.1" />
3738
<PackageReference Include="Bullseye" Version="3.3.0" />
3839
<PackageReference Include="Elastic.Elasticsearch.Managed" Version="0.3.0" />
3940

src/Elastic.Clients.Elasticsearch/Api/GetAsyncSearchResponse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public abstract partial class AsyncSearchResponseBase
3131
public DateTimeOffset ExpirationTime => ExpirationTimeInMillis.DateTimeOffset;
3232
}
3333

34-
public sealed partial class AsyncSearchSubmitRequestDescriptor<TDocument>
34+
public sealed partial class AsyncSearchSubmitRequestDescriptor
3535
{
36-
public AsyncSearchSubmitRequestDescriptor<TDocument> MatchAll(Action<MatchAllQueryDescriptor>? selector = null) => selector is null ? Query(q => q.MatchAll()) : Query(q => q.MatchAll(selector));
36+
public AsyncSearchSubmitRequestDescriptor MatchAll(Action<MatchAllQueryDescriptor>? selector = null) => selector is null ? Query(q => q.MatchAll()) : Query(q => q.MatchAll(selector));
3737
}

src/Elastic.Clients.Elasticsearch/Api/IndexRequest.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,20 @@ internal static HttpMethod GetHttpMethod(IndexRequest<TDocument> request) =>
3333
public sealed partial class IndexRequestDescriptor<TDocument> : ICustomJsonWriter
3434
{
3535
// TODO: Codegen
36-
public IndexRequestDescriptor(TDocument documentWithId, IndexName index = null, Id id = null) : this(index ?? typeof(TDocument), id ?? Elasticsearch.Id.From(documentWithId)) => DocumentFromPath(documentWithId);
37-
38-
// ??
39-
private void DocumentFromPath(TDocument document) => Assign(document, (a, v) => a.DocumentValue = v);
40-
41-
// TODO: Codegen
42-
// Perhaps not required as this should always be set via ctors
43-
public IndexRequestDescriptor<TDocument> Index(IndexName index) => Assign(index, (a, v) => a.RouteValues.Required("index", v));
36+
public IndexRequestDescriptor(TDocument documentWithId, IndexName index = null, Id id = null) : this(index ?? typeof(TDocument), id ?? Elasticsearch.Id.From(documentWithId)) => DocumentValue = documentWithId;
37+
4438

4539
// TODO: Codegen
46-
public IndexRequestDescriptor<TDocument> Document(TDocument document) => Assign(document, (a, v) => a.DocumentValue = v);
40+
public IndexRequestDescriptor<TDocument> Document(TDocument document)
41+
{
42+
DocumentValue = document;
43+
return Self;
44+
}
4745

4846
internal Id _id;
4947

5048
public void WriteJson(Utf8JsonWriter writer, Serializer sourceSerializer) => SourceSerialisation.Serialize(DocumentValue, writer, sourceSerializer);
5149

52-
// TODO: Codegen for optional params
53-
public IndexRequestDescriptor<TDocument> Id(Id id) => Assign(id, (a, v) => a.RouteValues.Optional("id", v));
54-
5550
protected override HttpMethod? DynamicHttpMethod => _id is not null || RouteValues.ContainsId ? HttpMethod.PUT : HttpMethod.POST;
5651
}
5752
}

src/Elastic.Clients.Elasticsearch/Common/Fluent/DescriptorBase.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public abstract class DescriptorBase<TDescriptor> : IDescriptor, ISelfSerializab
5454
{
5555
private readonly TDescriptor _self;
5656

57-
protected DescriptorBase() => _self = (TDescriptor)this;
57+
internal DescriptorBase() => _self = (TDescriptor)this;
5858

5959
[IgnoreDataMember]
6060
protected TDescriptor Self => _self;
@@ -92,35 +92,35 @@ public abstract class DescriptorBase<TDescriptor> : IDescriptor, ISelfSerializab
9292
void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings) => Serialize(writer, options, settings);
9393
}
9494

95-
public abstract class QueryDescriptorBase<TDescriptor> : DescriptorBase<TDescriptor>, IQuery
96-
where TDescriptor : QueryDescriptorBase<TDescriptor>, IQuery
97-
{
98-
internal string _name;
95+
//public abstract class QueryDescriptorBase<TDescriptor> : DescriptorBase<TDescriptor>, IQuery
96+
// where TDescriptor : QueryDescriptorBase<TDescriptor>, IQuery
97+
//{
98+
// internal string _name;
9999

100-
///// <inheritdoc cref="IQuery.Conditionless"/>
101-
//protected abstract bool Conditionless { get; }
100+
// ///// <inheritdoc cref="IQuery.Conditionless"/>
101+
// //protected abstract bool Conditionless { get; }
102102

103-
//double? IQuery.Boost { get; set; }
103+
// //double? IQuery.Boost { get; set; }
104104

105-
//bool IQuery.Conditionless => Conditionless;
105+
// //bool IQuery.Conditionless => Conditionless;
106106

107-
//bool IQuery.IsStrict { get; set; }
107+
// //bool IQuery.IsStrict { get; set; }
108108

109-
//bool IQuery.IsVerbatim { get; set; }
109+
// //bool IQuery.IsVerbatim { get; set; }
110110

111-
bool IQuery.IsWritable => true; /*Self.IsVerbatim || !Self.Conditionless;*/
111+
// bool IQuery.IsWritable => true; /*Self.IsVerbatim || !Self.Conditionless;*/
112112

113-
public TDescriptor Name(string name) => Assign(name, (a, v) => a._name = v);
113+
// public TDescriptor Name(string name) => Assign(name, (a, v) => a._name = v);
114114

115-
///// <inheritdoc cref="IQuery.Boost"/>
116-
//public TDescriptor Boost(double? boost) => Assign(boost, (a, v) => a.Boost = v);
115+
// ///// <inheritdoc cref="IQuery.Boost"/>
116+
// //public TDescriptor Boost(double? boost) => Assign(boost, (a, v) => a.Boost = v);
117117

118-
///// <inheritdoc cref="IQuery.IsVerbatim"/>
119-
//public TDescriptor Verbatim(bool verbatim = true) => Assign(verbatim, (a, v) => a.IsVerbatim = v);
118+
// ///// <inheritdoc cref="IQuery.IsVerbatim"/>
119+
// //public TDescriptor Verbatim(bool verbatim = true) => Assign(verbatim, (a, v) => a.IsVerbatim = v);
120120

121-
///// <inheritdoc cref="IQuery.IsStrict"/>
122-
//public TDescriptor Strict(bool strict = true) => Assign(strict, (a, v) => a.IsStrict = v);
123-
}
121+
// ///// <inheritdoc cref="IQuery.IsStrict"/>
122+
// //public TDescriptor Strict(bool strict = true) => Assign(strict, (a, v) => a.IsStrict = v);
123+
//}
124124

125125
//internal abstract class QueryDescriptorConverterBase<T> : JsonConverter<T> where T : QueryDescriptorBase<T>
126126
//{
@@ -143,18 +143,17 @@ public abstract class QueryDescriptorBase<TDescriptor> : DescriptorBase<TDescrip
143143
// internal abstract void WriteInternal(Utf8JsonWriter writer, T value, JsonSerializerOptions options);
144144
//}
145145

146-
public abstract class FieldNameQueryDescriptorBase<TDescriptor, T> : QueryDescriptorBase<TDescriptor>
147-
where TDescriptor : FieldNameQueryDescriptorBase<TDescriptor, T>
148-
{
149-
internal Field _field;
150-
151-
//bool IQuery.IsStrict { get; set; }
146+
//public abstract class FieldNameQueryDescriptorBase<TDescriptor> : QueryDescriptorBase<TDescriptor>
147+
// where TDescriptor : FieldNameQueryDescriptorBase<TDescriptor>
148+
//{
149+
// internal Field _field;
152150

151+
// //bool IQuery.IsStrict { get; set; }
153152

154-
//bool IQuery.IsVerbatim { get; set; }
153+
// //bool IQuery.IsVerbatim { get; set; }
155154

156-
public TDescriptor Field(Field field) => Assign(field, (a, v) => a._field = v);
155+
// public TDescriptor Field(Field field) => Assign(field, (a, v) => a._field = v);
157156

158-
public TDescriptor Field<TValue>(Expression<Func<TDescriptor, TValue>> objectPath) =>
159-
Assign(objectPath, (a, v) => a._field = v);
160-
}
157+
// public TDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> objectPath) =>
158+
// Assign(objectPath, (a, v) => a._field = v);
159+
//}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ namespace Elastic.Clients.Elasticsearch;
1010
internal static class Fluent
1111
{
1212
[MethodImpl(MethodImplOptions.AggressiveInlining)]
13-
internal static TDescriptor Assign<TDescriptor, TInterface, TValue>(TDescriptor self, TValue value, Action<TInterface, TValue> assign)
14-
where TDescriptor : class, TInterface
13+
internal static TDescriptor Assign<TDescriptor, TValue>(TDescriptor self, TValue value, Action<TDescriptor, TValue> assign)
14+
where TDescriptor : DescriptorBase<TDescriptor>
1515
{
1616
assign(self, value);
1717
return self;

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -155,25 +155,25 @@ public abstract partial class
155155

156156
void ISelfSerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings) => Serialize(writer, options, settings);
157157

158-
protected RequestDescriptorBase() => _descriptor = (TDescriptor)this;
158+
internal RequestDescriptorBase() => _descriptor = (TDescriptor)this;
159159

160160
protected abstract void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings);
161161

162-
protected RequestDescriptorBase(Func<RouteValues, RouteValues> pathSelector) : base(pathSelector) =>
162+
internal RequestDescriptorBase(Func<RouteValues, RouteValues> pathSelector) : base(pathSelector) =>
163163
_descriptor = (TDescriptor)this;
164164

165165
protected TDescriptor Self => _descriptor;
166166

167167
protected RouteValues RouteValues => ((IRequest<TParameters>)this).RouteValues;
168168

169-
protected TDescriptor Assign<TValue>(TValue value, Action<TDescriptor, TValue> assign) => Fluent.Assign(_descriptor, value, assign);
169+
//protected TDescriptor Assign<TValue>(TValue value, Action<TDescriptor, TValue> assign) => Fluent.Assign(_descriptor, value, assign);
170170

171-
protected TDescriptor InvokeAndAssign<T>(Action<T> configure, Action<TDescriptor, T> assign) where T : new()
172-
{
173-
var d = new T();
174-
configure(d);
175-
return Fluent.Assign(_descriptor, d, assign);
176-
}
171+
//protected TDescriptor InvokeAndAssign<T>(Action<T> configure, Action<TDescriptor, T> assign) where T : new()
172+
//{
173+
// var d = new T();
174+
// configure(d);
175+
// return Fluent.Assign(_descriptor, d, assign);
176+
//}
177177

178178
protected TDescriptor Qs(string name, object value)
179179
{

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ResolvedRouteValues(int size) : base(size) { }
1818
public class RouteValues : Dictionary<string, IUrlParameter>
1919
{
2020
/// <summary>
21-
/// Used specifically by index requests to determine whether to use PUT or POST.
21+
/// Used specifically by index requests to determine whether to use PUT or POST.
2222
/// </summary>
2323
internal bool ContainsId { get; private set; }
2424

@@ -41,10 +41,23 @@ internal ResolvedRouteValues Resolve(IElasticsearchClientSettings configuration)
4141
return resolved;
4242
}
4343

44-
private RouteValues Route(string name, IUrlParameter routeValue)
44+
private RouteValues Route(string name, IUrlParameter? routeValue, bool required = true)
4545
{
4646
switch (routeValue)
4747
{
48+
case null when !required:
49+
{
50+
if (!ContainsKey(name))
51+
return this;
52+
Remove(name);
53+
if (IsId(name))
54+
ContainsId = false; // invalidate cache
55+
return this;
56+
}
57+
58+
case null:
59+
throw new ArgumentNullException(name, $"{name} is required to build a URL to this API.");
60+
4861
default:
4962
this[name] = routeValue;
5063
if (IsId(name))
@@ -55,13 +68,9 @@ private RouteValues Route(string name, IUrlParameter routeValue)
5568

5669
private static bool IsId(string key) => key.Equals("id", StringComparison.OrdinalIgnoreCase);
5770

58-
internal RouteValues Required(string route, IUrlParameter value) => Route(route, value);
59-
60-
internal RouteValues Optional(string route, IUrlParameter value) => Route(route, value);
61-
62-
//internal RouteValues Optional(string route, Metrics value) => Route(route, value, false);
71+
internal RouteValues Required(string route, IUrlParameter? value) => Route(route, value);
6372

64-
//internal RouteValues Optional(string route, IndexMetrics value) => Route(route, value, false);
73+
internal RouteValues Optional(string route, IUrlParameter? value) => Route(route, value, false);
6574

6675
internal TActual Get<TActual>(string route)
6776
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static class Infer
4040

4141
public static Names Names(IEnumerable<string> names) => string.Join(",", names);
4242

43-
public static Id Id<T>(T document) where T : class => Elastic.Clients.Elasticsearch.Id.From(document);
43+
public static Id Id<T>(T document) where T : class => Elasticsearch.Id.From(document);
4444

4545
public static Fields Fields<T>(params Expression<Func<T, object>>[] fields) where T : class =>
4646
new(fields.Select(f => new Field(f)));
@@ -55,11 +55,11 @@ public static Fields Fields<T>(params Expression<Func<T, object>>[] fields) wher
5555
/// </summary>
5656
public static Field Field<T, TValue>(Expression<Func<T, TValue>> path, double? boost = null,
5757
string format = null)
58-
where T : class => new(path, boost, format);
58+
=> new(path, boost, format);
5959

6060
/// <inheritdoc cref="Field{T, TValue}" />
6161
public static Field Field<T>(Expression<Func<T, object>> path, double? boost = null, string format = null)
62-
where T : class => new(path, boost, format);
62+
=> new(path, boost, format);
6363

6464
public static Field Field(string field, double? boost = null, string format = null) =>
6565
new(field, boost, format);

0 commit comments

Comments
 (0)