Skip to content

Fix BucketsPath (de-)serialization #8164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
// 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.
//
// ███╗ ██╗ ██████╗ ████████╗██╗ ██████╗███████╗
// ████╗ ██║██╔═══██╗╚══██╔══╝██║██╔════╝██╔════╝
// ██╔██╗ ██║██║ ██║ ██║ ██║██║ █████╗
// ██║╚██╗██║██║ ██║ ██║ ██║██║ ██╔══╝
// ██║ ╚████║╚██████╔╝ ██║ ██║╚██████╗███████╗
// ╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝
// ------------------------------------------------
//
// This file is automatically generated.
// Please do not edit these files manually.
//
// ------------------------------------------------

#nullable restore

#nullable enable

#if ELASTICSEARCH_SERVERLESS
using Elastic.Clients.Elasticsearch.Serverless.Core;
#else
using Elastic.Clients.Elasticsearch.Core;
using Elastic.Clients.Elasticsearch.Fluent;
using Elastic.Clients.Elasticsearch.Serialization;
using Elastic.Transport;
#endif

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Text.Json;
using System.Text.Json.Serialization;

#if ELASTICSEARCH_SERVERLESS
namespace Elastic.Clients.Elasticsearch.Serverless.Aggregations;
#else
namespace Elastic.Clients.Elasticsearch.Aggregations;
#endif

/// <summary>
/// <para>Buckets path can be expressed in different ways, and an aggregation may accept some or all of these<br/>forms depending on its type. Please refer to each aggregation's documentation to know what buckets<br/>path forms they accept.</para>
Expand All @@ -43,8 +35,8 @@ public enum Kind
Dictionary
}

private readonly Kind _kind;
private readonly object _value;
internal readonly Kind _kind;
internal readonly object _value;

Kind IComplexUnion<Kind>.ValueKind => _kind;

Expand Down Expand Up @@ -111,12 +103,36 @@ public bool TryGetDictionary([NotNullWhen(true)] out Dictionary<string, string>?
public static implicit operator BucketsPath(Dictionary<string, string> dictionary) => BucketsPath.Dictionary(dictionary);
}

internal sealed class BucketsPathConverter : MultiItemUnionConverter<BucketsPath, BucketsPath.Kind>
internal sealed class BucketsPathConverter : JsonConverter<BucketsPath>
{
public BucketsPathConverter()
public override BucketsPath? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) =>
(reader.TokenType) switch
{
JsonTokenType.Null => null,
JsonTokenType.String => BucketsPath.Single(JsonSerializer.Deserialize<string>(ref reader, options)!),
JsonTokenType.StartArray => BucketsPath.Array(JsonSerializer.Deserialize<string[]>(ref reader, options)!),
JsonTokenType.StartObject => BucketsPath.Dictionary(JsonSerializer.Deserialize<Dictionary<string, string>>(ref reader, options)!),
_ => throw new JsonException($"Unexpected token '{reader.TokenType}'.")
};

public override void Write(Utf8JsonWriter writer, BucketsPath value, JsonSerializerOptions options)
{
_types = new Dictionary<BucketsPath.Kind, Type> { { BucketsPath.Kind.Single, typeof(string) }, { BucketsPath.Kind.Array, typeof(IReadOnlyCollection<string>) }, { BucketsPath.Kind.Dictionary, typeof(IReadOnlyDictionary<string, string>) } };
_factories = new Dictionary<Type, Func<object, BucketsPath>> { { typeof(string), o => BucketsPath.Single((string)o) }, { typeof(string[]), o => BucketsPath.Array((string[])o) }, { typeof(Dictionary<string, string>), o => BucketsPath.Dictionary((Dictionary<string, string>)o) } };
_uniquePropertyToType = new Dictionary<string, Type> { { "", typeof(string) }, { "", typeof(IReadOnlyCollection<string>) }, { "", typeof(IReadOnlyDictionary<string, string>) } };
switch (value._kind)
{
case BucketsPath.Kind.Single:
writer.WriteStringValue((string)value._value);
break;

case BucketsPath.Kind.Array:
JsonSerializer.Serialize(writer, (string[])value._value, options);
break;

case BucketsPath.Kind.Dictionary:
JsonSerializer.Serialize(writer, (Dictionary<string, string>)value._value, options);
break;

default:
throw new ArgumentOutOfRangeException();
}
}
}
}
Loading