Skip to content

Upgrade to support AutoMapper 8 #20

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

Merged
merged 4 commits into from
Nov 18, 2018
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Authors>Jimmy Bogard</Authors>
<LangVersion>latest</LangVersion>
<VersionPrefix>1.0.5</VersionPrefix>
<VersionPrefix>2.0.0</VersionPrefix>
<WarningsAsErrors>true</WarningsAsErrors>
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Summary>Expression mapping (OData) extensions for AutoMapper</Summary>
<Description>Expression mapping (OData) extensions for AutoMapper</Description>
<TargetFrameworks>netstandard2.0;netstandard1.3;net45</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyOriginatorKeyFile>..\..\AutoMapper.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand All @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="7.0.1" />
<PackageReference Include="AutoMapper" Version="8.0.0" />
</ItemGroup>

</Project>
18 changes: 9 additions & 9 deletions src/AutoMapper.Extensions.ExpressionMapping/ExpressionMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public bool IsMatch(TypePair context) => typeof(LambdaExpression).IsAssignableFr
&& typeof(LambdaExpression).IsAssignableFrom(context.DestinationType)
&& context.DestinationType != typeof(LambdaExpression);

public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression) =>
public Expression MapExpression(IConfigurationProvider configurationProvider, ProfileMap profileMap, IMemberMap memberMap, Expression sourceExpression, Expression destExpression, Expression contextExpression) =>
Call(null,
MapMethodInfo.MakeGenericMethod(sourceExpression.Type, destExpression.Type),
sourceExpression,
Expand Down Expand Up @@ -151,11 +151,11 @@ private static bool BothAreNullable(Expression node, Expression newLeft)
private static bool BothAreNonNullable(Expression node, Expression newLeft)
=> !node.Type.IsNullableType() && !newLeft.Type.IsNullableType();

protected override Expression VisitLambda<T>(Expression<T> expression)
protected override Expression VisitLambda<T>(Expression<T> node)
{
return expression.Parameters.Any(b => b.Type == _oldParam.Type)
? VisitLambdaExpression(expression)
: VisitAllParametersExpression(expression);
return node.Parameters.Any(b => b.Type == _oldParam.Type)
? VisitLambdaExpression(node)
: VisitAllParametersExpression(node);
}

private Expression VisitLambdaExpression<T>(Expression<T> expression)
Expand Down Expand Up @@ -207,8 +207,8 @@ protected override Expression VisitMember(MemberExpression node)
if (replacedExpression == node.Expression)
replacedExpression = _parentMappingVisitor.Visit(node.Expression);

if (propertyMap.CustomExpression != null)
return propertyMap.CustomExpression.ReplaceParameters(replacedExpression);
if (propertyMap.CustomMapExpression != null)
return propertyMap.CustomMapExpression.ReplaceParameters(replacedExpression);

Func<Expression, MemberInfo, Expression> getExpression = MakeMemberAccess;

Expand All @@ -235,7 +235,7 @@ private Expression GetConvertedSubMemberCall(MemberExpression node)
if (propertyMap == null)
return node;
var sourceType = GetSourceType(propertyMap);
var destType = propertyMap.DestinationPropertyType;
var destType = propertyMap.DestinationType;
if (sourceType == destType)
return MakeMemberAccess(baseExpression, node.Member);
var typeMap = _configurationProvider.ResolveTypeMap(sourceType, destType);
Expand All @@ -250,7 +250,7 @@ private Type GetSourceType(PropertyMap propertyMap) =>
throw new AutoMapperMappingException(
"Could not determine source property type. Make sure the property is mapped.",
null,
new TypePair(null, propertyMap.DestinationPropertyType),
new TypePair(null, propertyMap.DestinationType),
propertyMap.TypeMap,
propertyMap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ private static void FindChildPropertyTypeMaps(this Dictionary<Type, Type> typeMa
if (typeMap == null)
return;

FindMaps(typeMap.GetPropertyMaps().ToList());
FindMaps(typeMap.PropertyMaps.ToList());
void FindMaps(List<PropertyMap> maps)
{
foreach (PropertyMap pm in maps)
Expand All @@ -309,7 +309,7 @@ void FindMaps(List<PropertyMap> maps)

AddChildMappings
(
source.GetFieldOrProperty(pm.DestinationProperty.Name).GetMemberType(),
source.GetFieldOrProperty(pm.DestinationMember.Name).GetMemberType(),
pm.SourceMember.GetMemberType()
);
void AddChildMappings(Type sourcePropertyType, Type destPropertyType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public XpressionMapperVisitor(IMapper mapper, IConfigurationProvider configurati

protected IMapper Mapper { get; }

protected override Expression VisitParameter(ParameterExpression parameterExpression)
protected override Expression VisitParameter(ParameterExpression node)
{
InfoDictionary.Add(parameterExpression, TypeMappings);
var pair = InfoDictionary.SingleOrDefault(a => a.Key.Equals(parameterExpression));
return !pair.Equals(default(KeyValuePair<Type, MapperInfo>)) ? pair.Value.NewParameter : base.VisitParameter(parameterExpression);
InfoDictionary.Add(node, TypeMappings);
var pair = InfoDictionary.SingleOrDefault(a => a.Key.Equals(node));
return !pair.Equals(default(KeyValuePair<Type, MapperInfo>)) ? pair.Value.NewParameter : base.VisitParameter(node);
}

protected override Expression VisitMember(MemberExpression node)
Expand Down Expand Up @@ -328,54 +328,54 @@ protected void FindDestinationFullName(Type typeSource, Type typeDestination, st
PathMap pathMap = typeMap.FindPathMapByDestinationPath(destinationFullPath: sourceFullName);
if (pathMap != null)
{
propertyMapInfoList.Add(new PropertyMapInfo(pathMap.SourceExpression, new List<MemberInfo>()));
propertyMapInfoList.Add(new PropertyMapInfo(pathMap.CustomMapExpression, new List<MemberInfo>()));
return;
}


if (sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase) < 0)
{
var propertyMap = typeMap.GetPropertyMapByDestinationProperty(sourceFullName);
var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationProperty.Name);
var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationMember.Name);
if (propertyMap.ValueResolverConfig != null)
{
throw new InvalidOperationException(Resource.customResolversNotSupported);
}

if (propertyMap.CustomExpression == null && !propertyMap.SourceMembers.Any())
if (propertyMap.CustomMapExpression == null && !propertyMap.SourceMembers.Any())
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.srcMemberCannotBeNullFormat, typeSource.Name, typeDestination.Name, sourceFullName));

CompareSourceAndDestLiterals
(
propertyMap.CustomExpression != null ? propertyMap.CustomExpression.ReturnType : propertyMap.SourceMember.GetMemberType(),
propertyMap.CustomExpression != null ? propertyMap.CustomExpression.ToString() : propertyMap.SourceMember.Name,
propertyMap.CustomMapExpression != null ? propertyMap.CustomMapExpression.ReturnType : propertyMap.SourceMember.GetMemberType(),
propertyMap.CustomMapExpression != null ? propertyMap.CustomMapExpression.ToString() : propertyMap.SourceMember.Name,
sourceMemberInfo.GetMemberType()
);

void CompareSourceAndDestLiterals(Type mappedPropertyType, string mappedPropertyDescription, Type sourceMemberType)
{
//switch from IsValueType to IsLiteralType because we do not want to throw an exception for all structs
if ((mappedPropertyType.IsLiteralType() || sourceMemberType.IsLiteralType()) && sourceMemberType != mappedPropertyType)
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.expressionMapValueTypeMustMatchFormat, mappedPropertyType.Name, mappedPropertyDescription, sourceMemberType.Name, propertyMap.DestinationProperty.Name));
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.expressionMapValueTypeMustMatchFormat, mappedPropertyType.Name, mappedPropertyDescription, sourceMemberType.Name, propertyMap.DestinationMember.Name));
}

propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomExpression, propertyMap.SourceMembers.ToList()));
propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomMapExpression, propertyMap.SourceMembers.ToList()));
}
else
{
var propertyName = sourceFullName.Substring(0, sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase));
var propertyMap = typeMap.GetPropertyMapByDestinationProperty(propertyName);

var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationProperty.Name);
if (propertyMap.CustomExpression == null && !propertyMap.SourceMembers.Any())//If sourceFullName has a period then the SourceMember cannot be null. The SourceMember is required to find the ProertyMap of its child object.
var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationMember.Name);
if (propertyMap.CustomMapExpression == null && !propertyMap.SourceMembers.Any())//If sourceFullName has a period then the SourceMember cannot be null. The SourceMember is required to find the ProertyMap of its child object.
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.srcMemberCannotBeNullFormat, typeSource.Name, typeDestination.Name, propertyName));

propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomExpression, propertyMap.SourceMembers.ToList()));
propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomMapExpression, propertyMap.SourceMembers.ToList()));
var childFullName = sourceFullName.Substring(sourceFullName.IndexOf(period, StringComparison.OrdinalIgnoreCase) + 1);

FindDestinationFullName(sourceMemberInfo.GetMemberType(), propertyMap.CustomExpression == null
FindDestinationFullName(sourceMemberInfo.GetMemberType(), propertyMap.CustomMapExpression == null
? propertyMap.SourceMember.GetMemberType()
: propertyMap.CustomExpression.ReturnType, childFullName, propertyMapInfoList);
: propertyMap.CustomMapExpression.ReturnType, childFullName, propertyMapInfoList);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
Expand All @@ -7,11 +7,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="Shouldly" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>

<ItemGroup>
Expand Down