Skip to content

Port TermsExclude type #6859

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 28, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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.

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Elastic.Clients.Elasticsearch.Types.Aggregations;

/// <summary>
/// Filters which terms to exclude from the response.
/// </summary>
[JsonConverter(typeof(TermsExcludeConverter))]
public sealed class TermsExclude
{
/// <summary>
/// Creates an instance of <see cref="TermsExclude" /> that uses a regular expression pattern
/// to determine the terms to exclude from the response.
/// </summary>
/// <param name="pattern">The regular expression pattern.</param>
public TermsExclude(string regexPattern) => RegexPattern = regexPattern;

/// <summary>
/// Creates an instance of <see cref="TermsExclude" /> that uses a collection of terms
/// to exclude from the response.
/// </summary>
/// <param name="values">The exact terms to exclude.</param>
public TermsExclude(ICollection<string> values) => Values = values;

/// <summary>
/// The regular expression pattern to determine terms to exclude from the response.
/// </summary>
public string? RegexPattern { get; }

/// <summary>
/// Collection of terms to exclude from the response.
/// </summary>
public ICollection<string>? Values { get; }
}

internal sealed class TermsExcludeConverter : JsonConverter<TermsExclude>
{
public override TermsExclude? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
reader.Read();
return null;
}

TermsExclude termsExclude;

switch (reader.TokenType)
{
case JsonTokenType.StartArray:
var terms = JsonSerializer.Deserialize<IList<string>>(ref reader, options);
termsExclude = new TermsExclude(terms);
break;
case JsonTokenType.String:
var regex = reader.GetString();
termsExclude = new TermsExclude(regex);
break;
default:
throw new JsonException($"Unexpected token {reader.TokenType} when deserializing {nameof(TermsExclude)}");
}

return termsExclude;
}

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

if (value.Values is not null)
{
JsonSerializer.Serialize<IEnumerable<string>>(writer, value.Values, options);
return;
}

writer.WriteStringValue(value.RegexPattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override TermsAggregation Read(ref Utf8JsonReader reader, Type typeToConv
if (reader.ValueTextEquals("exclude"))
{
reader.Read();
var value = SingleOrManySerializationHelper.Deserialize<string>(ref reader, options);
var value = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.Aggregations.TermsExclude?>(ref reader, options);
if (value is not null)
{
agg.Exclude = value;
Expand Down Expand Up @@ -267,7 +267,7 @@ public override void Write(Utf8JsonWriter writer, TermsAggregation value, JsonSe
if (value.Exclude is not null)
{
writer.WritePropertyName("exclude");
SingleOrManySerializationHelper.Serialize<string>(value.Exclude, writer, options);
JsonSerializer.Serialize(writer, value.Exclude, options);
}

if (value.ExecutionHint is not null)
Expand Down Expand Up @@ -377,8 +377,7 @@ internal TermsAggregation()

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

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

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

Expand Down Expand Up @@ -427,7 +426,7 @@ public TermsAggregationDescriptor() : base()

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

private IList<string>? ExcludeValue { get; set; }
private Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? ExcludeValue { get; set; }

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

Expand Down Expand Up @@ -487,7 +486,7 @@ public TermsAggregationDescriptor<TDocument> CollectMode(Elastic.Clients.Elastic
return Self;
}

public TermsAggregationDescriptor<TDocument> Exclude(IList<string>? exclude)
public TermsAggregationDescriptor<TDocument> Exclude(Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? exclude)
{
ExcludeValue = exclude;
return Self;
Expand Down Expand Up @@ -597,7 +596,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
if (ExcludeValue is not null)
{
writer.WritePropertyName("exclude");
SingleOrManySerializationHelper.Serialize<string>(ExcludeValue, writer, options);
JsonSerializer.Serialize(writer, ExcludeValue, options);
}

if (ExecutionHintValue is not null)
Expand Down Expand Up @@ -720,7 +719,7 @@ public TermsAggregationDescriptor() : base()

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

private IList<string>? ExcludeValue { get; set; }
private Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? ExcludeValue { get; set; }

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

Expand Down Expand Up @@ -780,7 +779,7 @@ public TermsAggregationDescriptor CollectMode(Elastic.Clients.Elasticsearch.Aggr
return Self;
}

public TermsAggregationDescriptor Exclude(IList<string>? exclude)
public TermsAggregationDescriptor Exclude(Elastic.Clients.Elasticsearch.Aggregations.TermsExclude? exclude)
{
ExcludeValue = exclude;
return Self;
Expand Down Expand Up @@ -896,7 +895,7 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
if (ExcludeValue is not null)
{
writer.WritePropertyName("exclude");
SingleOrManySerializationHelper.Serialize<string>(ExcludeValue, writer, options);
JsonSerializer.Serialize(writer, ExcludeValue, options);
}

if (ExecutionHintValue is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
using System.Text.Json.Serialization;

#nullable restore
namespace Elastic.Clients.Elasticsearch;
internal sealed class TermsExcludeConverter : IEnumerableSingleOrManyConverter<string>
namespace Elastic.Clients.Elasticsearch.Aggregations;
public partial class TermsExclude : Union<string, IReadOnlyCollection<string>>
{
public TermsExclude(string termsExclude) : base(termsExclude)
{
}

public TermsExclude(IReadOnlyCollection<string> termsExclude) : base(termsExclude)
{
}
}
51 changes: 51 additions & 0 deletions tests/Tests/Types/TermsExcludeSerializationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.

using System.Threading.Tasks;
using Elastic.Clients.Elasticsearch.Types.Aggregations;
using Tests.Serialization;
using VerifyXunit;

namespace Tests.Types;

[UsesVerify]
public class TermsExcludeSerializationTests : SerializerTestBase
{
[U]
public async Task RoundTripSerialize_TermsExlucdeWithRegexPattern()
{
const string pattern = "water_.*";

var target = new TestClass
{
Exclude = new TermsExclude(pattern)
};

var result = await RoundTripAndVerifyJsonAsync(target);

result.Exclude.RegexPattern.Should().Be(pattern);
result.Exclude.Values.Should().BeNull();
}

[U]
public async Task RoundTripSerialize_TermsExlucdeWithValues()
{
var values = new[] { "term_a", "term_b" };

var target = new TestClass
{
Exclude = new TermsExclude(values)
};

var result = await RoundTripAndVerifyJsonAsync(target);

result.Exclude.RegexPattern.Should().BeNull();
result.Exclude.Values.Should().Contain(values);
}

private class TestClass
{
public TermsExclude Exclude { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
exclude: water_.*
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
exclude: [
term_a,
term_b
]
}