Skip to content

Commit c7f5659

Browse files
stevejgordongithub-actions[bot]
authored andcommitted
Port TermsExclude type (#6859)
1 parent 3380e34 commit c7f5659

File tree

102 files changed

+166
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+166
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
using System.Collections.Generic;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
10+
namespace Elastic.Clients.Elasticsearch.Types.Aggregations;
11+
12+
/// <summary>
13+
/// Filters which terms to exclude from the response.
14+
/// </summary>
15+
[JsonConverter(typeof(TermsExcludeConverter))]
16+
public sealed class TermsExclude
17+
{
18+
/// <summary>
19+
/// Creates an instance of <see cref="TermsExclude" /> that uses a regular expression pattern
20+
/// to determine the terms to exclude from the response.
21+
/// </summary>
22+
/// <param name="pattern">The regular expression pattern.</param>
23+
public TermsExclude(string regexPattern) => RegexPattern = regexPattern;
24+
25+
/// <summary>
26+
/// Creates an instance of <see cref="TermsExclude" /> that uses a collection of terms
27+
/// to exclude from the response.
28+
/// </summary>
29+
/// <param name="values">The exact terms to exclude.</param>
30+
public TermsExclude(ICollection<string> values) => Values = values;
31+
32+
/// <summary>
33+
/// The regular expression pattern to determine terms to exclude from the response.
34+
/// </summary>
35+
public string? RegexPattern { get; }
36+
37+
/// <summary>
38+
/// Collection of terms to exclude from the response.
39+
/// </summary>
40+
public ICollection<string>? Values { get; }
41+
}
42+
43+
internal sealed class TermsExcludeConverter : JsonConverter<TermsExclude>
44+
{
45+
public override TermsExclude? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
46+
{
47+
if (reader.TokenType == JsonTokenType.Null)
48+
{
49+
reader.Read();
50+
return null;
51+
}
52+
53+
TermsExclude termsExclude;
54+
55+
switch (reader.TokenType)
56+
{
57+
case JsonTokenType.StartArray:
58+
var terms = JsonSerializer.Deserialize<IList<string>>(ref reader, options);
59+
termsExclude = new TermsExclude(terms);
60+
break;
61+
case JsonTokenType.String:
62+
var regex = reader.GetString();
63+
termsExclude = new TermsExclude(regex);
64+
break;
65+
default:
66+
throw new JsonException($"Unexpected token {reader.TokenType} when deserializing {nameof(TermsExclude)}");
67+
}
68+
69+
return termsExclude;
70+
}
71+
72+
public override void Write(Utf8JsonWriter writer, TermsExclude value, JsonSerializerOptions options)
73+
{
74+
if (value is null)
75+
{
76+
writer.WriteNullValue();
77+
return;
78+
}
79+
80+
if (value.Values is not null)
81+
{
82+
JsonSerializer.Serialize<IEnumerable<string>>(writer, value.Values, options);
83+
return;
84+
}
85+
86+
writer.WriteStringValue(value.RegexPattern);
87+
}
88+
}

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

+9-10
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public override TermsAggregation Read(ref Utf8JsonReader reader, Type typeToConv
5555
if (reader.ValueTextEquals("exclude"))
5656
{
5757
reader.Read();
58-
var value = SingleOrManySerializationHelper.Deserialize<string>(ref reader, options);
58+
var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Aggregations.TermsExclude?>(ref reader, options);
5959
if (value is not null)
6060
{
6161
agg.Exclude = value;
@@ -267,7 +267,7 @@ public override void Write(Utf8JsonWriter writer, TermsAggregation value, JsonSe
267267
if (value.Exclude is not null)
268268
{
269269
writer.WritePropertyName("exclude");
270-
SingleOrManySerializationHelper.Serialize<string>(value.Exclude, writer, options);
270+
JsonSerializer.Serialize(writer, value.Exclude, options);
271271
}
272272

273273
if (value.ExecutionHint is not null)
@@ -377,8 +377,7 @@ internal TermsAggregation()
377377

378378
public Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationCollectMode? CollectMode { get; set; }
379379

380-
[JsonConverter(typeof(TermsExcludeConverter))]
381-
public IList<string>? Exclude { get; set; }
380+
public Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? Exclude { get; set; }
382381

383382
public Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationExecutionHint? ExecutionHint { get; set; }
384383

@@ -427,7 +426,7 @@ public TermsAggregationDescriptor() : base()
427426

428427
private Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationCollectMode? CollectModeValue { get; set; }
429428

430-
private IList<string>? ExcludeValue { get; set; }
429+
private Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? ExcludeValue { get; set; }
431430

432431
private Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationExecutionHint? ExecutionHintValue { get; set; }
433432

@@ -487,7 +486,7 @@ public TermsAggregationDescriptor<TDocument> CollectMode(Elastic.Clients.Elastic
487486
return Self;
488487
}
489488

490-
public TermsAggregationDescriptor<TDocument> Exclude(IList<string>? exclude)
489+
public TermsAggregationDescriptor<TDocument> Exclude(Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? exclude)
491490
{
492491
ExcludeValue = exclude;
493492
return Self;
@@ -597,7 +596,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
597596
if (ExcludeValue is not null)
598597
{
599598
writer.WritePropertyName("exclude");
600-
SingleOrManySerializationHelper.Serialize<string>(ExcludeValue, writer, options);
599+
JsonSerializer.Serialize(writer, ExcludeValue, options);
601600
}
602601

603602
if (ExecutionHintValue is not null)
@@ -720,7 +719,7 @@ public TermsAggregationDescriptor() : base()
720719

721720
private Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationCollectMode? CollectModeValue { get; set; }
722721

723-
private IList<string>? ExcludeValue { get; set; }
722+
private Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? ExcludeValue { get; set; }
724723

725724
private Elastic.Clients.Elasticsearch.Aggregations.TermsAggregationExecutionHint? ExecutionHintValue { get; set; }
726725

@@ -780,7 +779,7 @@ public TermsAggregationDescriptor CollectMode(Elastic.Clients.Elasticsearch.Aggr
780779
return Self;
781780
}
782781

783-
public TermsAggregationDescriptor Exclude(IList<string>? exclude)
782+
public TermsAggregationDescriptor Exclude(Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? exclude)
784783
{
785784
ExcludeValue = exclude;
786785
return Self;
@@ -896,7 +895,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
896895
if (ExcludeValue is not null)
897896
{
898897
writer.WritePropertyName("exclude");
899-
SingleOrManySerializationHelper.Serialize<string>(ExcludeValue, writer, options);
898+
JsonSerializer.Serialize(writer, ExcludeValue, options);
900899
}
901900

902901
if (ExecutionHintValue is not null)

src/Elastic.Clients.Elasticsearch/_Generated/Types/TermsExcludeConverter.g.cs renamed to src/Elastic.Clients.Elasticsearch/_Generated/Types/Aggregations/TermsExclude.g.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@
2525
using System.Text.Json.Serialization;
2626

2727
#nullable restore
28-
namespace Elastic.Clients.Elasticsearch;
29-
internal sealed class TermsExcludeConverter : IEnumerableSingleOrManyConverter<string>
28+
namespace Elastic.Clients.Elasticsearch.Aggregations;
29+
public partial class TermsExclude : Union<string, IReadOnlyCollection<string>>
3030
{
31+
public TermsExclude(string termsExclude) : base(termsExclude)
32+
{
33+
}
34+
35+
public TermsExclude(IReadOnlyCollection<string> termsExclude) : base(termsExclude)
36+
{
37+
}
3138
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.Threading.Tasks;
6+
using Elastic.Clients.Elasticsearch.Types.Aggregations;
7+
using Tests.Serialization;
8+
using VerifyXunit;
9+
10+
namespace Tests.Types;
11+
12+
[UsesVerify]
13+
public class TermsExcludeSerializationTests : SerializerTestBase
14+
{
15+
[U]
16+
public async Task RoundTripSerialize_TermsExlucdeWithRegexPattern()
17+
{
18+
const string pattern = "water_.*";
19+
20+
var target = new TestClass
21+
{
22+
Exclude = new TermsExclude(pattern)
23+
};
24+
25+
var result = await RoundTripAndVerifyJsonAsync(target);
26+
27+
result.Exclude.RegexPattern.Should().Be(pattern);
28+
result.Exclude.Values.Should().BeNull();
29+
}
30+
31+
[U]
32+
public async Task RoundTripSerialize_TermsExlucdeWithValues()
33+
{
34+
var values = new[] { "term_a", "term_b" };
35+
36+
var target = new TestClass
37+
{
38+
Exclude = new TermsExclude(values)
39+
};
40+
41+
var result = await RoundTripAndVerifyJsonAsync(target);
42+
43+
result.Exclude.RegexPattern.Should().BeNull();
44+
result.Exclude.Values.Should().Contain(values);
45+
}
46+
47+
private class TestClass
48+
{
49+
public TermsExclude Exclude { get; set; }
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
exclude: water_.*
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
exclude: [
3+
term_a,
4+
term_b
5+
]
6+
}

0 commit comments

Comments
 (0)