Skip to content

Commit

Permalink
Improve enum and request (de-)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd committed Feb 6, 2025
1 parent 0a6739e commit 4e18b84
Show file tree
Hide file tree
Showing 49 changed files with 4,532 additions and 488 deletions.

This file was deleted.

52 changes: 32 additions & 20 deletions src/Elastic.Clients.Elasticsearch/_Generated/Api/CreateRequest.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,45 @@ public sealed partial class CreateRequestParameters : RequestParameters
public Elastic.Clients.Elasticsearch.WaitForActiveShards? WaitForActiveShards { get => Q<Elastic.Clients.Elasticsearch.WaitForActiveShards?>("wait_for_active_shards"); set => Q("wait_for_active_shards", value); }
}

internal sealed partial class CreateRequestConverter<TDocument> : System.Text.Json.Serialization.JsonConverter<CreateRequest<TDocument>>
{
public override CreateRequest<TDocument> Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
return new CreateRequest<TDocument> { Document = reader.ReadValue<TDocument>(options, typeof(SourceMarker<TDocument>)) };
}

public override void Write(System.Text.Json.Utf8JsonWriter writer, CreateRequest<TDocument> value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteValue(options, value.Document, typeof(SourceMarker<TDocument>));
}
}

internal sealed partial class CreateRequestConverterFactory : System.Text.Json.Serialization.JsonConverterFactory
{
public override bool CanConvert(System.Type typeToConvert)
{
return typeToConvert.IsGenericType && typeToConvert.GetGenericTypeDefinition() == typeof(CreateRequest<>);
}

public override System.Text.Json.Serialization.JsonConverter CreateConverter(System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
var args = typeToConvert.GetGenericArguments();
#pragma warning disable IL3050
var converter = (System.Text.Json.Serialization.JsonConverter)System.Activator.CreateInstance(typeof(CreateRequestConverter<>).MakeGenericType(args[0]), System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public, binder: null, args: null, culture: null)!;
#pragma warning restore IL3050
return converter;
}
}

/// <summary>
/// <para>
/// Index a document.
/// Adds a JSON document to the specified data stream or index and makes it searchable.
/// If the target is an index and the document already exists, the request updates the document and increments its version.
/// </para>
/// </summary>
public sealed partial class CreateRequest<TDocument> : PlainRequest<CreateRequestParameters>, ISelfTwoWaySerializable
[JsonConverter(typeof(CreateRequestConverterFactory))]
public sealed partial class CreateRequest<TDocument> : PlainRequest<CreateRequestParameters>
{
public CreateRequest(Elastic.Clients.Elasticsearch.IndexName index, Elastic.Clients.Elasticsearch.Id id) : base(r => r.Required("index", index).Required("id", id))
{
Expand All @@ -118,7 +149,6 @@ internal CreateRequest()
/// Unique identifier for the document.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Id Id { get => P<Elastic.Clients.Elasticsearch.Id>("id"); set => PR("id", value); }

/// <summary>
Expand All @@ -128,7 +158,6 @@ internal CreateRequest()
/// If the target doesn’t exist and doesn’t match a data stream template, this request creates the index.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.IndexName Index { get => P<Elastic.Clients.Elasticsearch.IndexName>("index"); set => PR("index", value); }

/// <summary>
Expand All @@ -138,7 +167,6 @@ internal CreateRequest()
/// If a final pipeline is configured it will always run, regardless of the value of this parameter.
/// </para>
/// </summary>
[JsonIgnore]
public string? Pipeline { get => Q<string?>("pipeline"); set => Q("pipeline", value); }

/// <summary>
Expand All @@ -147,23 +175,20 @@ internal CreateRequest()
/// Valid values: <c>true</c>, <c>false</c>, <c>wait_for</c>.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Refresh? Refresh { get => Q<Elastic.Clients.Elasticsearch.Refresh?>("refresh"); set => Q("refresh", value); }

/// <summary>
/// <para>
/// Custom value used to route operations to a specific shard.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Routing? Routing { get => Q<Elastic.Clients.Elasticsearch.Routing?>("routing"); set => Q("routing", value); }

/// <summary>
/// <para>
/// Period the request waits for the following operations: automatic index creation, dynamic mapping updates, waiting for active shards.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Duration? Timeout { get => Q<Elastic.Clients.Elasticsearch.Duration?>("timeout"); set => Q("timeout", value); }

/// <summary>
Expand All @@ -172,15 +197,13 @@ internal CreateRequest()
/// The specified version must match the current version of the document for the request to succeed.
/// </para>
/// </summary>
[JsonIgnore]
public long? Version { get => Q<long?>("version"); set => Q("version", value); }

/// <summary>
/// <para>
/// Specific version type: <c>external</c>, <c>external_gte</c>.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.VersionType? VersionType { get => Q<Elastic.Clients.Elasticsearch.VersionType?>("version_type"); set => Q("version_type", value); }

/// <summary>
Expand All @@ -189,19 +212,8 @@ internal CreateRequest()
/// Set to <c>all</c> or any positive integer up to the total number of shards in the index (<c>number_of_replicas+1</c>).
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.WaitForActiveShards? WaitForActiveShards { get => Q<Elastic.Clients.Elasticsearch.WaitForActiveShards?>("wait_for_active_shards"); set => Q("wait_for_active_shards", value); }
public TDocument Document { get; set; }

void ISelfTwoWaySerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
settings.SourceSerializer.Serialize(Document, writer);
}

void ISelfTwoWaySerializable.Deserialize(ref Utf8JsonReader reader, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
Document = settings.SourceSerializer.Deserialize<TDocument>(ref reader);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ public sealed partial class DownsampleRequestParameters : RequestParameters
{
}

internal sealed partial class DownsampleRequestConverter : System.Text.Json.Serialization.JsonConverter<DownsampleRequest>
{
public override DownsampleRequest Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
return new DownsampleRequest { Config = reader.ReadValue<Elastic.Clients.Elasticsearch.IndexManagement.DownsampleConfig>(options) };
}

public override void Write(System.Text.Json.Utf8JsonWriter writer, DownsampleRequest value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteValue(options, value.Config);
}
}

/// <summary>
/// <para>
/// Downsample an index.
Expand All @@ -47,7 +60,8 @@ public sealed partial class DownsampleRequestParameters : RequestParameters
/// The source index must be read only (<c>index.blocks.write: true</c>).
/// </para>
/// </summary>
public sealed partial class DownsampleRequest : PlainRequest<DownsampleRequestParameters>, ISelfTwoWaySerializable
[JsonConverter(typeof(DownsampleRequestConverter))]
public sealed partial class DownsampleRequest : PlainRequest<DownsampleRequestParameters>
{
public DownsampleRequest(Elastic.Clients.Elasticsearch.IndexName index, Elastic.Clients.Elasticsearch.IndexName targetIndex) : base(r => r.Required("index", index).Required("target_index", targetIndex))
{
Expand All @@ -71,27 +85,15 @@ internal DownsampleRequest()
/// Name of the time series index to downsample.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.IndexName Index { get => P<Elastic.Clients.Elasticsearch.IndexName>("index"); set => PR("index", value); }

/// <summary>
/// <para>
/// Name of the index to create.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.IndexName TargetIndex { get => P<Elastic.Clients.Elasticsearch.IndexName>("target_index"); set => PR("target_index", value); }
public Elastic.Clients.Elasticsearch.IndexManagement.DownsampleConfig Config { get; set; }

void ISelfTwoWaySerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
JsonSerializer.Serialize(writer, Config, options);
}

void ISelfTwoWaySerializable.Deserialize(ref Utf8JsonReader reader, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
Config = settings.RequestResponseSerializer.Deserialize<Elastic.Clients.Elasticsearch.IndexManagement.DownsampleConfig>(ref reader);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,27 @@ public sealed partial class PutDataLifecycleRequestParameters : RequestParameter
public Elastic.Clients.Elasticsearch.Duration? Timeout { get => Q<Elastic.Clients.Elasticsearch.Duration?>("timeout"); set => Q("timeout", value); }
}

internal sealed partial class PutDataLifecycleRequestConverter : System.Text.Json.Serialization.JsonConverter<PutDataLifecycleRequest>
{
public override PutDataLifecycleRequest Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
return new PutDataLifecycleRequest { Lifecycle = reader.ReadValue<Elastic.Clients.Elasticsearch.IndexManagement.DataStreamLifecycle>(options) };
}

public override void Write(System.Text.Json.Utf8JsonWriter writer, PutDataLifecycleRequest value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteValue(options, value.Lifecycle);
}
}

/// <summary>
/// <para>
/// Update data stream lifecycles.
/// Update the data stream lifecycle of the specified data streams.
/// </para>
/// </summary>
public sealed partial class PutDataLifecycleRequest : PlainRequest<PutDataLifecycleRequestParameters>, ISelfTwoWaySerializable
[JsonConverter(typeof(PutDataLifecycleRequestConverter))]
public sealed partial class PutDataLifecycleRequest : PlainRequest<PutDataLifecycleRequestParameters>
{
public PutDataLifecycleRequest(Elastic.Clients.Elasticsearch.DataStreamNames name) : base(r => r.Required("name", name))
{
Expand All @@ -91,7 +105,6 @@ internal PutDataLifecycleRequest()
/// To target all data streams use <c>*</c> or <c>_all</c>.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.DataStreamNames Name { get => P<Elastic.Clients.Elasticsearch.DataStreamNames>("name"); set => PR("name", value); }

/// <summary>
Expand All @@ -101,7 +114,6 @@ internal PutDataLifecycleRequest()
/// Valid values are: <c>all</c>, <c>hidden</c>, <c>open</c>, <c>closed</c>, <c>none</c>.
/// </para>
/// </summary>
[JsonIgnore]
public ICollection<Elastic.Clients.Elasticsearch.ExpandWildcard>? ExpandWildcards { get => Q<ICollection<Elastic.Clients.Elasticsearch.ExpandWildcard>?>("expand_wildcards"); set => Q("expand_wildcards", value); }

/// <summary>
Expand All @@ -111,7 +123,6 @@ internal PutDataLifecycleRequest()
/// error.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Duration? MasterTimeout { get => Q<Elastic.Clients.Elasticsearch.Duration?>("master_timeout"); set => Q("master_timeout", value); }

/// <summary>
Expand All @@ -120,19 +131,8 @@ internal PutDataLifecycleRequest()
/// If no response is received before the timeout expires, the request fails and returns an error.
/// </para>
/// </summary>
[JsonIgnore]
public Elastic.Clients.Elasticsearch.Duration? Timeout { get => Q<Elastic.Clients.Elasticsearch.Duration?>("timeout"); set => Q("timeout", value); }
public Elastic.Clients.Elasticsearch.IndexManagement.DataStreamLifecycle Lifecycle { get; set; }

void ISelfTwoWaySerializable.Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
JsonSerializer.Serialize(writer, Lifecycle, options);
}

void ISelfTwoWaySerializable.Deserialize(ref Utf8JsonReader reader, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
Lifecycle = settings.RequestResponseSerializer.Deserialize<Elastic.Clients.Elasticsearch.IndexManagement.DataStreamLifecycle>(ref reader);
}
}

/// <summary>
Expand Down
Loading

0 comments on commit 4e18b84

Please sign in to comment.