-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathFields.cs
160 lines (127 loc) · 4.8 KB
/
Fields.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;
using Elastic.Transport;
#if ELASTICSEARCH_SERVERLESS
namespace Elastic.Clients.Elasticsearch.Serverless;
#else
namespace Elastic.Clients.Elasticsearch;
#endif
[JsonConverter(typeof(FieldsConverter))]
[DebuggerDisplay("{DebugDisplay,nq}")]
public sealed class Fields : IUrlParameter, IEnumerable<Field>, IEquatable<Fields>
{
internal readonly List<Field> ListOfFields;
internal Fields() => ListOfFields = new List<Field>();
internal Fields(IEnumerable<Field> fieldNames) => ListOfFields = new List<Field>(fieldNames);
private string DebugDisplay =>
$"Count: {ListOfFields.Count} [" +
string.Join(",", ListOfFields.Select((t, i) => $"({i + 1}: {t?.DebugDisplay ?? "NULL"})")) + "]";
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public IEnumerator<Field> GetEnumerator() => ListOfFields.GetEnumerator();
public bool Equals(Fields other) => EqualsAllFields(ListOfFields, other?.ListOfFields);
string IUrlParameter.GetString(ITransportConfiguration? settings)
{
if (settings is not IElasticsearchClientSettings elasticsearchClientSettings)
{
throw new ArgumentNullException(nameof(settings),
$"Can not resolve {nameof(Fields)} if no {nameof(IElasticsearchClientSettings)} is provided");
}
return string.Join(",",
ListOfFields.Where(f => f != null).Select(f => ((IUrlParameter)f).GetString(elasticsearchClientSettings)));
}
public override string ToString() => DebugDisplay;
public static implicit operator Fields(string[] fields) =>
fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f)));
public static implicit operator Fields(string field) => field.IsNullOrEmptyCommaSeparatedList(out var split)
? null
: new Fields(split.Select(f => new Field(f)));
public static implicit operator Fields(Expression[] fields) =>
fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f)));
public static implicit operator Fields(Expression field) =>
field == null ? null : new Fields(new[] { new Field(field) });
public static implicit operator Fields(Field field) => field == null ? null : new Fields(new[] { field });
public static implicit operator Fields(PropertyInfo field) =>
field == null ? null : new Fields(new Field[] { field });
public static implicit operator Fields(PropertyInfo[] fields) =>
fields.IsEmpty() ? null : new Fields(fields.Select(f => new Field(f)));
public static implicit operator Fields(Field[] fields) => fields.IsEmpty() ? null : new Fields(fields);
public Fields And<T, TValue>(Expression<Func<T, TValue>> field, double? boost = null, string format = null)
where T : class
{
ListOfFields.Add(new Field(field, boost, format));
return this;
}
public Fields And(string field, double? boost = null, string format = null)
{
ListOfFields.Add(new Field(field, boost, format));
return this;
}
public Fields And(PropertyInfo property, double? boost = null)
{
ListOfFields.Add(new Field(property, boost));
return this;
}
public Fields And<T>(params Expression<Func<T, object>>[] fields) where T : class
{
ListOfFields.AddRange(fields.Select(f => new Field(f)));
return this;
}
public Fields And(params string[] fields)
{
ListOfFields.AddRange(fields.Select(f => new Field(f)));
return this;
}
public Fields And(params PropertyInfo[] properties)
{
ListOfFields.AddRange(properties.Select(f => new Field(f)));
return this;
}
public Fields And(params Field[] fields)
{
ListOfFields.AddRange(fields);
return this;
}
public static bool operator ==(Fields left, Fields right) => Equals(left, right);
public static bool operator !=(Fields left, Fields right) => !Equals(left, right);
public override bool Equals(object obj)
{
switch (obj)
{
case Fields f:
return Equals(f);
case string s:
return Equals(s);
case Field fn:
return Equals(fn);
case Field[] fns:
return Equals(fns);
case Expression e:
return Equals(e);
case Expression[] es:
return Equals(es);
default:
return false;
}
}
private static bool EqualsAllFields(IReadOnlyList<Field> thisTypes, IReadOnlyList<Field> otherTypes)
{
if (thisTypes == null && otherTypes == null)
return true;
if (thisTypes == null || otherTypes == null)
return false;
if (thisTypes.Count != otherTypes.Count)
return false;
return thisTypes.Count == otherTypes.Count && !thisTypes.Except(otherTypes).Any();
}
public override int GetHashCode() => ListOfFields.GetHashCode();
}