Skip to content

Refactoring and tiny behavior fix for Ids #7730

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 2 commits into from
Jul 10, 2023
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
42 changes: 1 addition & 41 deletions src/Elastic.Clients.Elasticsearch/Core/Infer/Id/Ids.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using Elastic.Transport;

Expand All @@ -22,7 +21,7 @@ public partial class Ids : IUrlParameter, IEquatable<Ids>

public Ids(IList<Id> ids) => _ids = ids;

public Ids(IEnumerable<string> ids) => _ids = ids?.Select(i => new Id(i)).ToList();
public Ids(IEnumerable<string> ids) => _ids = ids.Select(i => new Id(i)).ToList();

public Ids(string value)
{
Expand Down Expand Up @@ -84,42 +83,3 @@ public override int GetHashCode()

public static bool operator !=(Ids left, Ids right) => !Equals(left, right);
}

internal sealed class IdsConverter : JsonConverter<Ids>
{
public override Ids? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray)
throw new JsonException($"Unexpected JSON token. Expected {JsonTokenType.StartArray} but read {reader.TokenType}");

var ids = new List<Id>();

while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
var id = JsonSerializer.Deserialize<Id>(ref reader, options);

if (id is not null)
ids.Add(id);
}

return new Ids(ids);
}

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

writer.WriteStartArray();

foreach (var id in value.IdsToSerialize)
{
JsonSerializer.Serialize<Id>(writer, id, options);
}

writer.WriteEndArray();
}
}
49 changes: 49 additions & 0 deletions src/Elastic.Clients.Elasticsearch/Core/Infer/Id/IdsConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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;

internal sealed class IdsConverter : JsonConverter<Ids>
{
public override Ids? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartArray)
throw new JsonException($"Unexpected JSON token. Expected {JsonTokenType.StartArray} but read {reader.TokenType}");

var ids = new List<Id>();

while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
var id = JsonSerializer.Deserialize<Id>(ref reader, options);

if (id is not null)
ids.Add(id);
}

return new Ids(ids);
}

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

writer.WriteStartArray();

foreach (var id in value.IdsToSerialize)
{
JsonSerializer.Serialize<Id>(writer, id, options);
}

writer.WriteEndArray();
}
}