Skip to content

Commit 32ce4f1

Browse files
Code gen for internally tagged union variant converters (#6779) (#6781)
* Cleanup and remove commented code * Rename file * Code gen for internally tagged union variant converters Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent 442618f commit 32ce4f1

12 files changed

+643
-145
lines changed

src/Elastic.Clients.Elasticsearch/Types/Mapping/Properties.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6-
using System.Collections.Generic;
76
using System.Linq.Expressions;
87

98
namespace Elastic.Clients.Elasticsearch.Mapping;
@@ -34,40 +33,3 @@ public partial class Properties<TDocument> : Properties
3433
{
3534
public void Add<TValue>(Expression<Func<TDocument, TValue>> name, IProperty property) => BackingDictionary.Add(name, property);
3635
}
37-
38-
// TODO
39-
// Code generator should generate these for any InternallyTaggedUnions that are IsADictionary types.
40-
// These work on generic and non-generic descriptors.
41-
//public partial class TypeMappingDescriptor
42-
//{
43-
// public TypeMappingDescriptor Properties<TDocument>(PropertiesDescriptor<TDocument> descriptor)
44-
// {
45-
// PropertiesValue = descriptor.PromisedValue;
46-
// return Self;
47-
// }
48-
49-
// public TypeMappingDescriptor Properties<TDocument>(Action<PropertiesDescriptor<TDocument>> configure)
50-
// {
51-
// var descriptor = new PropertiesDescriptor<TDocument>();
52-
// configure?.Invoke(descriptor);
53-
// PropertiesValue = descriptor.PromisedValue;
54-
// return Self;
55-
// }
56-
//}
57-
58-
//public partial class TypeMappingDescriptor<TDocument>
59-
//{
60-
// public TypeMappingDescriptor<TDocument> Properties(PropertiesDescriptor<TDocument> descriptor)
61-
// {
62-
// PropertiesValue = descriptor.PromisedValue;
63-
// return Self;
64-
// }
65-
66-
// public TypeMappingDescriptor<TDocument> Properties(Action<PropertiesDescriptor<TDocument>> configure)
67-
// {
68-
// var descriptor = new PropertiesDescriptor<TDocument>();
69-
// configure?.Invoke(descriptor);
70-
// PropertiesValue = descriptor.PromisedValue;
71-
// return Self;
72-
// }
73-
//}

src/Elastic.Clients.Elasticsearch/Types/Mapping/PropertyBase.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.

src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/AggregateDictionary.g.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,4 @@ public AggregateDictionary(IReadOnlyDictionary<string, IAggregate> backingDictio
8282
public Elastic.Clients.Elasticsearch.Aggregations.MatrixStatsAggregate? GetMatrixStats(string key) => TryGet<Elastic.Clients.Elasticsearch.Aggregations.MatrixStatsAggregate?>(key);
8383
private TAggregate TryGet<TAggregate>(string key)
8484
where TAggregate : class, IAggregate => BackingDictionary.TryGetValue(key, out var agg) ? agg as TAggregate : null;
85-
}
85+
}

src/Elastic.Clients.Elasticsearch/_Generated/Types/Analysis/Analyzers.g.cs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,18 @@ public AnalyzersDescriptor() : base(new Analyzers())
9393
public AnalyzersDescriptor Whitespace(string analyzerName, WhitespaceAnalyzer whitespaceAnalyzer) => AssignVariant(analyzerName, whitespaceAnalyzer);
9494
}
9595

96-
internal sealed partial class AnalyzerInterfaceConverter
96+
internal sealed partial class AnalyzerInterfaceConverter : JsonConverter<IAnalyzer>
9797
{
98-
private static IAnalyzer DeserializeVariant(string type, ref Utf8JsonReader reader, JsonSerializerOptions options)
98+
public override IAnalyzer Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
9999
{
100+
var copiedReader = reader;
101+
string? type = null;
102+
using var jsonDoc = JsonDocument.ParseValue(ref copiedReader);
103+
if (jsonDoc is not null && jsonDoc.RootElement.TryGetProperty("type", out var readType) && readType.ValueKind == JsonValueKind.String)
104+
{
105+
type = readType.ToString();
106+
}
107+
100108
switch (type)
101109
{
102110
case "dutch":
@@ -131,8 +139,68 @@ private static IAnalyzer DeserializeVariant(string type, ref Utf8JsonReader read
131139
throw new JsonException("Encounted an unknown variant type which could not be deserialised.");
132140
}
133141
}
142+
143+
public override void Write(Utf8JsonWriter writer, IAnalyzer value, JsonSerializerOptions options)
144+
{
145+
if (value is null)
146+
{
147+
writer.WriteNullValue();
148+
return;
149+
}
150+
151+
switch (value.Type)
152+
{
153+
case "dutch":
154+
JsonSerializer.Serialize(writer, value, typeof(DutchAnalyzer), options);
155+
return;
156+
case "snowball":
157+
JsonSerializer.Serialize(writer, value, typeof(SnowballAnalyzer), options);
158+
return;
159+
case "kuromoji":
160+
JsonSerializer.Serialize(writer, value, typeof(KuromojiAnalyzer), options);
161+
return;
162+
case "icu_analyzer":
163+
JsonSerializer.Serialize(writer, value, typeof(IcuAnalyzer), options);
164+
return;
165+
case "whitespace":
166+
JsonSerializer.Serialize(writer, value, typeof(WhitespaceAnalyzer), options);
167+
return;
168+
case "stop":
169+
JsonSerializer.Serialize(writer, value, typeof(StopAnalyzer), options);
170+
return;
171+
case "standard":
172+
JsonSerializer.Serialize(writer, value, typeof(StandardAnalyzer), options);
173+
return;
174+
case "simple":
175+
JsonSerializer.Serialize(writer, value, typeof(SimpleAnalyzer), options);
176+
return;
177+
case "pattern":
178+
JsonSerializer.Serialize(writer, value, typeof(PatternAnalyzer), options);
179+
return;
180+
case "nori":
181+
JsonSerializer.Serialize(writer, value, typeof(NoriAnalyzer), options);
182+
return;
183+
case "language":
184+
JsonSerializer.Serialize(writer, value, typeof(LanguageAnalyzer), options);
185+
return;
186+
case "keyword":
187+
JsonSerializer.Serialize(writer, value, typeof(KeywordAnalyzer), options);
188+
return;
189+
case "fingerprint":
190+
JsonSerializer.Serialize(writer, value, typeof(FingerprintAnalyzer), options);
191+
return;
192+
case "custom":
193+
JsonSerializer.Serialize(writer, value, typeof(CustomAnalyzer), options);
194+
return;
195+
default:
196+
var type = value.GetType();
197+
JsonSerializer.Serialize(writer, value, type, options);
198+
return;
199+
}
200+
}
134201
}
135202

203+
[JsonConverter(typeof(AnalyzerInterfaceConverter))]
136204
public partial interface IAnalyzer
137205
{
138206
public string Type { get; }

src/Elastic.Clients.Elasticsearch/_Generated/Types/Analysis/CharFilterDefinitions.g.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,18 @@ public CharFilterDefinitionsDescriptor() : base(new CharFilterDefinitions())
6666
public CharFilterDefinitionsDescriptor PatternReplaceCharFilter(string charFilterDefinitionName, PatternReplaceCharFilter patternReplaceCharFilter) => AssignVariant(charFilterDefinitionName, patternReplaceCharFilter);
6767
}
6868

69-
internal sealed partial class CharFilterDefinitionInterfaceConverter
69+
internal sealed partial class CharFilterDefinitionInterfaceConverter : JsonConverter<ICharFilterDefinition>
7070
{
71-
private static ICharFilterDefinition DeserializeVariant(string type, ref Utf8JsonReader reader, JsonSerializerOptions options)
71+
public override ICharFilterDefinition Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
7272
{
73+
var copiedReader = reader;
74+
string? type = null;
75+
using var jsonDoc = JsonDocument.ParseValue(ref copiedReader);
76+
if (jsonDoc is not null && jsonDoc.RootElement.TryGetProperty("type", out var readType) && readType.ValueKind == JsonValueKind.String)
77+
{
78+
type = readType.ToString();
79+
}
80+
7381
switch (type)
7482
{
7583
case "kuromoji_iteration_mark":
@@ -86,8 +94,41 @@ private static ICharFilterDefinition DeserializeVariant(string type, ref Utf8Jso
8694
throw new JsonException("Encounted an unknown variant type which could not be deserialised.");
8795
}
8896
}
97+
98+
public override void Write(Utf8JsonWriter writer, ICharFilterDefinition value, JsonSerializerOptions options)
99+
{
100+
if (value is null)
101+
{
102+
writer.WriteNullValue();
103+
return;
104+
}
105+
106+
switch (value.Type)
107+
{
108+
case "kuromoji_iteration_mark":
109+
JsonSerializer.Serialize(writer, value, typeof(KuromojiIterationMarkCharFilter), options);
110+
return;
111+
case "icu_normalizer":
112+
JsonSerializer.Serialize(writer, value, typeof(IcuNormalizationCharFilter), options);
113+
return;
114+
case "pattern_replace":
115+
JsonSerializer.Serialize(writer, value, typeof(PatternReplaceCharFilter), options);
116+
return;
117+
case "mapping":
118+
JsonSerializer.Serialize(writer, value, typeof(MappingCharFilter), options);
119+
return;
120+
case "html_strip":
121+
JsonSerializer.Serialize(writer, value, typeof(HtmlStripCharFilter), options);
122+
return;
123+
default:
124+
var type = value.GetType();
125+
JsonSerializer.Serialize(writer, value, type, options);
126+
return;
127+
}
128+
}
89129
}
90130

131+
[JsonConverter(typeof(CharFilterDefinitionInterfaceConverter))]
91132
public partial interface ICharFilterDefinition
92133
{
93134
public string Type { get; }

src/Elastic.Clients.Elasticsearch/_Generated/Types/Analysis/Normalizers.g.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,18 @@ public NormalizersDescriptor() : base(new Normalizers())
5757
public NormalizersDescriptor Lowercase(string normalizerName, LowercaseNormalizer lowercaseNormalizer) => AssignVariant(normalizerName, lowercaseNormalizer);
5858
}
5959

60-
internal sealed partial class NormalizerInterfaceConverter
60+
internal sealed partial class NormalizerInterfaceConverter : JsonConverter<INormalizer>
6161
{
62-
private static INormalizer DeserializeVariant(string type, ref Utf8JsonReader reader, JsonSerializerOptions options)
62+
public override INormalizer Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
6363
{
64+
var copiedReader = reader;
65+
string? type = null;
66+
using var jsonDoc = JsonDocument.ParseValue(ref copiedReader);
67+
if (jsonDoc is not null && jsonDoc.RootElement.TryGetProperty("type", out var readType) && readType.ValueKind == JsonValueKind.String)
68+
{
69+
type = readType.ToString();
70+
}
71+
6472
switch (type)
6573
{
6674
case "custom":
@@ -71,8 +79,32 @@ private static INormalizer DeserializeVariant(string type, ref Utf8JsonReader re
7179
throw new JsonException("Encounted an unknown variant type which could not be deserialised.");
7280
}
7381
}
82+
83+
public override void Write(Utf8JsonWriter writer, INormalizer value, JsonSerializerOptions options)
84+
{
85+
if (value is null)
86+
{
87+
writer.WriteNullValue();
88+
return;
89+
}
90+
91+
switch (value.Type)
92+
{
93+
case "custom":
94+
JsonSerializer.Serialize(writer, value, typeof(CustomNormalizer), options);
95+
return;
96+
case "lowercase":
97+
JsonSerializer.Serialize(writer, value, typeof(LowercaseNormalizer), options);
98+
return;
99+
default:
100+
var type = value.GetType();
101+
JsonSerializer.Serialize(writer, value, type, options);
102+
return;
103+
}
104+
}
74105
}
75106

107+
[JsonConverter(typeof(NormalizerInterfaceConverter))]
76108
public partial interface INormalizer
77109
{
78110
public string Type { get; }

0 commit comments

Comments
 (0)