Skip to content

[Backport 8.1] Code gen for internally tagged union variant converters #6781

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
Oct 17, 2022
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
38 changes: 0 additions & 38 deletions src/Elastic.Clients.Elasticsearch/Types/Mapping/Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Elastic.Clients.Elasticsearch.Mapping;
Expand Down Expand Up @@ -34,40 +33,3 @@ public partial class Properties<TDocument> : Properties
{
public void Add<TValue>(Expression<Func<TDocument, TValue>> name, IProperty property) => BackingDictionary.Add(name, property);
}

// TODO
// Code generator should generate these for any InternallyTaggedUnions that are IsADictionary types.
// These work on generic and non-generic descriptors.
//public partial class TypeMappingDescriptor
//{
// public TypeMappingDescriptor Properties<TDocument>(PropertiesDescriptor<TDocument> descriptor)
// {
// PropertiesValue = descriptor.PromisedValue;
// return Self;
// }

// public TypeMappingDescriptor Properties<TDocument>(Action<PropertiesDescriptor<TDocument>> configure)
// {
// var descriptor = new PropertiesDescriptor<TDocument>();
// configure?.Invoke(descriptor);
// PropertiesValue = descriptor.PromisedValue;
// return Self;
// }
//}

//public partial class TypeMappingDescriptor<TDocument>
//{
// public TypeMappingDescriptor<TDocument> Properties(PropertiesDescriptor<TDocument> descriptor)
// {
// PropertiesValue = descriptor.PromisedValue;
// return Self;
// }

// public TypeMappingDescriptor<TDocument> Properties(Action<PropertiesDescriptor<TDocument>> configure)
// {
// var descriptor = new PropertiesDescriptor<TDocument>();
// configure?.Invoke(descriptor);
// PropertiesValue = descriptor.PromisedValue;
// return Self;
// }
//}
55 changes: 0 additions & 55 deletions src/Elastic.Clients.Elasticsearch/Types/Mapping/PropertyBase.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ public AggregateDictionary(IReadOnlyDictionary<string, IAggregate> backingDictio
public Elastic.Clients.Elasticsearch.Aggregations.MatrixStatsAggregate? GetMatrixStats(string key) => TryGet<Elastic.Clients.Elasticsearch.Aggregations.MatrixStatsAggregate?>(key);
private TAggregate TryGet<TAggregate>(string key)
where TAggregate : class, IAggregate => BackingDictionary.TryGetValue(key, out var agg) ? agg as TAggregate : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,18 @@ public AnalyzersDescriptor() : base(new Analyzers())
public AnalyzersDescriptor Whitespace(string analyzerName, WhitespaceAnalyzer whitespaceAnalyzer) => AssignVariant(analyzerName, whitespaceAnalyzer);
}

internal sealed partial class AnalyzerInterfaceConverter
internal sealed partial class AnalyzerInterfaceConverter : JsonConverter<IAnalyzer>
{
private static IAnalyzer DeserializeVariant(string type, ref Utf8JsonReader reader, JsonSerializerOptions options)
public override IAnalyzer Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var copiedReader = reader;
string? type = null;
using var jsonDoc = JsonDocument.ParseValue(ref copiedReader);
if (jsonDoc is not null && jsonDoc.RootElement.TryGetProperty("type", out var readType) && readType.ValueKind == JsonValueKind.String)
{
type = readType.ToString();
}

switch (type)
{
case "dutch":
Expand Down Expand Up @@ -131,8 +139,68 @@ private static IAnalyzer DeserializeVariant(string type, ref Utf8JsonReader read
throw new JsonException("Encounted an unknown variant type which could not be deserialised.");
}
}

public override void Write(Utf8JsonWriter writer, IAnalyzer value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}

switch (value.Type)
{
case "dutch":
JsonSerializer.Serialize(writer, value, typeof(DutchAnalyzer), options);
return;
case "snowball":
JsonSerializer.Serialize(writer, value, typeof(SnowballAnalyzer), options);
return;
case "kuromoji":
JsonSerializer.Serialize(writer, value, typeof(KuromojiAnalyzer), options);
return;
case "icu_analyzer":
JsonSerializer.Serialize(writer, value, typeof(IcuAnalyzer), options);
return;
case "whitespace":
JsonSerializer.Serialize(writer, value, typeof(WhitespaceAnalyzer), options);
return;
case "stop":
JsonSerializer.Serialize(writer, value, typeof(StopAnalyzer), options);
return;
case "standard":
JsonSerializer.Serialize(writer, value, typeof(StandardAnalyzer), options);
return;
case "simple":
JsonSerializer.Serialize(writer, value, typeof(SimpleAnalyzer), options);
return;
case "pattern":
JsonSerializer.Serialize(writer, value, typeof(PatternAnalyzer), options);
return;
case "nori":
JsonSerializer.Serialize(writer, value, typeof(NoriAnalyzer), options);
return;
case "language":
JsonSerializer.Serialize(writer, value, typeof(LanguageAnalyzer), options);
return;
case "keyword":
JsonSerializer.Serialize(writer, value, typeof(KeywordAnalyzer), options);
return;
case "fingerprint":
JsonSerializer.Serialize(writer, value, typeof(FingerprintAnalyzer), options);
return;
case "custom":
JsonSerializer.Serialize(writer, value, typeof(CustomAnalyzer), options);
return;
default:
var type = value.GetType();
JsonSerializer.Serialize(writer, value, type, options);
return;
}
}
}

[JsonConverter(typeof(AnalyzerInterfaceConverter))]
public partial interface IAnalyzer
{
public string Type { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,18 @@ public CharFilterDefinitionsDescriptor() : base(new CharFilterDefinitions())
public CharFilterDefinitionsDescriptor PatternReplaceCharFilter(string charFilterDefinitionName, PatternReplaceCharFilter patternReplaceCharFilter) => AssignVariant(charFilterDefinitionName, patternReplaceCharFilter);
}

internal sealed partial class CharFilterDefinitionInterfaceConverter
internal sealed partial class CharFilterDefinitionInterfaceConverter : JsonConverter<ICharFilterDefinition>
{
private static ICharFilterDefinition DeserializeVariant(string type, ref Utf8JsonReader reader, JsonSerializerOptions options)
public override ICharFilterDefinition Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var copiedReader = reader;
string? type = null;
using var jsonDoc = JsonDocument.ParseValue(ref copiedReader);
if (jsonDoc is not null && jsonDoc.RootElement.TryGetProperty("type", out var readType) && readType.ValueKind == JsonValueKind.String)
{
type = readType.ToString();
}

switch (type)
{
case "kuromoji_iteration_mark":
Expand All @@ -86,8 +94,41 @@ private static ICharFilterDefinition DeserializeVariant(string type, ref Utf8Jso
throw new JsonException("Encounted an unknown variant type which could not be deserialised.");
}
}

public override void Write(Utf8JsonWriter writer, ICharFilterDefinition value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}

switch (value.Type)
{
case "kuromoji_iteration_mark":
JsonSerializer.Serialize(writer, value, typeof(KuromojiIterationMarkCharFilter), options);
return;
case "icu_normalizer":
JsonSerializer.Serialize(writer, value, typeof(IcuNormalizationCharFilter), options);
return;
case "pattern_replace":
JsonSerializer.Serialize(writer, value, typeof(PatternReplaceCharFilter), options);
return;
case "mapping":
JsonSerializer.Serialize(writer, value, typeof(MappingCharFilter), options);
return;
case "html_strip":
JsonSerializer.Serialize(writer, value, typeof(HtmlStripCharFilter), options);
return;
default:
var type = value.GetType();
JsonSerializer.Serialize(writer, value, type, options);
return;
}
}
}

[JsonConverter(typeof(CharFilterDefinitionInterfaceConverter))]
public partial interface ICharFilterDefinition
{
public string Type { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ public NormalizersDescriptor() : base(new Normalizers())
public NormalizersDescriptor Lowercase(string normalizerName, LowercaseNormalizer lowercaseNormalizer) => AssignVariant(normalizerName, lowercaseNormalizer);
}

internal sealed partial class NormalizerInterfaceConverter
internal sealed partial class NormalizerInterfaceConverter : JsonConverter<INormalizer>
{
private static INormalizer DeserializeVariant(string type, ref Utf8JsonReader reader, JsonSerializerOptions options)
public override INormalizer Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var copiedReader = reader;
string? type = null;
using var jsonDoc = JsonDocument.ParseValue(ref copiedReader);
if (jsonDoc is not null && jsonDoc.RootElement.TryGetProperty("type", out var readType) && readType.ValueKind == JsonValueKind.String)
{
type = readType.ToString();
}

switch (type)
{
case "custom":
Expand All @@ -71,8 +79,32 @@ private static INormalizer DeserializeVariant(string type, ref Utf8JsonReader re
throw new JsonException("Encounted an unknown variant type which could not be deserialised.");
}
}

public override void Write(Utf8JsonWriter writer, INormalizer value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}

switch (value.Type)
{
case "custom":
JsonSerializer.Serialize(writer, value, typeof(CustomNormalizer), options);
return;
case "lowercase":
JsonSerializer.Serialize(writer, value, typeof(LowercaseNormalizer), options);
return;
default:
var type = value.GetType();
JsonSerializer.Serialize(writer, value, type, options);
return;
}
}
}

[JsonConverter(typeof(NormalizerInterfaceConverter))]
public partial interface INormalizer
{
public string Type { get; }
Expand Down
Loading