|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
5 | 5 | using System;
|
6 |
| -using System.Collections.Concurrent; |
7 | 6 | using System.Collections.Generic;
|
8 | 7 | using System.Diagnostics.CodeAnalysis;
|
9 | 8 | using System.Linq;
|
10 |
| -using System.Reflection; |
11 | 9 | using System.Runtime.ExceptionServices;
|
12 |
| -using System.Runtime.Serialization; |
13 |
| -using System.Text; |
14 | 10 | using System.Threading;
|
15 | 11 | using System.Threading.Tasks;
|
16 | 12 |
|
17 | 13 | namespace Elastic.Clients.Elasticsearch;
|
18 | 14 |
|
19 | 15 | internal static class Extensions
|
20 | 16 | {
|
21 |
| - private static readonly ConcurrentDictionary<string, object> EnumCache = new(); |
22 |
| - |
23 |
| - //internal static bool NotWritable(this Query q) => q == null || !q.IsWritable; |
24 |
| - |
25 |
| - //internal static bool NotWritable(this IEnumerable<Query> qs) => qs == null || qs.All(q => q.NotWritable()); |
26 |
| - |
27 |
| - internal static string ToEnumValue<T>(this T enumValue) where T : struct |
28 |
| - { |
29 |
| - var enumType = typeof(T); |
30 |
| - var name = Enum.GetName(enumType, enumValue); |
31 |
| - var enumMemberAttribute = enumType.GetField(name).GetCustomAttribute<EnumMemberAttribute>(); |
32 |
| - |
33 |
| - //if (enumMemberAttribute != null) |
34 |
| - //return enumMemberAttribute.Value; |
35 |
| - |
36 |
| - //var alternativeEnumMemberAttribute = enumType.GetField(name).GetCustomAttribute<AlternativeEnumMemberAttribute>(); |
37 |
| - |
38 |
| - return enumMemberAttribute != null |
39 |
| - ? enumMemberAttribute.Value |
40 |
| - : enumValue.ToString(); |
41 |
| - } |
42 |
| - |
43 |
| - internal static T? ToEnum<T>(this string str, StringComparison comparison = StringComparison.OrdinalIgnoreCase) where T : struct |
44 |
| - { |
45 |
| - if (str == null) |
46 |
| - return null; |
47 |
| - |
48 |
| - var enumType = typeof(T); |
49 |
| - var key = $"{enumType.Name}.{str}"; |
50 |
| - if (EnumCache.TryGetValue(key, out var value)) |
51 |
| - return (T)value; |
52 |
| - |
53 |
| - foreach (var name in Enum.GetNames(enumType)) |
54 |
| - { |
55 |
| - if (name.Equals(str, comparison)) |
56 |
| - { |
57 |
| - var v = (T)Enum.Parse(enumType, name, true); |
58 |
| - EnumCache.TryAdd(key, v); |
59 |
| - return v; |
60 |
| - } |
61 |
| - |
62 |
| - var enumFieldInfo = enumType.GetField(name); |
63 |
| - var enumMemberAttribute = enumFieldInfo.GetCustomAttribute<EnumMemberAttribute>(); |
64 |
| - if (enumMemberAttribute?.Value.Equals(str, comparison) ?? false) |
65 |
| - { |
66 |
| - var v = (T)Enum.Parse(enumType, name); |
67 |
| - EnumCache.TryAdd(key, v); |
68 |
| - return v; |
69 |
| - } |
70 |
| - |
71 |
| - //var alternativeEnumMemberAttribute = enumFieldInfo.GetCustomAttribute<AlternativeEnumMemberAttribute>(); |
72 |
| - //if (alternativeEnumMemberAttribute?.Value.Equals(str, comparison) ?? false) |
73 |
| - //{ |
74 |
| - // var v = (T)Enum.Parse(enumType, name); |
75 |
| - // EnumCache.TryAdd(key, v); |
76 |
| - // return v; |
77 |
| - //} |
78 |
| - } |
79 |
| - |
80 |
| - return null; |
81 |
| - } |
82 |
| - |
83 |
| - internal static TReturn InvokeOrDefault<T, TReturn>(this Func<T, TReturn> func, T @default) |
84 |
| - where T : class, TReturn where TReturn : class => |
85 |
| - func?.Invoke(@default) ?? @default; |
86 |
| - |
87 |
| - internal static TReturn InvokeOrDefault<T1, T2, TReturn>(this Func<T1, T2, TReturn> func, T1 @default, |
88 |
| - T2 param2) |
89 |
| - where T1 : class, TReturn where TReturn : class => |
90 |
| - func?.Invoke(@default, param2) ?? @default; |
91 |
| - |
92 |
| - internal static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> items, Func<T, TKey> property) => |
93 |
| - items.GroupBy(property).Select(x => x.First()); |
94 |
| - |
95 |
| - internal static string Utf8String(this byte[] bytes) => |
96 |
| - bytes == null ? null : Encoding.UTF8.GetString(bytes, 0, bytes.Length); |
97 |
| - |
98 |
| - internal static byte[] Utf8Bytes(this string s) => s.IsNullOrEmpty() ? null : Encoding.UTF8.GetBytes(s); |
99 |
| - |
100 |
| - internal static bool IsNullOrEmpty(this IndexName value) => value == null || value.GetHashCode() == 0; |
101 |
| - |
102 |
| - internal static bool IsNullable(this Type type) => |
103 |
| - type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>); |
104 |
| - |
105 |
| - internal static void ThrowIfNullOrEmpty(this string @object, string parameterName, string when = null) |
106 |
| - { |
107 |
| - @object.ThrowIfNull(parameterName, when); |
108 |
| - if (string.IsNullOrWhiteSpace(@object)) |
109 |
| - { |
110 |
| - throw new ArgumentException( |
111 |
| - "Argument can't be null or empty" + (when.IsNullOrEmpty() ? "" : " when " + when), parameterName); |
112 |
| - } |
113 |
| - } |
114 |
| - |
115 | 17 | // ReSharper disable once ParameterOnlyUsedForPreconditionCheck.Global
|
116 | 18 | internal static void ThrowIfEmpty<T>(this IEnumerable<T> @object, string parameterName)
|
117 | 19 | {
|
@@ -164,9 +66,6 @@ internal static IEnumerable<T> AddIfNotNull<T>(this IEnumerable<T> list, T other
|
164 | 66 | return l;
|
165 | 67 | }
|
166 | 68 |
|
167 |
| - internal static bool HasAny<T>(this IEnumerable<T> list, Func<T, bool> predicate) => |
168 |
| - list != null && list.Any(predicate); |
169 |
| - |
170 | 69 | internal static bool HasAny<T>(this IEnumerable<T> list) => list != null && list.Any();
|
171 | 70 |
|
172 | 71 | internal static bool IsNullOrEmpty<T>(this IEnumerable<T>? list)
|
@@ -195,7 +94,7 @@ internal static bool IsNullOrEmptyCommaSeparatedList(this string? value, [NotNul
|
195 | 94 | if (string.IsNullOrWhiteSpace(value))
|
196 | 95 | return true;
|
197 | 96 |
|
198 |
| - split = value.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries) |
| 97 | + split = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries) |
199 | 98 | .Where(t => !t.IsNullOrEmpty())
|
200 | 99 | .Select(t => t.Trim())
|
201 | 100 | .ToArray();
|
@@ -224,12 +123,6 @@ internal static void AddRangeIfNotNull<T>(this List<T> list, IEnumerable<T> item
|
224 | 123 | list.AddRange(item.Where(x => x != null));
|
225 | 124 | }
|
226 | 125 |
|
227 |
| - internal static Dictionary<TKey, TValue> NullIfNoKeys<TKey, TValue>(this Dictionary<TKey, TValue> dictionary) |
228 |
| - { |
229 |
| - var i = dictionary?.Count; |
230 |
| - return i.GetValueOrDefault(0) > 0 ? dictionary : null; |
231 |
| - } |
232 |
| - |
233 | 126 | internal static async Task ForEachAsync<TSource, TResult>(
|
234 | 127 | this IEnumerable<TSource> lazyList,
|
235 | 128 | Func<TSource, long, Task<TResult>> taskSelector,
|
@@ -297,14 +190,4 @@ long page
|
297 | 190 | additionalRateLimiter?.Release();
|
298 | 191 | }
|
299 | 192 | }
|
300 |
| - |
301 |
| - internal static bool NullOrEquals<T>(this T o, T other) |
302 |
| - { |
303 |
| - if (o == null && other == null) |
304 |
| - return true; |
305 |
| - if (o == null || other == null) |
306 |
| - return false; |
307 |
| - |
308 |
| - return o.Equals(other); |
309 |
| - } |
310 | 193 | }
|
0 commit comments