Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge DUI3/Alpha into sdk #13

Merged
merged 2 commits into from
Jul 8, 2024
Merged
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
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Globalization;
using Speckle.Core.Logging;
Expand Down Expand Up @@ -157,16 +158,17 @@
#endregion
}

// Handle List<?>
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
// Handle List<>, IList<>, and IReadOnlyList<>
if (type.IsGenericType && IsGenericList(type))
{
if (value is not List<object> valueList)
{
return false;
}

var targetType = typeof(List<>).MakeGenericType(type.GenericTypeArguments);
Type listElementType = type.GenericTypeArguments[0];
IList ret = (IList)Activator.CreateInstance(type, valueList.Count);
IList ret = (IList)Activator.CreateInstance(targetType, valueList.Count);
foreach (object inputListElement in valueList)
{
if (!ConvertValue(listElementType, inputListElement, out object? convertedListElement))
Expand Down Expand Up @@ -311,4 +313,21 @@

return false;
}

/// <summary>
/// Tests that the given <paramref name="type"/> is assignable from a generic type def <see cref="List{T}"/>
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
[Pure]
private static bool IsGenericList(Type type)
{
if (!type.IsGenericType)
{
return false;

Check warning on line 327 in src/Speckle.Core/Serialisation/SerializationUtilities/ValueConverter.cs

View check run for this annotation

Codecov / codecov/patch

src/Speckle.Core/Serialisation/SerializationUtilities/ValueConverter.cs#L327

Added line #L327 was not covered by tests
}

Type typeDef = type.GetGenericTypeDefinition();
return typeDef == typeof(List<>) || typeDef == typeof(IList<>) || typeDef == typeof(IReadOnlyList<>);
}
}