-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reintroduce fluent API for
RangeQuery
- Loading branch information
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/Elastic.Clients.Elasticsearch.Shared/Types/QueryDsl/RangeQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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; | ||
|
||
#if ELASTICSEARCH_SERVERLESS | ||
using Elastic.Clients.Elasticsearch.Serverless.Fluent; | ||
#else | ||
using Elastic.Clients.Elasticsearch.Fluent; | ||
#endif | ||
|
||
#if ELASTICSEARCH_SERVERLESS | ||
namespace Elastic.Clients.Elasticsearch.Serverless.QueryDsl; | ||
#else | ||
namespace Elastic.Clients.Elasticsearch.QueryDsl; | ||
#endif | ||
|
||
// TODO: This should be removed after implementing descriptor generation for union types | ||
|
||
public sealed partial class QueryDescriptor<TDocument> | ||
{ | ||
public QueryDescriptor<TDocument> Range(Action<RangeQueryDescriptor<TDocument>> configure) => ProxiedSet(configure, "range"); | ||
|
||
private QueryDescriptor<TDocument> ProxiedSet<T>(Action<T> descriptorAction, string variantName) where T : ProxiedDescriptor<T> | ||
{ | ||
var descriptor = (T)Activator.CreateInstance(typeof(T), true); | ||
descriptorAction?.Invoke(descriptor); | ||
|
||
return Set(descriptor.Result, variantName); | ||
} | ||
} | ||
|
||
public sealed partial class QueryDescriptor | ||
{ | ||
public QueryDescriptor Range(Action<RangeQueryDescriptor> configure) => ProxiedSet(configure, "range"); | ||
public QueryDescriptor Range<TDocument>(Action<RangeQueryDescriptor<TDocument>> configure) => ProxiedSet(configure, "range"); | ||
|
||
private QueryDescriptor ProxiedSet<T>(Action<T> descriptorAction, string variantName) where T : ProxiedDescriptor<T> | ||
{ | ||
var descriptor = (T)Activator.CreateInstance(typeof(T), true); | ||
descriptorAction?.Invoke(descriptor); | ||
|
||
return Set(descriptor.Result, variantName); | ||
} | ||
} | ||
|
||
public abstract class ProxiedDescriptor<T> : Descriptor<T> | ||
where T : Descriptor<T> | ||
{ | ||
internal Descriptor Result { get; set; } | ||
|
||
protected T SetResult<TD>(Action<TD> descriptorAction) where TD : Descriptor | ||
{ | ||
var descriptor = (TD)Activator.CreateInstance(typeof(TD), true); | ||
descriptorAction?.Invoke(descriptor); | ||
Result = descriptor; | ||
return Self; | ||
} | ||
} | ||
|
||
public sealed class RangeQueryDescriptor<TDocument> : ProxiedDescriptor<RangeQueryDescriptor<TDocument>> | ||
{ | ||
public RangeQueryDescriptor<TDocument> NumberRange(Action<NumberRangeQueryDescriptor<TDocument>> configure) => | ||
SetResult(configure); | ||
|
||
public RangeQueryDescriptor<TDocument> DateRange(Action<DateRangeQueryDescriptor<TDocument>> configure) => | ||
SetResult(configure); | ||
} | ||
|
||
public sealed class RangeQueryDescriptor : ProxiedDescriptor<RangeQueryDescriptor> | ||
{ | ||
public RangeQueryDescriptor NumberRange(Action<NumberRangeQueryDescriptor> configure) => SetResult(configure); | ||
|
||
public RangeQueryDescriptor NumberRange<TDocument>(Action<NumberRangeQueryDescriptor<TDocument>> configure) => SetResult(configure); | ||
|
||
public RangeQueryDescriptor DateRange(Action<DateRangeQueryDescriptor> configure) => SetResult(configure); | ||
|
||
public RangeQueryDescriptor DateRange<TDocument>(Action<DateRangeQueryDescriptor<TDocument>> configure) => SetResult(configure); | ||
} | ||
|
||
public sealed partial class NumberRangeQuery | ||
{ | ||
public static implicit operator Query(NumberRangeQuery numberRangeQuery) => Query.Range(new RangeQuery(numberRangeQuery)); | ||
} | ||
|
||
public sealed partial class DateRangeQuery | ||
{ | ||
public static implicit operator Query(DateRangeQuery dateRangeQuery) => Query.Range(new RangeQuery(dateRangeQuery)); | ||
} |