Skip to content

Commit 2420d8f

Browse files
Ryan Nowakrynowak
Ryan Nowak
authored andcommitted
Simplify attribute splatting
Removes handling for IEnumerable<KVP<string, string>>. This isn't something we really need for the main scenario and it MASSIVELY complicates the codegen part of the feature. Requiring the dictionary to be an object-valued dictionary should cover the cases we care about.
1 parent 6761dec commit 2420d8f

File tree

4 files changed

+5
-70
lines changed

4 files changed

+5
-70
lines changed

src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ public void AddContent(int sequence, string textContent) { }
767767
public void AddContent<T>(int sequence, Microsoft.AspNetCore.Components.RenderFragment<T> fragment, T value) { }
768768
public void AddElementReferenceCapture(int sequence, System.Action<Microsoft.AspNetCore.Components.ElementRef> elementReferenceCaptureAction) { }
769769
public void AddMarkupContent(int sequence, string markupContent) { }
770-
public void AddMultipleAttributes<T>(int sequence, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, T>> attributes) { }
770+
public void AddMultipleAttributes(int sequence, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes) { }
771771
public void Clear() { }
772772
public void CloseComponent() { }
773773
public void CloseElement() { }

src/Components/Components/src/ParameterAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public sealed class ParameterAttribute : Attribute
2727
/// </para>
2828
/// <para>
2929
/// <see cref="CaptureUnmatchedValues"/> should only be applied to parameters of a type that
30-
/// can be used with <see cref="RenderTreeBuilder.AddMultipleAttributes{T}(int, System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{string, T}})"/>
30+
/// can be used with <see cref="RenderTreeBuilder.AddMultipleAttributes(int, System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{string, System.Object}})"/>
3131
/// such as <see cref="Dictionary{String, Object}"/>.
3232
/// </para>
3333
/// </remarks>

src/Components/Components/src/RenderTree/RenderTreeBuilder.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -485,19 +485,10 @@ public void AddAttribute(int sequence, in RenderTreeFrame frame)
485485
/// <summary>
486486
/// Adds frames representing multiple attributes with the same sequence number.
487487
/// </summary>
488-
/// <typeparam name="T">The attribute value type.</typeparam>
489488
/// <param name="sequence">An integer that represents the position of the instruction in the source code.</param>
490489
/// <param name="attributes">A collection of key-value pairs representing attributes.</param>
491-
public void AddMultipleAttributes<T>(int sequence, IEnumerable<KeyValuePair<string, T>> attributes)
490+
public void AddMultipleAttributes(int sequence, IEnumerable<KeyValuePair<string, object>> attributes)
492491
{
493-
// NOTE: The IEnumerable<KeyValuePair<string, T>> is the simplest way to support a variety of
494-
// different types like IReadOnlyDictionary<>, Dictionary<>, and IDictionary<>.
495-
//
496-
// None of those types are contravariant, and since we want to support attributes having a value
497-
// of type object, the simplest thing to do is drop down to IEnumerable<KeyValuePair<>> which
498-
// is contravariant. This also gives us things like List<KeyValuePair<>> and KeyValuePair<>[]
499-
// for free even though we don't expect those types to be common.
500-
501492
// Calling this up-front just to make sure we validate before mutating anything.
502493
AssertCanAddAttribute();
503494

src/Components/Components/test/RenderTreeBuilderTest.cs

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public void CanAddMultipleAttributes_AllowsNull()
254254

255255
// Act
256256
builder.OpenElement(0, "myelement");
257-
builder.AddMultipleAttributes<object>(0, null);
257+
builder.AddMultipleAttributes(0, null);
258258
builder.CloseElement();
259259

260260
// Assert
@@ -307,20 +307,6 @@ public void CanAddMultipleAttributes_InterspersedWithOtherAttributes()
307307
frame => AssertFrame.Attribute(frame, "attribute7", "the end"));
308308
}
309309

310-
[Fact]
311-
public void CanAddMultipleAttributes_DictionaryString()
312-
{
313-
var attributes = new Dictionary<string, string>
314-
{
315-
{ "attribute1", "test1" },
316-
{ "attribute2", "123" },
317-
{ "attribute3", "456" },
318-
};
319-
320-
// Act & Assert
321-
CanAddMultipleAttributesTest(attributes);
322-
}
323-
324310
[Fact]
325311
public void CanAddMultipleAttributes_DictionaryObject()
326312
{
@@ -335,20 +321,6 @@ public void CanAddMultipleAttributes_DictionaryObject()
335321
CanAddMultipleAttributesTest(attributes);
336322
}
337323

338-
[Fact]
339-
public void CanAddMultipleAttributes_IReadOnlyDictionaryString()
340-
{
341-
var attributes = new Dictionary<string, string>
342-
{
343-
{ "attribute1", "test1" },
344-
{ "attribute2", "123" },
345-
{ "attribute3", "456" },
346-
};
347-
348-
// Act & Assert
349-
CanAddMultipleAttributesTest((IReadOnlyDictionary<string, string>)attributes);
350-
}
351-
352324
[Fact]
353325
public void CanAddMultipleAttributes_IReadOnlyDictionaryObject()
354326
{
@@ -363,20 +335,6 @@ public void CanAddMultipleAttributes_IReadOnlyDictionaryObject()
363335
CanAddMultipleAttributesTest((IReadOnlyDictionary<string, object>)attributes);
364336
}
365337

366-
[Fact]
367-
public void CanAddMultipleAttributes_ListKvpString()
368-
{
369-
var attributes = new List<KeyValuePair<string, object>>()
370-
{
371-
new KeyValuePair<string, object>("attribute1", "test1"),
372-
new KeyValuePair<string, object>("attribute2", "123"),
373-
new KeyValuePair<string, object>("attribute3", "456"),
374-
};
375-
376-
// Act & Assert
377-
CanAddMultipleAttributesTest(attributes);
378-
}
379-
380338
[Fact]
381339
public void CanAddMultipleAttributes_ListKvpObject()
382340
{
@@ -391,20 +349,6 @@ public void CanAddMultipleAttributes_ListKvpObject()
391349
CanAddMultipleAttributesTest(attributes);
392350
}
393351

394-
[Fact]
395-
public void CanAddMultipleAttributes_ArrayKvpString()
396-
{
397-
var attributes = new KeyValuePair<string, string>[]
398-
{
399-
new KeyValuePair<string, string>("attribute1", "test1"),
400-
new KeyValuePair<string, string>("attribute2", "123"),
401-
new KeyValuePair<string, string>("attribute3", "456"),
402-
};
403-
404-
// Act & Assert
405-
CanAddMultipleAttributesTest(attributes);
406-
}
407-
408352
[Fact]
409353
public void CanAddMultipleAttributes_ArrayKvpObject()
410354
{
@@ -419,7 +363,7 @@ public void CanAddMultipleAttributes_ArrayKvpObject()
419363
CanAddMultipleAttributesTest(attributes);
420364
}
421365

422-
private void CanAddMultipleAttributesTest<T>(IEnumerable<KeyValuePair<string, T>> attributes)
366+
private void CanAddMultipleAttributesTest(IEnumerable<KeyValuePair<string, object>> attributes)
423367
{
424368
// Arrange
425369
var builder = new RenderTreeBuilder(new TestRenderer());

0 commit comments

Comments
 (0)