Skip to content

Commit

Permalink
Simpler to just visit the parameter. (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaiseD authored Nov 25, 2022
1 parent 2e3c1f3 commit edb717f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 118 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using AutoMapper.Extensions.ExpressionMapping.Extensions;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Linq.Expressions;

namespace AutoMapper.Extensions.ExpressionMapping
{
Expand Down Expand Up @@ -29,112 +26,5 @@ protected override Expression VisitParameter(ParameterExpression node)

return base.VisitParameter(node);
}

protected override Expression VisitTypeBinary(TypeBinaryExpression node)
{
if (!(node.Expression is ParameterExpression))
return base.VisitTypeBinary(node);

if (!object.ReferenceEquals(CurrentParameter, node.GetParameterExpression()))
return base.VisitTypeBinary(node);

return Expression.TypeIs
(
string.IsNullOrEmpty(ParentFullName)
? NewParameter
: ExpressionHelpers.MemberAccesses(ParentFullName, NewParameter),
node.TypeOperand
);
}

protected override Expression VisitMember(MemberExpression node)
{
if (node.NodeType == ExpressionType.Constant)
return base.VisitMember(node);

if (!object.ReferenceEquals(CurrentParameter, node.GetParameterExpression()) || !node.IsMemberExpression())
return base.VisitMember(node);

return ExpressionHelpers.MemberAccesses
(
string.IsNullOrEmpty(ParentFullName)
? node.GetPropertyFullName()
: $"{ParentFullName}.{node.GetPropertyFullName()}",
NewParameter
);
}

protected override Expression VisitMethodCall(MethodCallExpression node)
{
if (!IsParentParameterExpression())
return base.VisitMethodCall(node);

if (!object.ReferenceEquals(CurrentParameter, node.GetParameterExpression()))
return base.VisitMethodCall(node);

if (node.Method.IsStatic)
{
if (!IsExtentionMethod())
return base.VisitMethodCall(node);

if (node.Method.IsGenericMethod)
return Expression.Call
(
node.Method.DeclaringType,
node.Method.Name,
node.Method.GetGenericArguments(),
GetNewArgumentsForExtensionMethod()
);
else
return Expression.Call(node.Method, GetNewArgumentsForExtensionMethod());
}

//instance method
if (node.Method.IsGenericMethod)
{
return Expression.Call
(
GetNewParent(),
node.Method.Name,
node.Method.GetGenericArguments(),
node.Arguments.ToArray()
);
}
else
{
return Expression.Call
(
GetNewParent(),
node.Method,
node.Arguments
);
}

Expression[] GetNewArgumentsForExtensionMethod()
{
Expression[] arguments = node.Arguments.ToArray();
arguments[0] = GetNewParent();
return arguments.ToArray();
}

Expression GetNewParent()
=> string.IsNullOrEmpty(ParentFullName)
? NewParameter
: ExpressionHelpers.MemberAccesses(ParentFullName, NewParameter);

bool IsParentParameterExpression()
{
if (node.Method.IsStatic)
return node.Arguments[0] is ParameterExpression;

if (!node.Method.IsStatic)
return node.Object is ParameterExpression;

return false;
}

bool IsExtentionMethod()
=> node.Method.IsDefined(typeof(ExtensionAttribute), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void Can_map_parameter_body_from_child_reference_without_member_expressio
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);

//Assert
Assert.Equal("x => (Convert(Property(x.Category, \"BrandId\"), Int32) == 2)", mappedExpression.ToString());
Assert.Equal("x => (Property(x.Category, \"BrandId\") == 2)", mappedExpression.ToString());
}

public class TestCategory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void Can_map_parameter_body_without_member_expression()
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);

//Assert
Assert.Equal("x => (Convert(Property(x, \"BrandId\"), Int32) == 2)", mappedExpression.ToString());
Assert.Equal("x => (Property(x, \"BrandId\") == 2)", mappedExpression.ToString());
}

public class TestProduct
Expand Down
8 changes: 3 additions & 5 deletions tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/EF.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
internal class EF
{
internal static object Property<T>(object p, string v)
internal static T Property<T>(object p, string v)
{
throw new NotImplementedException();
return default;
}
}
}

0 comments on commit edb717f

Please sign in to comment.