Skip to content

Generate types with "AdditionalProperty" behaviour #6584

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 6 commits into from
Jul 19, 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
Expand Up @@ -28,18 +28,72 @@ public partial class CoordsGeoBounds
{
[JsonInclude]
[JsonPropertyName("bottom")]
public double Bottom { get; init; }
public double Bottom { get; set; }

[JsonInclude]
[JsonPropertyName("left")]
public double Left { get; init; }
public double Left { get; set; }

[JsonInclude]
[JsonPropertyName("right")]
public double Right { get; init; }
public double Right { get; set; }

[JsonInclude]
[JsonPropertyName("top")]
public double Top { get; init; }
public double Top { get; set; }
}

public sealed partial class CoordsGeoBoundsDescriptor : SerializableDescriptorBase<CoordsGeoBoundsDescriptor>
{
internal CoordsGeoBoundsDescriptor(Action<CoordsGeoBoundsDescriptor> configure) => configure.Invoke(this);
public CoordsGeoBoundsDescriptor() : base()
{
}

private double BottomValue { get; set; }

private double LeftValue { get; set; }

private double RightValue { get; set; }

private double TopValue { get; set; }

public CoordsGeoBoundsDescriptor Bottom(double bottom)
{
BottomValue = bottom;
return Self;
}

public CoordsGeoBoundsDescriptor Left(double left)
{
LeftValue = left;
return Self;
}

public CoordsGeoBoundsDescriptor Right(double right)
{
RightValue = right;
return Self;
}

public CoordsGeoBoundsDescriptor Top(double top)
{
TopValue = top;
return Self;
}

protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
writer.WriteStartObject();
writer.WritePropertyName("bottom");
writer.WriteNumberValue(BottomValue);
writer.WritePropertyName("left");
writer.WriteNumberValue(LeftValue);
writer.WritePropertyName("right");
writer.WriteNumberValue(RightValue);
writer.WritePropertyName("top");
writer.WriteNumberValue(TopValue);
writer.WriteEndObject();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,126 @@
#nullable restore
namespace Elastic.Clients.Elasticsearch
{
internal sealed class GeoDistanceSortConverter : JsonConverter<GeoDistanceSort>
{
public override GeoDistanceSort Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
throw new JsonException("Unexpected JSON detected.");
var variant = new GeoDistanceSort();
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
if (reader.TokenType == JsonTokenType.PropertyName)
{
var property = reader.GetString();
if (property == "distance_type")
{
variant.DistanceType = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.GeoDistanceType?>(ref reader, options);
continue;
}

if (property == "ignore_unmapped")
{
variant.IgnoreUnmapped = JsonSerializer.Deserialize<bool?>(ref reader, options);
continue;
}

if (property == "mode")
{
variant.Mode = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.SortMode?>(ref reader, options);
continue;
}

if (property == "order")
{
variant.Order = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.SortOrder?>(ref reader, options);
continue;
}

if (property == "unit")
{
variant.Unit = JsonSerializer.Deserialize<Elastic.Clients.Elasticsearch.DistanceUnit?>(ref reader, options);
continue;
}

variant.Field = property;
reader.Read();
variant.Location = JsonSerializer.Deserialize<IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation>>(ref reader, options);
}
}

reader.Read();
return variant;
}

public override void Write(Utf8JsonWriter writer, GeoDistanceSort value, JsonSerializerOptions options)
{
writer.WriteStartObject();
if (value.Field is not null && value.Location is not null)
{
if (!options.TryGetClientSettings(out var settings))
{
throw new JsonException("Unable to retrive client settings for JsonSerializerOptions.");
}

var propertyName = settings.Inferrer.Field(value.Field);
writer.WritePropertyName(propertyName);
JsonSerializer.Serialize(writer, value.Location, options);
}

if (value.DistanceType is not null)
{
writer.WritePropertyName("distance_type");
JsonSerializer.Serialize(writer, value.DistanceType, options);
}

if (value.IgnoreUnmapped.HasValue)
{
writer.WritePropertyName("ignore_unmapped");
writer.WriteBooleanValue(value.IgnoreUnmapped.Value);
}

if (value.Mode is not null)
{
writer.WritePropertyName("mode");
JsonSerializer.Serialize(writer, value.Mode, options);
}

if (value.Order is not null)
{
writer.WritePropertyName("order");
JsonSerializer.Serialize(writer, value.Order, options);
}

if (value.Unit is not null)
{
writer.WritePropertyName("unit");
JsonSerializer.Serialize(writer, value.Unit, options);
}

writer.WriteEndObject();
}
}

[JsonConverter(typeof(GeoDistanceSortConverter))]
public partial class GeoDistanceSort : ISortOptionsVariant
{
[JsonInclude]
[JsonPropertyName("distance_type")]
public Elastic.Clients.Elasticsearch.GeoDistanceType? DistanceType { get; set; }

[JsonInclude]
[JsonPropertyName("field")]
public Elastic.Clients.Elasticsearch.Field Field { get; set; }

[JsonInclude]
[JsonPropertyName("ignore_unmapped")]
public bool? IgnoreUnmapped { get; set; }

[JsonInclude]
[JsonPropertyName("location")]
public IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> Location { get; set; }

[JsonInclude]
[JsonPropertyName("mode")]
public Elastic.Clients.Elasticsearch.SortMode? Mode { get; set; }
Expand Down Expand Up @@ -64,6 +174,10 @@ public GeoDistanceSortDescriptor() : base()

private Elastic.Clients.Elasticsearch.DistanceUnit? UnitValue { get; set; }

private Elastic.Clients.Elasticsearch.Field FieldValue { get; set; }

private IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> LocationValue { get; set; }

public GeoDistanceSortDescriptor<TDocument> DistanceType(Elastic.Clients.Elasticsearch.GeoDistanceType? distanceType)
{
DistanceTypeValue = distanceType;
Expand Down Expand Up @@ -94,9 +208,34 @@ public GeoDistanceSortDescriptor<TDocument> Unit(Elastic.Clients.Elasticsearch.D
return Self;
}

public GeoDistanceSortDescriptor<TDocument> Location(IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> location)
{
LocationValue = location;
return Self;
}

public GeoDistanceSortDescriptor<TDocument> Field(Elastic.Clients.Elasticsearch.Field field)
{
FieldValue = field;
return Self;
}

public GeoDistanceSortDescriptor<TDocument> Field<TValue>(Expression<Func<TDocument, TValue>> field)
{
FieldValue = field;
return Self;
}

protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
writer.WriteStartObject();
if (FieldValue is not null && LocationValue is not null)
{
var propertyName = settings.Inferrer.Field(FieldValue);
writer.WritePropertyName(propertyName);
JsonSerializer.Serialize(writer, LocationValue, options);
}

if (DistanceTypeValue is not null)
{
writer.WritePropertyName("distance_type");
Expand Down Expand Up @@ -148,6 +287,10 @@ public GeoDistanceSortDescriptor() : base()

private Elastic.Clients.Elasticsearch.DistanceUnit? UnitValue { get; set; }

private Elastic.Clients.Elasticsearch.Field FieldValue { get; set; }

private IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> LocationValue { get; set; }

public GeoDistanceSortDescriptor DistanceType(Elastic.Clients.Elasticsearch.GeoDistanceType? distanceType)
{
DistanceTypeValue = distanceType;
Expand Down Expand Up @@ -178,9 +321,40 @@ public GeoDistanceSortDescriptor Unit(Elastic.Clients.Elasticsearch.DistanceUnit
return Self;
}

public GeoDistanceSortDescriptor Location(IEnumerable<Elastic.Clients.Elasticsearch.GeoLocation> location)
{
LocationValue = location;
return Self;
}

public GeoDistanceSortDescriptor Field(Elastic.Clients.Elasticsearch.Field field)
{
FieldValue = field;
return Self;
}

public GeoDistanceSortDescriptor Field<TDocument, TValue>(Expression<Func<TDocument, TValue>> field)
{
FieldValue = field;
return Self;
}

public GeoDistanceSortDescriptor Field<TDocument>(Expression<Func<TDocument, object>> field)
{
FieldValue = field;
return Self;
}

protected override void Serialize(Utf8JsonWriter writer, JsonSerializerOptions options, IElasticsearchClientSettings settings)
{
writer.WriteStartObject();
if (FieldValue is not null && LocationValue is not null)
{
var propertyName = settings.Inferrer.Field(FieldValue);
writer.WritePropertyName(propertyName);
JsonSerializer.Serialize(writer, LocationValue, options);
}

if (DistanceTypeValue is not null)
{
writer.WritePropertyName("distance_type");
Expand Down
Loading