|
8 | 8 | using System.ComponentModel; |
9 | 9 | using System.Linq; |
10 | 10 |
|
11 | | -namespace Elastic.Clients.Elasticsearch |
12 | | -{ |
| 11 | +namespace Elastic.Clients.Elasticsearch; |
| 12 | + |
| 13 | +public interface IIsADictionary { } |
13 | 14 |
|
14 | | - public interface IIsADictionary { } |
| 15 | +public interface IIsADictionary<TKey, TValue> : IDictionary<TKey, TValue>, IIsADictionary { } |
15 | 16 |
|
16 | | - public interface IIsADictionary<TKey, TValue> : IDictionary<TKey, TValue>, IIsADictionary { } |
| 17 | +/// <summary> |
| 18 | +/// A base dictionary type for internally tagged unions. |
| 19 | +/// </summary> |
| 20 | +/// <typeparam name="TKey"></typeparam> |
| 21 | +/// <typeparam name="TValue"></typeparam> |
17 | 22 |
|
18 | | - public abstract class IsADictionaryBase<TKey, TValue> : IIsADictionary<TKey, TValue> |
| 23 | +public abstract class IsADictionaryBase<TKey, TValue> : IIsADictionary<TKey, TValue> |
| 24 | +{ |
| 25 | + internal IsADictionaryBase() => BackingDictionary = new Dictionary<TKey, TValue>(); |
| 26 | + |
| 27 | + internal IsADictionaryBase(IDictionary<TKey, TValue> backingDictionary) |
19 | 28 | { |
20 | | - protected IsADictionaryBase() => BackingDictionary = new Dictionary<TKey, TValue>(); |
| 29 | + if (backingDictionary != null) |
| 30 | + foreach (var key in backingDictionary.Keys) |
| 31 | + ValidateKey(Sanitize(key)); |
21 | 32 |
|
22 | | - protected IsADictionaryBase(IDictionary<TKey, TValue> backingDictionary) |
23 | | - { |
24 | | - // ReSharper disable VirtualMemberCallInConstructor |
25 | | - if (backingDictionary != null) |
26 | | - foreach (var key in backingDictionary.Keys) |
27 | | - ValidateKey(Sanitize(key)); |
28 | | - // ReSharper enable VirtualMemberCallInConstructor |
| 33 | + BackingDictionary = backingDictionary != null |
| 34 | + ? new Dictionary<TKey, TValue>(backingDictionary) |
| 35 | + : new Dictionary<TKey, TValue>(); |
| 36 | + } |
29 | 37 |
|
30 | | - BackingDictionary = backingDictionary != null |
31 | | - ? new Dictionary<TKey, TValue>(backingDictionary) |
32 | | - : new Dictionary<TKey, TValue>(); |
33 | | - } |
| 38 | + public TValue this[TKey key] |
| 39 | + { |
| 40 | + get => BackingDictionary[Sanitize(key)]; |
| 41 | + set => BackingDictionary[ValidateKey(Sanitize(key))] = value; |
| 42 | + } |
34 | 43 |
|
35 | | - public TValue this[TKey key] |
36 | | - { |
37 | | - get => BackingDictionary[Sanitize(key)]; |
38 | | - set => BackingDictionary[ValidateKey(Sanitize(key))] = value; |
39 | | - } |
| 44 | + internal Dictionary<TKey, TValue> BackingDictionary { get; } |
| 45 | + int ICollection<KeyValuePair<TKey, TValue>>.Count => BackingDictionary.Count; |
| 46 | + bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => Self.IsReadOnly; |
40 | 47 |
|
41 | | - protected Dictionary<TKey, TValue> BackingDictionary { get; } |
42 | | - int ICollection<KeyValuePair<TKey, TValue>>.Count => BackingDictionary.Count; |
43 | | - bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly => Self.IsReadOnly; |
| 48 | + TValue IDictionary<TKey, TValue>.this[TKey key] |
| 49 | + { |
| 50 | + get => BackingDictionary[Sanitize(key)]; |
| 51 | + set => BackingDictionary[ValidateKey(Sanitize(key))] = value; |
| 52 | + } |
44 | 53 |
|
45 | | - TValue IDictionary<TKey, TValue>.this[TKey key] |
46 | | - { |
47 | | - get => BackingDictionary[Sanitize(key)]; |
48 | | - set => BackingDictionary[ValidateKey(Sanitize(key))] = value; |
49 | | - } |
| 54 | + ICollection<TKey> IDictionary<TKey, TValue>.Keys => BackingDictionary.Keys; |
| 55 | + private ICollection<KeyValuePair<TKey, TValue>> Self => BackingDictionary; |
| 56 | + ICollection<TValue> IDictionary<TKey, TValue>.Values => BackingDictionary.Values; |
50 | 57 |
|
51 | | - ICollection<TKey> IDictionary<TKey, TValue>.Keys => BackingDictionary.Keys; |
52 | | - private ICollection<KeyValuePair<TKey, TValue>> Self => BackingDictionary; |
53 | | - ICollection<TValue> IDictionary<TKey, TValue>.Values => BackingDictionary.Values; |
| 58 | + void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) |
| 59 | + { |
| 60 | + ValidateKey(Sanitize(item.Key)); |
| 61 | + Self.Add(item); |
| 62 | + } |
54 | 63 |
|
55 | | - void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item) |
56 | | - { |
57 | | - ValidateKey(Sanitize(item.Key)); |
58 | | - Self.Add(item); |
59 | | - } |
| 64 | + void ICollection<KeyValuePair<TKey, TValue>>.Clear() => BackingDictionary.Clear(); |
60 | 65 |
|
61 | | - void ICollection<KeyValuePair<TKey, TValue>>.Clear() => BackingDictionary.Clear(); |
| 66 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 67 | + bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) => Self.Contains(item); |
62 | 68 |
|
63 | | - [EditorBrowsable(EditorBrowsableState.Never)] |
64 | | - bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item) => Self.Contains(item); |
| 69 | + void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) => Self.CopyTo(array, arrayIndex); |
65 | 70 |
|
66 | | - void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) => Self.CopyTo(array, arrayIndex); |
| 71 | + bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) => Self.Remove(item); |
67 | 72 |
|
68 | | - bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item) => Self.Remove(item); |
| 73 | + void IDictionary<TKey, TValue>.Add(TKey key, TValue value) => BackingDictionary.Add(ValidateKey(Sanitize(key)), value); |
69 | 74 |
|
70 | | - void IDictionary<TKey, TValue>.Add(TKey key, TValue value) => BackingDictionary.Add(ValidateKey(Sanitize(key)), value); |
| 75 | + [EditorBrowsable(EditorBrowsableState.Never)] |
| 76 | + bool IDictionary<TKey, TValue>.ContainsKey(TKey key) => BackingDictionary.ContainsKey(Sanitize(key)); |
71 | 77 |
|
72 | | - [EditorBrowsable(EditorBrowsableState.Never)] |
73 | | - bool IDictionary<TKey, TValue>.ContainsKey(TKey key) => BackingDictionary.ContainsKey(Sanitize(key)); |
| 78 | + bool IDictionary<TKey, TValue>.Remove(TKey key) => BackingDictionary.Remove(Sanitize(key)); |
74 | 79 |
|
75 | | - bool IDictionary<TKey, TValue>.Remove(TKey key) => BackingDictionary.Remove(Sanitize(key)); |
| 80 | + bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value) => BackingDictionary.TryGetValue(Sanitize(key), out value); |
76 | 81 |
|
77 | | - bool IDictionary<TKey, TValue>.TryGetValue(TKey key, out TValue value) => BackingDictionary.TryGetValue(Sanitize(key), out value); |
| 82 | + IEnumerator IEnumerable.GetEnumerator() => BackingDictionary.GetEnumerator(); |
78 | 83 |
|
79 | | - IEnumerator IEnumerable.GetEnumerator() => BackingDictionary.GetEnumerator(); |
| 84 | + IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => BackingDictionary.GetEnumerator(); |
80 | 85 |
|
81 | | - IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => BackingDictionary.GetEnumerator(); |
| 86 | + protected virtual TKey ValidateKey(TKey key) => key; |
82 | 87 |
|
83 | | - protected virtual TKey ValidateKey(TKey key) => key; |
| 88 | + protected virtual TKey Sanitize(TKey key) => key; |
| 89 | +} |
84 | 90 |
|
85 | | - protected virtual TKey Sanitize(TKey key) => key; |
86 | | - } |
| 91 | +public abstract class IsADictionaryDescriptorBase<TDescriptor, TPromised, TKey, TValue> |
| 92 | + : DescriptorPromiseBase<TDescriptor, TPromised> |
| 93 | + where TDescriptor : IsADictionaryDescriptorBase<TDescriptor, TPromised, TKey, TValue> |
| 94 | + where TPromised : class, IIsADictionary<TKey, TValue> |
| 95 | +{ |
| 96 | + protected IsADictionaryDescriptorBase(TPromised instance) : base(instance) { } |
87 | 97 |
|
88 | | - public abstract class IsADictionaryDescriptorBase<TDescriptor, TPromised, TKey, TValue> |
89 | | - : DescriptorPromiseBase<TDescriptor, TPromised> |
90 | | - where TDescriptor : IsADictionaryDescriptorBase<TDescriptor, TPromised, TKey, TValue> |
91 | | - where TPromised : class, IIsADictionary<TKey, TValue> |
| 98 | + protected TDescriptor Assign(TKey key, TValue value) |
92 | 99 | { |
93 | | - protected IsADictionaryDescriptorBase(TPromised instance) : base(instance) { } |
94 | | - |
95 | | - protected TDescriptor Assign(TKey key, TValue value) |
96 | | - { |
97 | | - PromisedValue.Add(key, value); |
98 | | - return Self; |
99 | | - } |
| 100 | + PromisedValue.Add(key, value); |
| 101 | + return Self; |
100 | 102 | } |
| 103 | +} |
101 | 104 |
|
102 | | - public interface IIsAReadOnlyDictionary { } |
| 105 | +public interface IIsAReadOnlyDictionary { } |
103 | 106 |
|
104 | | - public interface IIsAReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>, IIsAReadOnlyDictionary { } |
| 107 | +public interface IIsAReadOnlyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>, IIsAReadOnlyDictionary { } |
105 | 108 |
|
106 | | - public abstract class IsAReadOnlyDictionaryBase<TKey, TValue> : IIsAReadOnlyDictionary<TKey, TValue> |
| 109 | +public abstract class IsAReadOnlyDictionaryBase<TKey, TValue> : IIsAReadOnlyDictionary<TKey, TValue> |
| 110 | +{ |
| 111 | + protected IsAReadOnlyDictionaryBase(IReadOnlyDictionary<TKey, TValue> backingDictionary) |
107 | 112 | { |
108 | | - protected IsAReadOnlyDictionaryBase(IReadOnlyDictionary<TKey, TValue> backingDictionary) |
109 | | - { |
110 | | - if (backingDictionary == null) |
111 | | - return; |
| 113 | + if (backingDictionary == null) |
| 114 | + return; |
112 | 115 |
|
113 | | - var dictionary = new Dictionary<TKey, TValue>(backingDictionary.Count); |
114 | | - foreach (var key in backingDictionary.Keys) |
115 | | - // ReSharper disable once VirtualMemberCallInConstructor |
116 | | - // expect all implementations of Sanitize to be pure |
117 | | - dictionary[Sanitize(key)] = backingDictionary[key]; |
| 116 | + var dictionary = new Dictionary<TKey, TValue>(backingDictionary.Count); |
| 117 | + foreach (var key in backingDictionary.Keys) |
| 118 | + // ReSharper disable once VirtualMemberCallInConstructor |
| 119 | + // expect all implementations of Sanitize to be pure |
| 120 | + dictionary[Sanitize(key)] = backingDictionary[key]; |
118 | 121 |
|
119 | | - BackingDictionary = dictionary; |
120 | | - } |
| 122 | + BackingDictionary = dictionary; |
| 123 | + } |
121 | 124 |
|
122 | | - public int Count => BackingDictionary.Count; |
| 125 | + public int Count => BackingDictionary.Count; |
123 | 126 |
|
124 | | - public TValue this[TKey key] => BackingDictionary[key]; |
| 127 | + public TValue this[TKey key] => BackingDictionary[key]; |
125 | 128 |
|
126 | | - public IEnumerable<TKey> Keys => BackingDictionary.Keys; |
| 129 | + public IEnumerable<TKey> Keys => BackingDictionary.Keys; |
127 | 130 |
|
128 | | - public IEnumerable<TValue> Values => BackingDictionary.Values; |
129 | | - protected internal IReadOnlyDictionary<TKey, TValue> BackingDictionary { get; } = EmptyReadOnly<TKey, TValue>.Dictionary; |
| 131 | + public IEnumerable<TValue> Values => BackingDictionary.Values; |
| 132 | + protected internal IReadOnlyDictionary<TKey, TValue> BackingDictionary { get; } = EmptyReadOnly<TKey, TValue>.Dictionary; |
130 | 133 |
|
131 | | - IEnumerator IEnumerable.GetEnumerator() => BackingDictionary.GetEnumerator(); |
| 134 | + IEnumerator IEnumerable.GetEnumerator() => BackingDictionary.GetEnumerator(); |
132 | 135 |
|
133 | | - IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => |
134 | | - BackingDictionary.GetEnumerator(); |
| 136 | + IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator() => |
| 137 | + BackingDictionary.GetEnumerator(); |
135 | 138 |
|
136 | | - public bool ContainsKey(TKey key) => BackingDictionary.ContainsKey(key); |
| 139 | + public bool ContainsKey(TKey key) => BackingDictionary.ContainsKey(key); |
137 | 140 |
|
138 | | - public bool TryGetValue(TKey key, out TValue value) => |
139 | | - BackingDictionary.TryGetValue(key, out value); |
| 141 | + public bool TryGetValue(TKey key, out TValue value) => |
| 142 | + BackingDictionary.TryGetValue(key, out value); |
140 | 143 |
|
141 | | - protected virtual TKey Sanitize(TKey key) => key; |
142 | | - } |
| 144 | + protected virtual TKey Sanitize(TKey key) => key; |
| 145 | +} |
143 | 146 |
|
144 | | - internal static class EmptyReadOnlyExtensions |
145 | | - { |
146 | | - public static IReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> enumerable) => |
147 | | - enumerable == null ? EmptyReadOnly<T>.Collection : new ReadOnlyCollection<T>(enumerable.ToList()); |
| 147 | +internal static class EmptyReadOnlyExtensions |
| 148 | +{ |
| 149 | + public static IReadOnlyCollection<T> ToReadOnlyCollection<T>(this IEnumerable<T> enumerable) => |
| 150 | + enumerable == null ? EmptyReadOnly<T>.Collection : new ReadOnlyCollection<T>(enumerable.ToList()); |
148 | 151 |
|
149 | | - public static IReadOnlyCollection<T> ToReadOnlyCollection<T>(this IList<T> enumerable) => |
150 | | - enumerable == null || enumerable.Count == 0 ? EmptyReadOnly<T>.Collection : new ReadOnlyCollection<T>(enumerable); |
151 | | - } |
| 152 | + public static IReadOnlyCollection<T> ToReadOnlyCollection<T>(this IList<T> enumerable) => |
| 153 | + enumerable == null || enumerable.Count == 0 ? EmptyReadOnly<T>.Collection : new ReadOnlyCollection<T>(enumerable); |
| 154 | +} |
152 | 155 |
|
153 | 156 |
|
154 | | - internal static class EmptyReadOnly<TElement> |
155 | | - { |
156 | | - public static readonly IReadOnlyCollection<TElement> Collection = new ReadOnlyCollection<TElement>(new TElement[0]); |
157 | | - public static readonly IReadOnlyList<TElement> List = new List<TElement>(); |
158 | | - } |
| 157 | +internal static class EmptyReadOnly<TElement> |
| 158 | +{ |
| 159 | + public static readonly IReadOnlyCollection<TElement> Collection = new ReadOnlyCollection<TElement>(new TElement[0]); |
| 160 | + public static readonly IReadOnlyList<TElement> List = new List<TElement>(); |
| 161 | +} |
159 | 162 |
|
160 | | - internal static class EmptyReadOnly<TKey, TValue> |
161 | | - { |
162 | | - public static readonly IReadOnlyDictionary<TKey, TValue> Dictionary = new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>(0)); |
163 | | - } |
| 163 | +internal static class EmptyReadOnly<TKey, TValue> |
| 164 | +{ |
| 165 | + public static readonly IReadOnlyDictionary<TKey, TValue> Dictionary = new ReadOnlyDictionary<TKey, TValue>(new Dictionary<TKey, TValue>(0)); |
164 | 166 | } |
0 commit comments