Skip to content

Commit

Permalink
Final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
flobernd committed Feb 17, 2025
1 parent 22a2fac commit 2966103
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,10 @@ public IndexRequest(Elastic.Clients.Elasticsearch.IndexName index) : base(r => r
{
}

//[JsonConstructor]
//internal IndexRequest()
//{
//}
[JsonConstructor]
public IndexRequest() : this(typeof(TDocument))
{
}

internal override ApiUrls ApiUrls => ApiUrlLookup.NoNamespaceIndex;

Expand Down Expand Up @@ -352,4 +352,4 @@ protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions o
{
settings.SourceSerializer.Serialize(DocumentValue, writer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public TermVectorsRequest(Elastic.Clients.Elasticsearch.IndexName index) : base(
}

[JsonConstructor]
internal TermVectorsRequest()
public TermVectorsRequest() : this(typeof(TDocument))
{
}

Expand Down
3 changes: 0 additions & 3 deletions src/Elastic.Clients.Elasticsearch/_Shared/Api/IndexRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ namespace Elastic.Clients.Elasticsearch;

public partial class IndexRequest<TDocument>
{
[JsonConstructor]
public IndexRequest() : this(typeof(TDocument)) { }

public IndexRequest(TDocument document, Id id) : this(typeof(TDocument), id) => Document = document;

protected override HttpMethod? DynamicHttpMethod => GetHttpMethod(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// 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;

namespace Elastic.Clients.Elasticsearch.Serialization;

/// <summary>
/// A delegate that selects a union variant (e.g. based on the current JSON token type).
/// </summary>
/// <param name="reader">A reference to a <see cref="Utf8JsonReader"/> instance.</param>
/// <param name="options">The <see cref="JsonSerializerOptions"/> to use.</param>
/// <returns>The selected <see cref="UnionTag"/> value.</returns>
/// <remarks>
/// IMPORTANT:
/// The <see cref="Utf8JsonReader"/> is passed by reference for best performance. If the selector function
/// implementation needs to advance the reader position, it must always operate on a copy of the original
/// <paramref name="reader"/> or restore the reader state before returning.
/// </remarks>
internal delegate UnionTag JsonUnionSelectorFunc(ref Utf8JsonReader reader, JsonSerializerOptions options);

[Flags]
internal enum JsonTokenTypes
{
None = 0,
StartObject = 1 << JsonTokenType.StartObject,
EndObject = 1 << JsonTokenType.EndObject,
StartArray = 1 << JsonTokenType.StartArray,
EndArray = 1 << JsonTokenType.EndArray,
String = 1 << JsonTokenType.String | 1 << JsonTokenType.PropertyName,
Number = 1 << JsonTokenType.Number,
True = 1 << JsonTokenType.True,
False = 1 << JsonTokenType.False
}

internal static class JsonUnionSelector
{
public static UnionTag ByTokenType(ref Utf8JsonReader reader, JsonSerializerOptions options, JsonTokenTypes first, JsonTokenTypes second)
{
_ = reader;
_ = options;

if (((int)first & (1 << (int)reader.TokenType)) is not 0)
{
return UnionTag.T1;
}

if (((int)second & (1 << (int)reader.TokenType)) is not 0)
{
return UnionTag.T2;
}

return UnionTag.None;
}

public static UnionTag ByPropertyOfT1(ref Utf8JsonReader reader, JsonSerializerOptions options, string name)
{
reader.ValidateToken(JsonTokenType.StartObject);

var internalReader = reader;

while (internalReader.Read() && (internalReader.TokenType is JsonTokenType.PropertyName))
{
if (internalReader.ValueTextEquals(name))
{
return UnionTag.T1;
}

internalReader.Read();
internalReader.Skip();
}

return UnionTag.T2;
}

public static UnionTag ByPropertyOfT2(ref Utf8JsonReader reader, JsonSerializerOptions options, string name)
{
reader.ValidateToken(JsonTokenType.StartObject);

var internalReader = reader;

while (internalReader.Read() && (internalReader.TokenType is JsonTokenType.PropertyName))
{
if (internalReader.ValueTextEquals(name))
{
return UnionTag.T2;
}

internalReader.Read();
internalReader.Skip();
}

return UnionTag.T1;
}
}

0 comments on commit 2966103

Please sign in to comment.