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.
This commit is contained in:
parent
6761dec9c6
commit
2420d8f0ac
|
|
@ -767,7 +767,7 @@ namespace Microsoft.AspNetCore.Components.RenderTree
|
||||||
public void AddContent<T>(int sequence, Microsoft.AspNetCore.Components.RenderFragment<T> fragment, T value) { }
|
public void AddContent<T>(int sequence, Microsoft.AspNetCore.Components.RenderFragment<T> fragment, T value) { }
|
||||||
public void AddElementReferenceCapture(int sequence, System.Action<Microsoft.AspNetCore.Components.ElementRef> elementReferenceCaptureAction) { }
|
public void AddElementReferenceCapture(int sequence, System.Action<Microsoft.AspNetCore.Components.ElementRef> elementReferenceCaptureAction) { }
|
||||||
public void AddMarkupContent(int sequence, string markupContent) { }
|
public void AddMarkupContent(int sequence, string markupContent) { }
|
||||||
public void AddMultipleAttributes<T>(int sequence, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, T>> attributes) { }
|
public void AddMultipleAttributes(int sequence, System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string, object>> attributes) { }
|
||||||
public void Clear() { }
|
public void Clear() { }
|
||||||
public void CloseComponent() { }
|
public void CloseComponent() { }
|
||||||
public void CloseElement() { }
|
public void CloseElement() { }
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ namespace Microsoft.AspNetCore.Components
|
||||||
/// </para>
|
/// </para>
|
||||||
/// <para>
|
/// <para>
|
||||||
/// <see cref="CaptureUnmatchedValues"/> should only be applied to parameters of a type that
|
/// <see cref="CaptureUnmatchedValues"/> should only be applied to parameters of a type that
|
||||||
/// can be used with <see cref="RenderTreeBuilder.AddMultipleAttributes{T}(int, System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{string, T}})"/>
|
/// can be used with <see cref="RenderTreeBuilder.AddMultipleAttributes(int, System.Collections.Generic.IEnumerable{System.Collections.Generic.KeyValuePair{string, System.Object}})"/>
|
||||||
/// such as <see cref="Dictionary{String, Object}"/>.
|
/// such as <see cref="Dictionary{String, Object}"/>.
|
||||||
/// </para>
|
/// </para>
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
|
|
|
||||||
|
|
@ -485,19 +485,10 @@ namespace Microsoft.AspNetCore.Components.RenderTree
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds frames representing multiple attributes with the same sequence number.
|
/// Adds frames representing multiple attributes with the same sequence number.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T">The attribute value type.</typeparam>
|
|
||||||
/// <param name="sequence">An integer that represents the position of the instruction in the source code.</param>
|
/// <param name="sequence">An integer that represents the position of the instruction in the source code.</param>
|
||||||
/// <param name="attributes">A collection of key-value pairs representing attributes.</param>
|
/// <param name="attributes">A collection of key-value pairs representing attributes.</param>
|
||||||
public void AddMultipleAttributes<T>(int sequence, IEnumerable<KeyValuePair<string, T>> attributes)
|
public void AddMultipleAttributes(int sequence, IEnumerable<KeyValuePair<string, object>> attributes)
|
||||||
{
|
{
|
||||||
// NOTE: The IEnumerable<KeyValuePair<string, T>> is the simplest way to support a variety of
|
|
||||||
// different types like IReadOnlyDictionary<>, Dictionary<>, and IDictionary<>.
|
|
||||||
//
|
|
||||||
// None of those types are contravariant, and since we want to support attributes having a value
|
|
||||||
// of type object, the simplest thing to do is drop down to IEnumerable<KeyValuePair<>> which
|
|
||||||
// is contravariant. This also gives us things like List<KeyValuePair<>> and KeyValuePair<>[]
|
|
||||||
// for free even though we don't expect those types to be common.
|
|
||||||
|
|
||||||
// Calling this up-front just to make sure we validate before mutating anything.
|
// Calling this up-front just to make sure we validate before mutating anything.
|
||||||
AssertCanAddAttribute();
|
AssertCanAddAttribute();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -254,7 +254,7 @@ namespace Microsoft.AspNetCore.Components.Test
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
builder.OpenElement(0, "myelement");
|
builder.OpenElement(0, "myelement");
|
||||||
builder.AddMultipleAttributes<object>(0, null);
|
builder.AddMultipleAttributes(0, null);
|
||||||
builder.CloseElement();
|
builder.CloseElement();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
|
|
@ -307,20 +307,6 @@ namespace Microsoft.AspNetCore.Components.Test
|
||||||
frame => AssertFrame.Attribute(frame, "attribute7", "the end"));
|
frame => AssertFrame.Attribute(frame, "attribute7", "the end"));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void CanAddMultipleAttributes_DictionaryString()
|
|
||||||
{
|
|
||||||
var attributes = new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{ "attribute1", "test1" },
|
|
||||||
{ "attribute2", "123" },
|
|
||||||
{ "attribute3", "456" },
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
CanAddMultipleAttributesTest(attributes);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanAddMultipleAttributes_DictionaryObject()
|
public void CanAddMultipleAttributes_DictionaryObject()
|
||||||
{
|
{
|
||||||
|
|
@ -335,20 +321,6 @@ namespace Microsoft.AspNetCore.Components.Test
|
||||||
CanAddMultipleAttributesTest(attributes);
|
CanAddMultipleAttributesTest(attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void CanAddMultipleAttributes_IReadOnlyDictionaryString()
|
|
||||||
{
|
|
||||||
var attributes = new Dictionary<string, string>
|
|
||||||
{
|
|
||||||
{ "attribute1", "test1" },
|
|
||||||
{ "attribute2", "123" },
|
|
||||||
{ "attribute3", "456" },
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
CanAddMultipleAttributesTest((IReadOnlyDictionary<string, string>)attributes);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanAddMultipleAttributes_IReadOnlyDictionaryObject()
|
public void CanAddMultipleAttributes_IReadOnlyDictionaryObject()
|
||||||
{
|
{
|
||||||
|
|
@ -363,20 +335,6 @@ namespace Microsoft.AspNetCore.Components.Test
|
||||||
CanAddMultipleAttributesTest((IReadOnlyDictionary<string, object>)attributes);
|
CanAddMultipleAttributesTest((IReadOnlyDictionary<string, object>)attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void CanAddMultipleAttributes_ListKvpString()
|
|
||||||
{
|
|
||||||
var attributes = new List<KeyValuePair<string, object>>()
|
|
||||||
{
|
|
||||||
new KeyValuePair<string, object>("attribute1", "test1"),
|
|
||||||
new KeyValuePair<string, object>("attribute2", "123"),
|
|
||||||
new KeyValuePair<string, object>("attribute3", "456"),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
CanAddMultipleAttributesTest(attributes);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanAddMultipleAttributes_ListKvpObject()
|
public void CanAddMultipleAttributes_ListKvpObject()
|
||||||
{
|
{
|
||||||
|
|
@ -391,20 +349,6 @@ namespace Microsoft.AspNetCore.Components.Test
|
||||||
CanAddMultipleAttributesTest(attributes);
|
CanAddMultipleAttributesTest(attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void CanAddMultipleAttributes_ArrayKvpString()
|
|
||||||
{
|
|
||||||
var attributes = new KeyValuePair<string, string>[]
|
|
||||||
{
|
|
||||||
new KeyValuePair<string, string>("attribute1", "test1"),
|
|
||||||
new KeyValuePair<string, string>("attribute2", "123"),
|
|
||||||
new KeyValuePair<string, string>("attribute3", "456"),
|
|
||||||
};
|
|
||||||
|
|
||||||
// Act & Assert
|
|
||||||
CanAddMultipleAttributesTest(attributes);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CanAddMultipleAttributes_ArrayKvpObject()
|
public void CanAddMultipleAttributes_ArrayKvpObject()
|
||||||
{
|
{
|
||||||
|
|
@ -419,7 +363,7 @@ namespace Microsoft.AspNetCore.Components.Test
|
||||||
CanAddMultipleAttributesTest(attributes);
|
CanAddMultipleAttributesTest(attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CanAddMultipleAttributesTest<T>(IEnumerable<KeyValuePair<string, T>> attributes)
|
private void CanAddMultipleAttributesTest(IEnumerable<KeyValuePair<string, object>> attributes)
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var builder = new RenderTreeBuilder(new TestRenderer());
|
var builder = new RenderTreeBuilder(new TestRenderer());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue