Skip to content

Commit 9d7101e

Browse files
authored
Fully implement and test DataStreamName(s) (#6367)
1 parent 7d6404d commit 9d7101e

File tree

23 files changed

+380
-41
lines changed

23 files changed

+380
-41
lines changed
Lines changed: 79 additions & 0 deletions
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(DataStreamNameConverter))]
14+
[DebuggerDisplay("{DebugDisplay,nq}")]
15+
public sealed class DataStreamName : IEquatable<DataStreamName>, IUrlParameter
16+
{
17+
internal DataStreamName(string index) => Name = index;
18+
19+
public string Name { get; }
20+
21+
internal string DebugDisplay => Name;
22+
23+
public static implicit operator DataStreamName(string dataStreamName) => new(dataStreamName);
24+
25+
public static bool operator ==(DataStreamName left, DataStreamName right) => Equals(left, right);
26+
27+
public static bool operator !=(DataStreamName left, DataStreamName right) => !Equals(left, right);
28+
29+
bool IEquatable<DataStreamName>.Equals(DataStreamName other) => EqualsMarker(other);
30+
31+
private bool EqualsString(string other) => !other.IsNullOrEmpty() && other.Equals(Name, StringComparison.Ordinal);
32+
33+
public override bool Equals(object obj) => obj is string s ? EqualsString(s) : obj is DataStreamName i && EqualsMarker(i);
34+
35+
string IUrlParameter.GetString(ITransportConfiguration? settings) => Name;
36+
37+
public override string ToString() => Name ?? string.Empty;
38+
39+
private bool EqualsMarker(DataStreamName other)
40+
{
41+
if (other == null)
42+
return false;
43+
44+
return EqualsString(other.Name);
45+
}
46+
47+
private static int TypeHashCode { get; } = typeof(DataStreamName).GetHashCode();
48+
49+
public override int GetHashCode()
50+
{
51+
unchecked
52+
{
53+
return (TypeHashCode * 23) ^ (Name.GetHashCode());
54+
}
55+
}
56+
}
57+
58+
internal sealed class DataStreamNameConverter : JsonConverter<DataStreamName>
59+
{
60+
public override DataStreamName? 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, DataStreamName value, JsonSerializerOptions options)
69+
{
70+
if (value is null || value.Name is null)
71+
{
72+
writer.WriteNullValue();
73+
return;
74+
}
75+
76+
writer.WriteStringValue(value.Name);
77+
}
78+
}
79+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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.Collections;
7+
using System.Collections.Generic;
8+
using System.Diagnostics;
9+
using System.Linq;
10+
using System.Text.Json;
11+
using System.Text.Json.Serialization;
12+
using Elastic.Transport;
13+
14+
namespace Elastic.Clients.Elasticsearch
15+
{
16+
[JsonConverter(typeof(DataStreamNamesConverter))]
17+
[DebuggerDisplay("{DebugDisplay,nq}")]
18+
public sealed class DataStreamNames : IUrlParameter, IEnumerable<DataStreamName>, IEquatable<DataStreamNames>
19+
{
20+
internal readonly IList<DataStreamName> Names;
21+
22+
internal DataStreamNames() => Names = new List<DataStreamName>();
23+
24+
internal DataStreamNames(IEnumerable<DataStreamName> dataStreamNames) => Names = dataStreamNames.ToList();
25+
26+
internal DataStreamNames(IList<DataStreamName> dataStreamNames) => Names = dataStreamNames;
27+
28+
private string DebugDisplay =>
29+
$"Count: {Names.Count} [" + string.Join(",", Names.Select((t, i) => $"({i + 1}: {t?.DebugDisplay ?? "NULL"})")) + "]";
30+
31+
public override string ToString() => string.Join(",", Names.Where(f => f is not null).Select(f => f.Name));
32+
33+
public static implicit operator DataStreamNames(string[] dataStreamNames) => dataStreamNames.IsEmpty() ? null : new DataStreamNames(dataStreamNames.Select(f => new DataStreamName(f)));
34+
35+
public static implicit operator DataStreamNames(string dataStreamName) => dataStreamName.IsNullOrEmptyCommaSeparatedList(out var split)
36+
? null
37+
: new DataStreamNames(split.Select(f => new DataStreamName(f)));
38+
39+
public static implicit operator DataStreamNames(DataStreamName dataStreamName) => dataStreamName == null ? null : new DataStreamNames(new[] { dataStreamName });
40+
41+
public static implicit operator DataStreamNames(DataStreamName[] dataStreamNames) => dataStreamNames.IsEmpty() ? null : new DataStreamNames(dataStreamNames);
42+
43+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
44+
45+
public IEnumerator<DataStreamName> GetEnumerator() => Names.GetEnumerator();
46+
47+
public static bool operator ==(DataStreamNames left, DataStreamNames right) => Equals(left, right);
48+
49+
public static bool operator !=(DataStreamNames left, DataStreamNames right) => !Equals(left, right);
50+
51+
public override bool Equals(object obj) =>
52+
obj switch
53+
{
54+
DataStreamNames f => Equals(f),
55+
string s => Equals(s),
56+
DataStreamName fn => Equals(fn),
57+
DataStreamName[] fns => Equals(fns),
58+
_ => false,
59+
};
60+
61+
public bool Equals(DataStreamNames other) => EqualsAllNames(Names, other.Names);
62+
63+
private static bool EqualsAllNames(IList<DataStreamName> thisTypes, IList<DataStreamName> otherTypes)
64+
{
65+
if (thisTypes is null && otherTypes is null)
66+
return true;
67+
68+
if (thisTypes is null || otherTypes is null)
69+
return false;
70+
71+
if (thisTypes.Count != otherTypes.Count)
72+
return false;
73+
74+
return !thisTypes.Except(otherTypes).Any();
75+
}
76+
77+
public override int GetHashCode()
78+
{
79+
var hashCodes = new List<int>(Names.Count);
80+
foreach (var item in Names)
81+
{
82+
hashCodes.Add(item.GetHashCode());
83+
}
84+
85+
hashCodes.Sort();
86+
87+
unchecked
88+
{
89+
var hash = 17;
90+
foreach (var hashCode in hashCodes)
91+
{
92+
hash = hash * 23 + hashCode;
93+
}
94+
return typeof(DataStreamName).GetHashCode() ^ hash;
95+
}
96+
}
97+
98+
string IUrlParameter.GetString(ITransportConfiguration? settings) => ToString();
99+
}
100+
101+
internal sealed class DataStreamNamesConverter : JsonConverter<DataStreamNames>
102+
{
103+
public override DataStreamNames? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
104+
{
105+
if (reader.TokenType != JsonTokenType.StartArray)
106+
throw new JsonException($"Expected {JsonTokenType.StartArray} token but read '{reader.TokenType}' token for DataStreamNames.");
107+
108+
return JsonSerializer.Deserialize<string[]>(ref reader, options);
109+
}
110+
111+
public override void Write(Utf8JsonWriter writer, DataStreamNames value, JsonSerializerOptions options)
112+
{
113+
if (value is null)
114+
{
115+
writer.WriteNullValue();
116+
return;
117+
}
118+
119+
JsonSerializer.Serialize(writer, value.Names, options);
120+
}
121+
}
122+
}

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Elastic.Clients.Elasticsearch/Common/Infer/Field/Field.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public bool Equals(Field other) => _type != null
115115
? other != null && _type == other._type && _comparisonValue.Equals(other._comparisonValue)
116116
: other != null && _comparisonValue.Equals(other._comparisonValue);
117117

118-
string IUrlParameter.GetString(ITransportConfiguration settings)
118+
string IUrlParameter.GetString(ITransportConfiguration? settings)
119119
{
120120
if (!(settings is IElasticsearchClientSettings ElasticsearchSettings))
121121
{

src/Elastic.Clients.Elasticsearch/Common/Infer/Fields/Fields.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public partial class Fields : IUrlParameter, IEnumerable<Field>, IEquatable<Fiel
125125

126126
public bool Equals(Fields other) => EqualsAllFields(ListOfFields, other?.ListOfFields);
127127

128-
string IUrlParameter.GetString(ITransportConfiguration settings)
128+
string IUrlParameter.GetString(ITransportConfiguration? settings)
129129
{
130130
if (settings is not IElasticsearchClientSettings elasticsearchClientSettings)
131131
{

src/Elastic.Clients.Elasticsearch/Common/Infer/Id/Id.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public bool Equals(Id other)
5757
};
5858
}
5959

60-
string IUrlParameter.GetString(ITransportConfiguration settings)
60+
string IUrlParameter.GetString(ITransportConfiguration? settings)
6161
{
6262
var elasticClientSettings = (IElasticsearchClientSettings)settings;
6363
return GetString(elasticClientSettings);

src/Elastic.Clients.Elasticsearch/Common/Infer/Id/Ids.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public bool Equals(Ids other)
3838
_ids.OrderBy(id => id).SequenceEqual(other._ids.OrderBy(id => id));
3939
}
4040

41-
string IUrlParameter.GetString(ITransportConfiguration settings) =>
41+
string IUrlParameter.GetString(ITransportConfiguration? settings) =>
4242
string.Join(",", _ids ?? Enumerable.Empty<string>());
4343

4444
public override string ToString() => DebugDisplay;

src/Elastic.Clients.Elasticsearch/Common/Infer/Indices/Indices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal Indices(IEnumerable<string> indices) : base(new ManyIndices(indices)) {
3737

3838
public override string ToString() => DebugDisplay;
3939

40-
string IUrlParameter.GetString(ITransportConfiguration settings) => Match(
40+
string IUrlParameter.GetString(ITransportConfiguration? settings) => Match(
4141
all => "_all",
4242
many =>
4343
{

src/Elastic.Clients.Elasticsearch/Common/Infer/Inferrer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ public class RelationName : IEquatable<RelationName>, IUrlParameter
353353

354354
public bool Equals(RelationName other) => EqualsMarker(other);
355355

356-
string IUrlParameter.GetString(ITransportConfiguration settings)
356+
string IUrlParameter.GetString(ITransportConfiguration? settings)
357357
{
358358
if (settings is not IElasticsearchClientSettings nestSettings)
359359
throw new ArgumentNullException(nameof(settings),

src/Elastic.Clients.Elasticsearch/Common/Infer/JoinFieldRouting/Routing.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public bool Equals(Routing other)
7878
return false;
7979
}
8080

81-
string IUrlParameter.GetString(ITransportConfiguration settings)
81+
string IUrlParameter.GetString(ITransportConfiguration? settings)
8282
{
8383
var ElasticsearchClientSettings = settings as IElasticsearchClientSettings;
8484
return GetString(ElasticsearchClientSettings);

0 commit comments

Comments
 (0)