Skip to content

Commit 3947c98

Browse files
committed
Cleanups
1 parent 1f39af7 commit 3947c98

File tree

8 files changed

+149
-116
lines changed

8 files changed

+149
-116
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components.Endpoints.Binding;
5+
6+
// Uses a concrete type that implements IDictionary<TKey, TValue> as a buffer.
7+
internal class DictionaryBufferAdapter<TDictionaryType, TKey, TValue>
8+
: IDictionaryBufferAdapter<TDictionaryType, TDictionaryType, TKey, TValue>
9+
where TDictionaryType : IDictionary<TKey, TValue>, new()
10+
where TKey : IParsable<TKey>
11+
{
12+
public static TDictionaryType Add(ref TDictionaryType buffer, TKey key, TValue value)
13+
{
14+
buffer.Add(key, value);
15+
return buffer;
16+
}
17+
18+
public static TDictionaryType CreateBuffer() => new TDictionaryType();
19+
20+
public static TDictionaryType ToResult(TDictionaryType buffer) => buffer;
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Components.Endpoints.Binding;
5+
6+
// Adapts a concrete dictionary type into an interface.
7+
internal class DictionaryStaticCastAdapter<TDictionaryInterface, TDictionaryImplementation, TDictionaryAdapter, TBuffer, TKey, TValue>
8+
: IDictionaryBufferAdapter<TDictionaryInterface, TBuffer, TKey, TValue>
9+
where TDictionaryAdapter : IDictionaryBufferAdapter<TDictionaryImplementation, TBuffer, TKey, TValue>
10+
where TDictionaryImplementation : TDictionaryInterface
11+
where TKey : IParsable<TKey>
12+
{
13+
public static TBuffer CreateBuffer() => TDictionaryAdapter.CreateBuffer();
14+
15+
public static TBuffer Add(ref TBuffer buffer, TKey key, TValue element) => TDictionaryAdapter.Add(ref buffer, key, element);
16+
17+
public static TDictionaryInterface ToResult(TBuffer buffer) => TDictionaryAdapter.ToResult(buffer);
18+
}

src/Components/Endpoints/src/Binding/Converters/DictionaryAdapters/IDictionaryBufferAdapter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33

44
namespace Microsoft.AspNetCore.Components.Endpoints.Binding;
55

6+
// Interface for constructing a dictionary like instance using the
7+
// dictionary converter.
8+
// This interface abstracts over the different ways of constructing a dictionary.
9+
// For example, Immutable types use a builder as a buffer, while other types
10+
// use an instance of the dictionary itself as a buffer.
611
internal interface IDictionaryBufferAdapter<TDictionary, TBuffer, TKey, TValue>
712
where TKey : IParsable<TKey>
813
{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Immutable;
5+
6+
namespace Microsoft.AspNetCore.Components.Endpoints.Binding;
7+
8+
internal class ImmutableDictionaryBufferAdapter<TKey, TValue>
9+
: IDictionaryBufferAdapter<ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>.Builder, TKey, TValue>
10+
where TKey : IParsable<TKey>
11+
{
12+
public static ImmutableDictionary<TKey, TValue>.Builder Add(ref ImmutableDictionary<TKey, TValue>.Builder buffer, TKey key, TValue value)
13+
{
14+
buffer.Add(key, value);
15+
return buffer;
16+
}
17+
18+
public static ImmutableDictionary<TKey, TValue>.Builder CreateBuffer() => ImmutableDictionary.CreateBuilder<TKey, TValue>();
19+
20+
public static ImmutableDictionary<TKey, TValue> ToResult(ImmutableDictionary<TKey, TValue>.Builder buffer) => buffer.ToImmutable();
21+
22+
internal static DictionaryConverter<IImmutableDictionary<TKey, TValue>> CreateInterfaceConverter(FormDataConverter<TValue> valueTypeConverter)
23+
{
24+
return new DictionaryConverter<IImmutableDictionary<TKey, TValue>,
25+
DictionaryStaticCastAdapter<
26+
IImmutableDictionary<TKey, TValue>,
27+
ImmutableDictionary<TKey, TValue>,
28+
ImmutableDictionaryBufferAdapter<TKey, TValue>,
29+
ImmutableDictionary<TKey, TValue>.Builder,
30+
TKey,
31+
TValue>,
32+
ImmutableDictionary<TKey, TValue>.Builder,
33+
TKey,
34+
TValue>(valueTypeConverter);
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Immutable;
5+
6+
namespace Microsoft.AspNetCore.Components.Endpoints.Binding;
7+
8+
internal class ImmutableSortedDictionaryBufferAdapter<TKey, TValue>
9+
: IDictionaryBufferAdapter<ImmutableSortedDictionary<TKey, TValue>, ImmutableSortedDictionary<TKey, TValue>.Builder, TKey, TValue>
10+
where TKey : IParsable<TKey>
11+
{
12+
public static ImmutableSortedDictionary<TKey, TValue>.Builder Add(ref ImmutableSortedDictionary<TKey, TValue>.Builder buffer, TKey key, TValue value)
13+
{
14+
buffer.Add(key, value);
15+
return buffer;
16+
}
17+
18+
public static ImmutableSortedDictionary<TKey, TValue>.Builder CreateBuffer() => ImmutableSortedDictionary.CreateBuilder<TKey, TValue>();
19+
20+
public static ImmutableSortedDictionary<TKey, TValue> ToResult(ImmutableSortedDictionary<TKey, TValue>.Builder buffer) => buffer.ToImmutable();
21+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.ObjectModel;
5+
6+
namespace Microsoft.AspNetCore.Components.Endpoints.Binding;
7+
8+
internal class ReadOnlyDictionaryBufferAdapter<TKey, TValue>
9+
: IDictionaryBufferAdapter<ReadOnlyDictionary<TKey, TValue>, Dictionary<TKey, TValue>, TKey, TValue>
10+
where TKey : IParsable<TKey>
11+
{
12+
public static Dictionary<TKey, TValue> Add(ref Dictionary<TKey, TValue> buffer, TKey key, TValue value)
13+
{
14+
buffer.Add(key, value);
15+
return buffer;
16+
}
17+
18+
public static Dictionary<TKey, TValue> CreateBuffer() =>
19+
new Dictionary<TKey, TValue>();
20+
21+
public static ReadOnlyDictionary<TKey, TValue> ToResult(Dictionary<TKey, TValue> buffer) =>
22+
new ReadOnlyDictionary<TKey, TValue>(buffer);
23+
24+
internal static DictionaryConverter<IReadOnlyDictionary<TKey, TValue>> CreateInterfaceConverter(FormDataConverter<TValue> valueTypeConverter)
25+
{
26+
return new DictionaryConverter<IReadOnlyDictionary<TKey, TValue>,
27+
DictionaryStaticCastAdapter<
28+
IReadOnlyDictionary<TKey, TValue>,
29+
ReadOnlyDictionary<TKey, TValue>,
30+
ReadOnlyDictionaryBufferAdapter<TKey, TValue>,
31+
Dictionary<TKey, TValue>,
32+
TKey,
33+
TValue>,
34+
Dictionary<TKey, TValue>,
35+
TKey,
36+
TValue>(valueTypeConverter);
37+
}
38+
39+
internal static DictionaryConverter<ReadOnlyDictionary<TKey, TValue>> CreateConverter(FormDataConverter<TValue> valueTypeConverter)
40+
{
41+
return new DictionaryConverter<ReadOnlyDictionary<TKey, TValue>,
42+
ReadOnlyDictionaryBufferAdapter<TKey, TValue>,
43+
Dictionary<TKey, TValue>,
44+
TKey,
45+
TValue>(valueTypeConverter);
46+
}
47+
}

src/Components/Endpoints/src/Binding/Converters/DictionaryConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ internal override bool TryRead(
6868

6969
TDictionaryPolicy.Add(ref buffer, keyValue!, currentValue);
7070
keyCount++;
71-
if (keyCount > maxCollectionSize)
71+
if (keyCount == maxCollectionSize)
7272
{
7373
succeded = false;
7474
break;

src/Components/Endpoints/src/Binding/Factories/Dictionary/TypedDictionaryConverterFactory.cs

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -151,118 +151,3 @@ var _ when type.IsAssignableTo(typeof(IDictionary<TKey, TValue>)) && type.GetCon
151151
throw new InvalidOperationException($"Unable to create converter for '{type.FullName}'.");
152152
}
153153
}
154-
155-
internal class DictionaryBufferAdapter<TDictionaryType, TKey, TValue>
156-
: IDictionaryBufferAdapter<TDictionaryType, TDictionaryType, TKey, TValue>
157-
where TDictionaryType : IDictionary<TKey, TValue>, new()
158-
where TKey : IParsable<TKey>
159-
{
160-
public static TDictionaryType Add(ref TDictionaryType buffer, TKey key, TValue value)
161-
{
162-
buffer.Add(key, value);
163-
return buffer;
164-
}
165-
166-
public static TDictionaryType CreateBuffer() => new TDictionaryType();
167-
168-
public static TDictionaryType ToResult(TDictionaryType buffer) => buffer;
169-
}
170-
171-
internal class ReadOnlyDictionaryBufferAdapter<TKey, TValue>
172-
: IDictionaryBufferAdapter<ReadOnlyDictionary<TKey, TValue>, Dictionary<TKey, TValue>, TKey, TValue>
173-
where TKey : IParsable<TKey>
174-
{
175-
public static Dictionary<TKey, TValue> Add(ref Dictionary<TKey, TValue> buffer, TKey key, TValue value)
176-
{
177-
buffer.Add(key, value);
178-
return buffer;
179-
}
180-
181-
public static Dictionary<TKey, TValue> CreateBuffer() =>
182-
new Dictionary<TKey, TValue>();
183-
184-
public static ReadOnlyDictionary<TKey, TValue> ToResult(Dictionary<TKey, TValue> buffer) =>
185-
new ReadOnlyDictionary<TKey, TValue>(buffer);
186-
187-
internal static DictionaryConverter<IReadOnlyDictionary<TKey, TValue>> CreateInterfaceConverter(FormDataConverter<TValue> valueTypeConverter)
188-
{
189-
return new DictionaryConverter<IReadOnlyDictionary<TKey, TValue>,
190-
DictionaryStaticCastAdapter<
191-
IReadOnlyDictionary<TKey, TValue>,
192-
ReadOnlyDictionary<TKey, TValue>,
193-
ReadOnlyDictionaryBufferAdapter<TKey, TValue>,
194-
Dictionary<TKey, TValue>,
195-
TKey,
196-
TValue>,
197-
Dictionary<TKey, TValue>,
198-
TKey,
199-
TValue>(valueTypeConverter);
200-
}
201-
202-
internal static DictionaryConverter<ReadOnlyDictionary<TKey, TValue>> CreateConverter(FormDataConverter<TValue> valueTypeConverter)
203-
{
204-
return new DictionaryConverter<ReadOnlyDictionary<TKey, TValue>,
205-
ReadOnlyDictionaryBufferAdapter<TKey, TValue>,
206-
Dictionary<TKey, TValue>,
207-
TKey,
208-
TValue>(valueTypeConverter);
209-
}
210-
}
211-
212-
internal class ImmutableDictionaryBufferAdapter<TKey, TValue>
213-
: IDictionaryBufferAdapter<ImmutableDictionary<TKey, TValue>, ImmutableDictionary<TKey, TValue>.Builder, TKey, TValue>
214-
where TKey : IParsable<TKey>
215-
{
216-
public static ImmutableDictionary<TKey, TValue>.Builder Add(ref ImmutableDictionary<TKey, TValue>.Builder buffer, TKey key, TValue value)
217-
{
218-
buffer.Add(key, value);
219-
return buffer;
220-
}
221-
222-
public static ImmutableDictionary<TKey, TValue>.Builder CreateBuffer() => ImmutableDictionary.CreateBuilder<TKey, TValue>();
223-
224-
public static ImmutableDictionary<TKey, TValue> ToResult(ImmutableDictionary<TKey, TValue>.Builder buffer) => buffer.ToImmutable();
225-
226-
internal static DictionaryConverter<IImmutableDictionary<TKey, TValue>> CreateInterfaceConverter(FormDataConverter<TValue> valueTypeConverter)
227-
{
228-
return new DictionaryConverter<IImmutableDictionary<TKey, TValue>,
229-
DictionaryStaticCastAdapter<
230-
IImmutableDictionary<TKey, TValue>,
231-
ImmutableDictionary<TKey, TValue>,
232-
ImmutableDictionaryBufferAdapter<TKey, TValue>,
233-
ImmutableDictionary<TKey, TValue>.Builder,
234-
TKey,
235-
TValue>,
236-
ImmutableDictionary<TKey, TValue>.Builder,
237-
TKey,
238-
TValue>(valueTypeConverter);
239-
}
240-
}
241-
242-
internal class ImmutableSortedDictionaryBufferAdapter<TKey, TValue>
243-
: IDictionaryBufferAdapter<ImmutableSortedDictionary<TKey, TValue>, ImmutableSortedDictionary<TKey, TValue>.Builder, TKey, TValue>
244-
where TKey : IParsable<TKey>
245-
{
246-
public static ImmutableSortedDictionary<TKey, TValue>.Builder Add(ref ImmutableSortedDictionary<TKey, TValue>.Builder buffer, TKey key, TValue value)
247-
{
248-
buffer.Add(key, value);
249-
return buffer;
250-
}
251-
252-
public static ImmutableSortedDictionary<TKey, TValue>.Builder CreateBuffer() => ImmutableSortedDictionary.CreateBuilder<TKey, TValue>();
253-
254-
public static ImmutableSortedDictionary<TKey, TValue> ToResult(ImmutableSortedDictionary<TKey, TValue>.Builder buffer) => buffer.ToImmutable();
255-
}
256-
257-
internal class DictionaryStaticCastAdapter<TDictionaryInterface, TDictionaryImplementation, TDictionaryAdapter, TBuffer, TKey, TValue>
258-
: IDictionaryBufferAdapter<TDictionaryInterface, TBuffer, TKey, TValue>
259-
where TDictionaryAdapter : IDictionaryBufferAdapter<TDictionaryImplementation, TBuffer, TKey, TValue>
260-
where TDictionaryImplementation : TDictionaryInterface
261-
where TKey : IParsable<TKey>
262-
{
263-
public static TBuffer CreateBuffer() => TDictionaryAdapter.CreateBuffer();
264-
265-
public static TBuffer Add(ref TBuffer buffer, TKey key, TValue element) => TDictionaryAdapter.Add(ref buffer, key, element);
266-
267-
public static TDictionaryInterface ToResult(TBuffer buffer) => TDictionaryAdapter.ToResult(buffer);
268-
}

0 commit comments

Comments
 (0)