Skip to content

Commit b7dd9cf

Browse files
authored
Respond to data schema improvements (#6465)
* Respond to most date changes * Support spec changes such as stringified
1 parent 94a2314 commit b7dd9cf

File tree

254 files changed

+1628
-1490
lines changed

Some content is hidden

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

254 files changed

+1628
-1490
lines changed

src/Elastic.Clients.Elasticsearch/Api/AsyncSearch/AsyncSearchStatusResponse.cs

-17
This file was deleted.

src/Elastic.Clients.Elasticsearch/Api/AsyncSearch/GetAsyncSearchResponse.cs

-17
This file was deleted.

src/Elastic.Clients.Elasticsearch/Common/TimeUnit/Time.cs renamed to src/Elastic.Clients.Elasticsearch/Common/DateTime/Duration.cs

+33-33
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
namespace Elastic.Clients.Elasticsearch;
1313

1414
/// <summary>
15-
/// Represents a time value
15+
/// Represents a duration value.
1616
/// </summary>
17-
[JsonConverter(typeof(TimeConverter))]
18-
public sealed class Time : IComparable<Time>, IEquatable<Time>, IUrlParameter
17+
[JsonConverter(typeof(DurationConverter))]
18+
public sealed class Duration : IComparable<Duration>, IEquatable<Duration>, IUrlParameter
1919
{
2020
private const double MicrosecondsInATick = 0.1; // 10 ticks = 1 microsecond
2121
private const double MillisecondsInADay = MillisecondsInAnHour * 24;
@@ -40,18 +40,18 @@ public sealed class Time : IComparable<Time>, IEquatable<Time>, IUrlParameter
4040

4141
private static readonly double FLOAT_TOLERANCE = 1e-7; // less than 1 nanosecond
4242

43-
private Time(int specialFactor, bool specialValue)
43+
private Duration(int specialFactor, bool specialValue)
4444
{
4545
if (!specialValue)
4646
throw new ArgumentException("this constructor is only for static TimeValues");
4747

4848
StaticTimeValue = specialFactor;
4949
}
5050

51-
public Time(TimeSpan timeSpan)
51+
public Duration(TimeSpan timeSpan)
5252
: this(timeSpan.TotalMilliseconds) { }
5353

54-
public Time(double milliseconds)
54+
public Duration(double milliseconds)
5555
{
5656
if (Math.Abs(milliseconds - -1) < FLOAT_TOLERANCE)
5757
StaticTimeValue = -1;
@@ -61,14 +61,14 @@ public Time(double milliseconds)
6161
Reduce(milliseconds);
6262
}
6363

64-
public Time(double factor, TimeUnit interval)
64+
public Duration(double factor, TimeUnit interval)
6565
{
6666
Factor = factor;
6767
Interval = interval;
6868
Milliseconds = GetExactMilliseconds(Factor.Value, Interval.Value);
6969
}
7070

71-
public Time(string timeUnit)
71+
public Duration(string timeUnit)
7272
{
7373
if (timeUnit.IsNullOrEmpty())
7474
throw new ArgumentException("Expression string is empty", nameof(timeUnit));
@@ -85,17 +85,17 @@ public Time(string timeUnit)
8585

8686
public double? Milliseconds { get; private set; }
8787

88-
public static Time MinusOne { get; } = new Time(-1, true);
88+
public static Duration MinusOne { get; } = new Duration(-1, true);
8989

90-
public static Time Zero { get; } = new Time(0, true);
90+
public static Duration Zero { get; } = new Duration(0, true);
9191

9292
private int? StaticTimeValue { get; }
9393

94-
public static implicit operator Time(TimeSpan span) => new(span);
94+
public static implicit operator Duration(TimeSpan span) => new(span);
9595

96-
public static implicit operator Time(double milliseconds) => new(milliseconds);
96+
public static implicit operator Duration(double milliseconds) => new(milliseconds);
9797

98-
public static implicit operator Time(string expression) => new(expression);
98+
public static implicit operator Duration(string expression) => new(expression);
9999

100100
private void ParseExpression(string timeUnit)
101101
{
@@ -147,7 +147,7 @@ private void ParseExpression(string timeUnit)
147147
Milliseconds = GetExactMilliseconds(Factor.Value, Interval.Value);
148148
}
149149

150-
public int CompareTo(Time other)
150+
public int CompareTo(Duration other)
151151
{
152152
if (other == null)
153153
return 1;
@@ -182,21 +182,21 @@ public int CompareTo(Time other)
182182

183183
private static bool IsIntegerGreaterThanZero(double d) => Math.Abs(d % 1) < double.Epsilon;
184184

185-
public static bool operator <(Time left, Time right) => left.CompareTo(right) < 0;
185+
public static bool operator <(Duration left, Duration right) => left.CompareTo(right) < 0;
186186

187-
public static bool operator <=(Time left, Time right) => left.CompareTo(right) < 0 || left.Equals(right);
187+
public static bool operator <=(Duration left, Duration right) => left.CompareTo(right) < 0 || left.Equals(right);
188188

189-
public static bool operator >(Time left, Time right) => left.CompareTo(right) > 0;
189+
public static bool operator >(Duration left, Duration right) => left.CompareTo(right) > 0;
190190

191-
public static bool operator >=(Time left, Time right) => left.CompareTo(right) > 0 || left.Equals(right);
191+
public static bool operator >=(Duration left, Duration right) => left.CompareTo(right) > 0 || left.Equals(right);
192192

193-
public static bool operator ==(Time left, Time right) =>
193+
public static bool operator ==(Duration left, Duration right) =>
194194
ReferenceEquals(left, null) ? right is null : left.Equals(right);
195195

196-
public static bool operator !=(Time left, Time right) => !(left == right);
196+
public static bool operator !=(Duration left, Duration right) => !(left == right);
197197

198198
/// <summary>
199-
/// Converts this instance of <see cref="Time" /> to an instance of <see cref="TimeSpan" />.
199+
/// Converts this instance of <see cref="Duration" /> to an instance of <see cref="TimeSpan" />.
200200
/// For values in <see cref="TimeUnit.Microseconds" /> and <see cref="TimeUnit.Nanoseconds" />, value will be rounded to
201201
/// the nearest Tick.
202202
/// All other values will be rounded to the nearest Millisecond.
@@ -206,7 +206,7 @@ public int CompareTo(Time other)
206206
/// special time values <see cref="MinusOne" /> and <see cref="Zero" /> do not have a <see cref="TimeSpan" />
207207
/// representation.
208208
/// </para>
209-
/// <para>instance of <see cref="Time" /> has no value for <see cref="Interval" /></para>
209+
/// <para>instance of <see cref="Duration" /> has no value for <see cref="Interval" /></para>
210210
/// </exception>
211211
public TimeSpan ToTimeSpan()
212212
{
@@ -256,7 +256,7 @@ string IUrlParameter.GetString(ITransportConfiguration config)
256256
return Milliseconds.ToString();
257257
}
258258

259-
public bool Equals(Time other)
259+
public bool Equals(Duration other)
260260
{
261261
if (ReferenceEquals(null, other))
262262
return false;
@@ -286,7 +286,7 @@ public override bool Equals(object obj)
286286
if (obj.GetType() != GetType())
287287
return false;
288288

289-
return Equals((Time)obj);
289+
return Equals((Duration)obj);
290290
}
291291

292292
public override int GetHashCode() => StaticTimeValue.HasValue
@@ -392,33 +392,33 @@ private static string ExponentFormat(double d)
392392
}
393393
}
394394

395-
internal sealed class TimeConverter : JsonConverter<Time>
395+
internal sealed class DurationConverter : JsonConverter<Duration>
396396
{
397-
public override Time? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
397+
public override Duration? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
398398
{
399399
var token = reader.TokenType;
400400

401401
switch (token)
402402
{
403403
case JsonTokenType.String:
404-
return new Time(reader.GetString());
404+
return new Duration(reader.GetString());
405405
case JsonTokenType.Number:
406406
var milliseconds = reader.GetInt64();
407407
if (milliseconds == -1)
408-
return Time.MinusOne;
408+
return Duration.MinusOne;
409409
if (milliseconds == 0)
410-
return Time.Zero;
411-
return new Time(milliseconds);
410+
return Duration.Zero;
411+
return new Duration(milliseconds);
412412
default:
413413
return null;
414414
}
415415
}
416416

417-
public override void Write(Utf8JsonWriter writer, Time value, JsonSerializerOptions options)
417+
public override void Write(Utf8JsonWriter writer, Duration value, JsonSerializerOptions options)
418418
{
419-
if (value == Time.MinusOne)
419+
if (value == Duration.MinusOne)
420420
writer.WriteNumberValue(-1);
421-
else if (value == Time.Zero)
421+
else if (value == Duration.Zero)
422422
writer.WriteNumberValue(0);
423423
else if (value.Factor.HasValue && value.Interval.HasValue)
424424
writer.WriteStringValue(value.ToString());

src/Elastic.Clients.Elasticsearch/Common/DateTimeUtil.cs

-43
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,3 @@ internal static class DateTimeUtil
1212
{
1313
public static readonly DateTimeOffset UnixEpoch = new(1970, 1, 1, 0, 0, 0, 0, TimeSpan.Zero);
1414
}
15-
16-
[JsonConverter(typeof(EpochMillisConverter))]
17-
public readonly struct EpochMillis
18-
{
19-
public static EpochMillis Zero = new(0);
20-
21-
public EpochMillis() => MillisecondsSinceEpoch = 0;
22-
23-
public EpochMillis(long millisecondsSinceEpoch) => MillisecondsSinceEpoch = millisecondsSinceEpoch;
24-
25-
public static EpochMillis Parse(string millisecondsSinceEpoch)
26-
{
27-
if (!long.TryParse(millisecondsSinceEpoch, out var parsedValue))
28-
throw new InvalidOperationException($"Unable to parse value '{millisecondsSinceEpoch}' to epoch milliseconds.");
29-
30-
return new EpochMillis(parsedValue);
31-
}
32-
33-
public long MillisecondsSinceEpoch { get; private init; }
34-
35-
public DateTimeOffset DateTimeOffset => DateTimeUtil.UnixEpoch.AddMilliseconds(MillisecondsSinceEpoch);
36-
}
37-
38-
internal sealed class EpochMillisConverter : JsonConverter<EpochMillis>
39-
{
40-
public override EpochMillis Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
41-
{
42-
if (reader.TokenType == JsonTokenType.String)
43-
{
44-
var valueAsString = reader.GetString();
45-
return EpochMillis.Parse(valueAsString);
46-
}
47-
else if (reader.TokenType == JsonTokenType.Number)
48-
{
49-
var value = reader.GetInt64();
50-
return new EpochMillis(value);
51-
}
52-
53-
throw new JsonException("Value could not be parsed to EpochMillis");
54-
}
55-
56-
public override void Write(Utf8JsonWriter writer, EpochMillis value, JsonSerializerOptions options) => writer.WriteNumberValue(value.MillisecondsSinceEpoch);
57-
}

src/Elastic.Clients.Elasticsearch/Common/UrlParameters/DataStreamNames/DataStreamName.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Elastic.Clients.Elasticsearch
1414
[DebuggerDisplay("{DebugDisplay,nq}")]
1515
public sealed class DataStreamName : IEquatable<DataStreamName>, IUrlParameter
1616
{
17-
internal DataStreamName(string index) => Name = index;
17+
internal DataStreamName(string dataStreamName) => Name = dataStreamName;
1818

1919
public string Name { get; }
2020

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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.Diagnostics;
7+
using System.Text.Json;
8+
using System.Text.Json.Serialization;
9+
using Elastic.Transport;
10+
11+
namespace Elastic.Clients.Elasticsearch
12+
{
13+
[JsonConverter(typeof(ScrollIdConverter))]
14+
[DebuggerDisplay("{DebugDisplay,nq}")]
15+
public sealed class ScrollId : IEquatable<ScrollId>, IUrlParameter
16+
{
17+
internal ScrollId(string scrollId) => Id = scrollId;
18+
19+
public string Id { get; }
20+
21+
internal string DebugDisplay => Id;
22+
23+
public static implicit operator ScrollId(string scrollId) => new(scrollId);
24+
25+
public static bool operator ==(ScrollId left, ScrollId right) => Equals(left, right);
26+
27+
public static bool operator !=(ScrollId left, ScrollId right) => !Equals(left, right);
28+
29+
bool IEquatable<ScrollId>.Equals(ScrollId other) => EqualsMarker(other);
30+
31+
private bool EqualsString(string other) => !other.IsNullOrEmpty() && other.Equals(Id, StringComparison.Ordinal);
32+
33+
public override bool Equals(object obj) => obj is string s ? EqualsString(s) : obj is ScrollId i && EqualsMarker(i);
34+
35+
string IUrlParameter.GetString(ITransportConfiguration? settings) => Id;
36+
37+
public override string ToString() => Id ?? string.Empty;
38+
39+
private bool EqualsMarker(ScrollId other)
40+
{
41+
if (other == null)
42+
return false;
43+
44+
return EqualsString(other.Id);
45+
}
46+
47+
private static int TypeHashCode { get; } = typeof(ScrollId).GetHashCode();
48+
49+
public override int GetHashCode()
50+
{
51+
unchecked
52+
{
53+
return (TypeHashCode * 23) ^ (Id.GetHashCode());
54+
}
55+
}
56+
}
57+
58+
internal sealed class ScrollIdConverter : JsonConverter<ScrollId>
59+
{
60+
public override ScrollId? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
61+
{
62+
if (reader.TokenType != JsonTokenType.String)
63+
throw new JsonException($"Unexpected token '{reader.TokenType}' for DataStreamName");
64+
65+
return reader.GetString();
66+
}
67+
68+
public override void Write(Utf8JsonWriter writer, ScrollId value, JsonSerializerOptions options)
69+
{
70+
if (value is null || value.Id is null)
71+
{
72+
writer.WriteNullValue();
73+
return;
74+
}
75+
76+
writer.WriteStringValue(value.Id);
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)