|
| 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 | + |
| 7 | +#if ELASTICSEARCH_SERVERLESS |
| 8 | +using Elastic.Clients.Elasticsearch.Serverless.Fluent; |
| 9 | +#else |
| 10 | +using Elastic.Clients.Elasticsearch.Fluent; |
| 11 | +#endif |
| 12 | + |
| 13 | +#if ELASTICSEARCH_SERVERLESS |
| 14 | +namespace Elastic.Clients.Elasticsearch.Serverless.QueryDsl; |
| 15 | +#else |
| 16 | +namespace Elastic.Clients.Elasticsearch.QueryDsl; |
| 17 | +#endif |
| 18 | + |
| 19 | +// TODO: This should be removed after implementing descriptor generation for union types |
| 20 | + |
| 21 | +public sealed partial class QueryDescriptor<TDocument> |
| 22 | +{ |
| 23 | + public QueryDescriptor<TDocument> Range(Action<RangeQueryDescriptor<TDocument>> configure) => ProxiedSet(configure, "range"); |
| 24 | + |
| 25 | + private QueryDescriptor<TDocument> ProxiedSet<T>(Action<T> descriptorAction, string variantName) where T : ProxiedDescriptor<T> |
| 26 | + { |
| 27 | + var descriptor = (T)Activator.CreateInstance(typeof(T), true); |
| 28 | + descriptorAction?.Invoke(descriptor); |
| 29 | + |
| 30 | + return Set(descriptor.Result, variantName); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +public sealed partial class QueryDescriptor |
| 35 | +{ |
| 36 | + public QueryDescriptor Range(Action<RangeQueryDescriptor> configure) => ProxiedSet(configure, "range"); |
| 37 | + public QueryDescriptor Range<TDocument>(Action<RangeQueryDescriptor<TDocument>> configure) => ProxiedSet(configure, "range"); |
| 38 | + |
| 39 | + private QueryDescriptor ProxiedSet<T>(Action<T> descriptorAction, string variantName) where T : ProxiedDescriptor<T> |
| 40 | + { |
| 41 | + var descriptor = (T)Activator.CreateInstance(typeof(T), true); |
| 42 | + descriptorAction?.Invoke(descriptor); |
| 43 | + |
| 44 | + return Set(descriptor.Result, variantName); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +public abstract class ProxiedDescriptor<T> : Descriptor<T> |
| 49 | + where T : Descriptor<T> |
| 50 | +{ |
| 51 | + internal Descriptor Result { get; set; } |
| 52 | + |
| 53 | + protected T SetResult<TD>(Action<TD> descriptorAction) where TD : Descriptor |
| 54 | + { |
| 55 | + var descriptor = (TD)Activator.CreateInstance(typeof(TD), true); |
| 56 | + descriptorAction?.Invoke(descriptor); |
| 57 | + Result = descriptor; |
| 58 | + return Self; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +public sealed class RangeQueryDescriptor<TDocument> : ProxiedDescriptor<RangeQueryDescriptor<TDocument>> |
| 63 | +{ |
| 64 | + public RangeQueryDescriptor<TDocument> NumberRange(Action<NumberRangeQueryDescriptor<TDocument>> configure) => |
| 65 | + SetResult(configure); |
| 66 | + |
| 67 | + public RangeQueryDescriptor<TDocument> DateRange(Action<DateRangeQueryDescriptor<TDocument>> configure) => |
| 68 | + SetResult(configure); |
| 69 | +} |
| 70 | + |
| 71 | +public sealed class RangeQueryDescriptor : ProxiedDescriptor<RangeQueryDescriptor> |
| 72 | +{ |
| 73 | + public RangeQueryDescriptor NumberRange(Action<NumberRangeQueryDescriptor> configure) => SetResult(configure); |
| 74 | + |
| 75 | + public RangeQueryDescriptor NumberRange<TDocument>(Action<NumberRangeQueryDescriptor<TDocument>> configure) => SetResult(configure); |
| 76 | + |
| 77 | + public RangeQueryDescriptor DateRange(Action<DateRangeQueryDescriptor> configure) => SetResult(configure); |
| 78 | + |
| 79 | + public RangeQueryDescriptor DateRange<TDocument>(Action<DateRangeQueryDescriptor<TDocument>> configure) => SetResult(configure); |
| 80 | +} |
| 81 | + |
| 82 | +public sealed partial class NumberRangeQuery |
| 83 | +{ |
| 84 | + public static implicit operator Query(NumberRangeQuery numberRangeQuery) => Query.Range(new RangeQuery(numberRangeQuery)); |
| 85 | +} |
| 86 | + |
| 87 | +public sealed partial class DateRangeQuery |
| 88 | +{ |
| 89 | + public static implicit operator Query(DateRangeQuery dateRangeQuery) => Query.Range(new RangeQuery(dateRangeQuery)); |
| 90 | +} |
0 commit comments