Skip to content

Commit c6192d2

Browse files
committed
Support for BCL value types in object flattening
1 parent 8c0e5db commit c6192d2

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

AgileMapper.UnitTests/WhenFlatteningToDictionaries.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace AgileObjects.AgileMapper.UnitTests
22
{
3+
using System;
34
using System.Collections.Generic;
45
using TestClasses;
56
using Xunit;
@@ -46,6 +47,15 @@ public void ShouldFlattenAComplexTypeMember()
4647
((int)result["Value.Value"]).ShouldBe(1234);
4748
}
4849

50+
[Fact]
51+
public void ShouldFlattenANullableComplexTypeMember()
52+
{
53+
var source = new PublicProperty<DateTimeOffset?> { Value = null };
54+
var result = Mapper.Flatten(source).ToDictionary<string>();
55+
56+
result.ShouldNotContainKey("Value");
57+
}
58+
4959
[Fact]
5060
public void ShouldHandleANullComplexTypeMember()
5161
{

AgileMapper/Extensions/Internal/TypeExtensions.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,9 @@ public static bool IsEnumerable(this Type type)
129129
type.IsAssignableTo(typeof(IEnumerable)));
130130
}
131131

132-
public static bool IsQueryable(this Type type)
133-
{
134-
return type.IsGenericType() && type.GetGenericTypeDefinition() == typeof(IQueryable<>);
135-
}
132+
public static bool IsQueryable(this Type type) => type.IsClosedTypeOf(typeof(IQueryable<>));
136133

137-
public static bool IsComplex(this Type type)
138-
{
139-
return !type.IsSimple() && !type.IsEnumerable();
140-
}
134+
public static bool IsComplex(this Type type) => !type.IsSimple() && !type.IsEnumerable();
141135

142136
public static bool IsSimple(this Type type)
143137
{
@@ -153,15 +147,18 @@ public static bool IsSimple(this Type type)
153147
return true;
154148
}
155149

156-
return type == typeof(Guid);
157-
}
150+
if ((type == typeof(Guid)) ||
151+
(type == typeof(TimeSpan)) ||
152+
(type == typeof(DateTimeOffset)))
153+
{
154+
return true;
155+
}
158156

159-
public static bool IsDictionary(this Type type)
160-
{
161-
// ReSharper disable once UnusedVariable
162-
return IsDictionary(type, out var keyAndValueTypes);
157+
return type.IsValueType() && type.IsFromBcl();
163158
}
164159

160+
public static bool IsDictionary(this Type type) => IsDictionary(type, out var _);
161+
165162
public static bool IsDictionary(this Type type, out KeyValuePair<Type, Type> keyAndValueTypes)
166163
{
167164
keyAndValueTypes = GetDictionaryTypes(type);

AgileMapper/Validation/EnumMappingMismatchFinder.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,7 @@ protected override Expression VisitBinary(BinaryExpression binary)
150150
return base.VisitBinary(binary);
151151
}
152152

153-
private static bool IsEnum(Type type)
154-
{
155-
// ReSharper disable once UnusedVariable
156-
return IsEnum(type, out var enumType);
157-
}
153+
private static bool IsEnum(Type type) => IsEnum(type, out var _);
158154

159155
private bool TryGetMatch(Expression targetMemberAccess, out TargetMemberData targetMemberData)
160156
{

0 commit comments

Comments
 (0)