-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathIds.cs
85 lines (65 loc) · 2.34 KB
/
Ids.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// 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.Diagnostics;
using System.Linq;
using System.Text.Json.Serialization;
using Elastic.Transport;
namespace Elastic.Clients.Elasticsearch;
[DebuggerDisplay("{DebugDisplay,nq}")]
[JsonConverter(typeof(IdsConverter))]
public partial class Ids : IUrlParameter, IEquatable<Ids>
{
private readonly IList<Id> _ids;
public Ids(IEnumerable<Id> ids) => _ids = ids.ToList();
public Ids(IList<Id> ids) => _ids = ids;
public Ids(IEnumerable<string> ids) => _ids = ids.Select(i => new Id(i)).ToList();
public Ids(string value)
{
if (!value.IsNullOrEmptyCommaSeparatedList(out var arr))
_ids = arr.Select(i => new Id(i)).ToList();
}
internal IList<Id> IdsToSerialize => _ids;
private string DebugDisplay => ((IUrlParameter)this).GetString(null);
public bool Equals(Ids other)
{
if (other == null)
return false;
if (_ids == null && other._ids == null)
return true;
if (_ids == null || other._ids == null)
return false;
return _ids.Count == other._ids.Count &&
_ids.OrderBy(id => id).SequenceEqual(other._ids.OrderBy(id => id));
}
string IUrlParameter.GetString(ITransportConfiguration? settings)
{
if (settings is not ElasticsearchClientSettings elasticsearchClientSettings)
{
throw new Exception("Unexpected settings type.");
}
return string.Join(",", _ids.Select(i => i.GetString(elasticsearchClientSettings)) ?? Enumerable.Empty<string>());
}
public override string ToString() => DebugDisplay;
public static implicit operator Ids(string value) =>
value.IsNullOrEmptyCommaSeparatedList(out var arr) ? null : new Ids(arr);
public static implicit operator Ids(string[] value) =>
value.IsEmpty() ? null : new Ids(value);
public override bool Equals(object obj) => obj is Ids other && Equals(other);
public override int GetHashCode()
{
if (_ids == null)
return 0;
unchecked
{
var hc = 0;
foreach (var id in _ids.OrderBy(id => id))
hc = hc * 17 + id.GetHashCode();
return hc;
}
}
public static bool operator ==(Ids left, Ids right) => Equals(left, right);
public static bool operator !=(Ids left, Ids right) => !Equals(left, right);
}