Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class DescriptorPromiseBase<TDescriptor, TValue> : IDescriptor,
{
internal readonly TValue PromisedValue;

protected DescriptorPromiseBase(TValue instance)
internal DescriptorPromiseBase(TValue instance)
{
PromisedValue = instance;
Self = (TDescriptor)this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public PropertyName(PropertyInfo property)

string IUrlParameter.GetString(ITransportConfiguration settings)
{
if (!(settings is IElasticsearchClientSettings elasticsearchSettings))
if (settings is not IElasticsearchClientSettings elasticsearchSettings)
{
throw new ArgumentNullException(nameof(settings),
$"Can not resolve {nameof(PropertyName)} if no {nameof(IElasticsearchClientSettings)} is provided");
Expand Down
216 changes: 109 additions & 107 deletions src/Elastic.Clients.Elasticsearch/Common/IsADictionaryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,157 +8,159 @@
using System.ComponentModel;
using System.Linq;

namespace Elastic.Clients.Elasticsearch
{
namespace Elastic.Clients.Elasticsearch;

public interface IIsADictionary { }

public interface IIsADictionary { }
public interface IIsADictionary<TKey, TValue> : IDictionary<TKey, TValue>, IIsADictionary { }

public interface IIsADictionary<TKey, TValue> : IDictionary<TKey, TValue>, IIsADictionary { }
/// <summary>
/// A base dictionary type for internally tagged unions.
/// </summary>
/// <typeparam name="TKey"></typeparam>
/// <typeparam name="TValue"></typeparam>

public abstract class IsADictionaryBase<TKey, TValue> : IIsADictionary<TKey, TValue>
public abstract class IsADictionaryBase<TKey, TValue> : IIsADictionary<TKey, TValue>
{
internal IsADictionaryBase() => BackingDictionary = new Dictionary<TKey, TValue>();

internal IsADictionaryBase(IDictionary<TKey, TValue> backingDictionary)
{
protected IsADictionaryBase() => BackingDictionary = new Dictionary<TKey, TValue>();
if (backingDictionary != null)
foreach (var key in backingDictionary.Keys)
ValidateKey(Sanitize(key));

protected IsADictionaryBase(IDictionary<TKey, TValue> backingDictionary)
{
// ReSharper disable VirtualMemberCallInConstructor
if (backingDictionary != null)
foreach (var key in backingDictionary.Keys)
ValidateKey(Sanitize(key));
// ReSharper enable VirtualMemberCallInConstructor
BackingDictionary = backingDictionary != null
? new Dictionary<TKey, TValue>(backingDictionary)
: new Dictionary<TKey, TValue>();
}

BackingDictionary = backingDictionary != null
? new Dictionary<TKey, TValue>(backingDictionary)
: new Dictionary<TKey, TValue>();
}
public TValue this[TKey key]
{
get => BackingDictionary[Sanitize(key)];
set => BackingDictionary[ValidateKey(Sanitize(key))] = value;
}

public TValue this[TKey key]
{
get => BackingDictionary[Sanitize(key)];
set => BackingDictionary[ValidateKey(Sanitize(key))] = value;
}
internal Dictionary<TKey, TValue> BackingDictionary { get; }
int ICollection<KeyValuePair<TKey, TValue>>.Count => BackingDictionary.Count;
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => Self.IsReadOnly;

protected Dictionary<TKey, TValue> BackingDictionary { get; }
int ICollection<KeyValuePair<TKey, TValue>>.Count => BackingDictionary.Count;
bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => Self.IsReadOnly;
TValue IDictionary<TKey, TValue>.this[TKey key]
{
get => BackingDictionary[Sanitize(key)];
set => BackingDictionary[ValidateKey(Sanitize(key))] = value;
}

TValue IDictionary<TKey, TValue>.this[TKey key]
{
get => BackingDictionary[Sanitize(key)];
set => BackingDictionary[ValidateKey(Sanitize(key))] = value;
}
ICollection<TKey> IDictionary<TKey, TValue>.Keys => BackingDictionary.Keys;
private ICollection<KeyValuePair<TKey, TValue>> Self => BackingDictionary;
ICollection<TValue> IDictionary<TKey, TValue>.Values => BackingDictionary.Values;

ICollection<TKey> IDictionary<TKey, TValue>.Keys => BackingDictionary.Keys;
private ICollection<KeyValuePair<TKey, TValue>> Self => BackingDictionary;
ICollection<TValue> IDictionary<TKey, TValue>.Values => BackingDictionary.Values;
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
ValidateKey(Sanitize(item.Key));
Self.Add(item);
}

void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
ValidateKey(Sanitize(item.Key));
Self.Add(item);
}
void ICollection<KeyValuePair<TKey, TValue>>.Clear() => BackingDictionary.Clear();

void ICollection<KeyValuePair<TKey, TValue>>.Clear() => BackingDictionary.Clear();
[EditorBrowsable(EditorBrowsableState.Never)]
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) => Self.Contains(item);

[EditorBrowsable(EditorBrowsableState.Never)]
bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) => Self.Contains(item);
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) => Self.CopyTo(array, arrayIndex);

void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) => Self.CopyTo(array, arrayIndex);
bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) => Self.Remove(item);

bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) => Self.Remove(item);
void IDictionary<TKey, TValue>.Add(TKey key, TValue value) => BackingDictionary.Add(ValidateKey(Sanitize(key)), value);

void IDictionary<TKey, TValue>.Add(TKey key, TValue value) => BackingDictionary.Add(ValidateKey(Sanitize(key)), value);
[EditorBrowsable(EditorBrowsableState.Never)]
bool IDictionary<TKey, TValue>.ContainsKey(TKey key) => BackingDictionary.ContainsKey(Sanitize(key));

[EditorBrowsable(EditorBrowsableState.Never)]
bool IDictionary<TKey, TValue>.ContainsKey(TKey key) => BackingDictionary.ContainsKey(Sanitize(key));
bool IDictionary<TKey, TValue>.Remove(TKey key) => BackingDictionary.Remove(Sanitize(key));

bool IDictionary<TKey, TValue>.Remove(TKey key) => BackingDictionary.Remove(Sanitize(key));
bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value) => BackingDictionary.TryGetValue(Sanitize(key), out value);

bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value) => BackingDictionary.TryGetValue(Sanitize(key), out value);
IEnumerator IEnumerable.GetEnumerator() => BackingDictionary.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => BackingDictionary.GetEnumerator();
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => BackingDictionary.GetEnumerator();

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => BackingDictionary.GetEnumerator();
protected virtual TKey ValidateKey(TKey key) => key;

protected virtual TKey ValidateKey(TKey key) => key;
protected virtual TKey Sanitize(TKey key) => key;
}

protected virtual TKey Sanitize(TKey key) => key;
}
public abstract class IsADictionaryDescriptorBase<TDescriptor, TPromised, TKey, TValue>
: DescriptorPromiseBase<TDescriptor, TPromised>
where TDescriptor : IsADictionaryDescriptorBase<TDescriptor, TPromised, TKey, TValue>
where TPromised : class, IIsADictionary<TKey, TValue>
{
protected IsADictionaryDescriptorBase(TPromised instance) : base(instance) { }

public abstract class IsADictionaryDescriptorBase<TDescriptor, TPromised, TKey, TValue>
: DescriptorPromiseBase<TDescriptor, TPromised>
where TDescriptor : IsADictionaryDescriptorBase<TDescriptor, TPromised, TKey, TValue>
where TPromised : class, IIsADictionary<TKey, TValue>
protected TDescriptor Assign(TKey key, TValue value)
{
protected IsADictionaryDescriptorBase(TPromised instance) : base(instance) { }

protected TDescriptor Assign(TKey key, TValue value)
{
PromisedValue.Add(key, value);
return Self;
}
PromisedValue.Add(key, value);
return Self;
}
}

public interface IIsAReadOnlyDictionary { }
public interface IIsAReadOnlyDictionary { }

public interface IIsAReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>, IIsAReadOnlyDictionary { }
public interface IIsAReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>, IIsAReadOnlyDictionary { }

public abstract class IsAReadOnlyDictionaryBase<TKey, TValue> : IIsAReadOnlyDictionary<TKey, TValue>
public abstract class IsAReadOnlyDictionaryBase<TKey, TValue> : IIsAReadOnlyDictionary<TKey, TValue>
{
protected IsAReadOnlyDictionaryBase(IReadOnlyDictionary<TKey, TValue> backingDictionary)
{
protected IsAReadOnlyDictionaryBase(IReadOnlyDictionary<TKey, TValue> backingDictionary)
{
if (backingDictionary == null)
return;
if (backingDictionary == null)
return;

var dictionary = new Dictionary<TKey, TValue>(backingDictionary.Count);
foreach (var key in backingDictionary.Keys)
// ReSharper disable once VirtualMemberCallInConstructor
// expect all implementations of Sanitize to be pure
dictionary[Sanitize(key)] = backingDictionary[key];
var dictionary = new Dictionary<TKey, TValue>(backingDictionary.Count);
foreach (var key in backingDictionary.Keys)
// ReSharper disable once VirtualMemberCallInConstructor
// expect all implementations of Sanitize to be pure
dictionary[Sanitize(key)] = backingDictionary[key];

BackingDictionary = dictionary;
}
BackingDictionary = dictionary;
}

public int Count => BackingDictionary.Count;
public int Count => BackingDictionary.Count;

public TValue this[TKey key] => BackingDictionary[key];
public TValue this[TKey key] => BackingDictionary[key];

public IEnumerable<TKey> Keys => BackingDictionary.Keys;
public IEnumerable<TKey> Keys => BackingDictionary.Keys;

public IEnumerable<TValue> Values => BackingDictionary.Values;
protected internal IReadOnlyDictionary<TKey, TValue> BackingDictionary { get; } = EmptyReadOnly<TKey, TValue>.Dictionary;
public IEnumerable<TValue> Values => BackingDictionary.Values;
protected internal IReadOnlyDictionary<TKey, TValue> BackingDictionary { get; } = EmptyReadOnly<TKey, TValue>.Dictionary;

IEnumerator IEnumerable.GetEnumerator() => BackingDictionary.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => BackingDictionary.GetEnumerator();

IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() =>
BackingDictionary.GetEnumerator();
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() =>
BackingDictionary.GetEnumerator();

public bool ContainsKey(TKey key) => BackingDictionary.ContainsKey(key);
public bool ContainsKey(TKey key) => BackingDictionary.ContainsKey(key);

public bool TryGetValue(TKey key, out TValue value) =>
BackingDictionary.TryGetValue(key, out value);
public bool TryGetValue(TKey key, out TValue value) =>
BackingDictionary.TryGetValue(key, out value);

protected virtual TKey Sanitize(TKey key) => key;
}
protected virtual TKey Sanitize(TKey key) => key;
}

internal static class EmptyReadOnlyExtensions
{
public static IReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> enumerable) =>
enumerable == null ? EmptyReadOnly<T>.Collection : new ReadOnlyCollection<T>(enumerable.ToList());
internal static class EmptyReadOnlyExtensions
{
public static IReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> enumerable) =>
enumerable == null ? EmptyReadOnly<T>.Collection : new ReadOnlyCollection<T>(enumerable.ToList());

public static IReadOnlyCollection<T> ToReadOnlyCollection<T>(this IList<T> enumerable) =>
enumerable == null || enumerable.Count == 0 ? EmptyReadOnly<T>.Collection : new ReadOnlyCollection<T>(enumerable);
}
public static IReadOnlyCollection<T> ToReadOnlyCollection<T>(this IList<T> enumerable) =>
enumerable == null || enumerable.Count == 0 ? EmptyReadOnly<T>.Collection : new ReadOnlyCollection<T>(enumerable);
}


internal static class EmptyReadOnly<TElement>
{
public static readonly IReadOnlyCollection<TElement> Collection = new ReadOnlyCollection<TElement>(new TElement[0]);
public static readonly IReadOnlyList<TElement> List = new List<TElement>();
}
internal static class EmptyReadOnly<TElement>
{
public static readonly IReadOnlyCollection<TElement> Collection = new ReadOnlyCollection<TElement>(new TElement[0]);
public static readonly IReadOnlyList<TElement> List = new List<TElement>();
}

internal static class EmptyReadOnly<TKey, TValue>
{
public static readonly IReadOnlyDictionary<TKey, TValue> Dictionary = new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>(0));
}
internal static class EmptyReadOnly<TKey, TValue>
{
public static readonly IReadOnlyDictionary<TKey, TValue> Dictionary = new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>(0));
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public DefaultRequestResponseSerializer(IElasticsearchClientSettings settings)
new SelfTwoWaySerializableConverterFactory(settings),
new IndicesJsonConverter(settings),
new DictionaryConverter(),
new IsADictionaryConverter(),
new UnionConverter()
},
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
Expand Down
Loading