Skip to content

Commit

Permalink
change list to UseTargetValue on mapping to target
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Jan 29, 2020
1 parent 60bc45a commit 2131d5c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Mapster.Tests/Mapster.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ExpressionDebugger" Version="2.0.0" />
<PackageReference Include="ExpressionDebugger" Version="2.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
Expand Down
5 changes: 3 additions & 2 deletions src/Mapster.Tests/WhenMappingToTarget.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

Expand All @@ -18,13 +19,13 @@ public void MappingToTarget_With_SameList()
b.List.ShouldBe(new List<int>{1,2,3});
}

internal class Foo
public class Foo
{
public double A { get; set; }
public List<int> List { get; set; }
}

internal class Bar
public class Bar
{
public double A { get; set; }
public List<int> List { get; set; }
Expand Down
1 change: 1 addition & 0 deletions src/Mapster/Adapters/ArrayAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Mapster.Adapters
public class ArrayAdapter : BaseAdapter
{
protected override int Score => -123;
protected override ObjectType ObjectType => ObjectType.Collection;

protected override bool CanMap(PreCompileArgument arg)
{
Expand Down
12 changes: 11 additions & 1 deletion src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,19 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de
);
}

var result = Expression.Variable(arg.DestinationType, "result");
//if (object.ReferenceEquals(source, destination))
// return destination;
if (destination != null &&
!source.Type.GetTypeInfo().IsValueType &&
!destination.Type.GetTypeInfo().IsValueType &&
(source.Type.IsAssignableFrom(destination.Type) || destination.Type.IsAssignableFrom(source.Type)))
{
var refEquals = Expression.Call(typeof(object), nameof(ReferenceEquals), null, source, destination);
blocks.Add(Expression.IfThen(refEquals, Expression.Return(label, destination)));
}

//var result = new TDest();
var result = Expression.Variable(arg.DestinationType, "result");
var assign = Expression.Assign(result, set);
var assignActions = new List<Expression> {assign};

Expand Down
36 changes: 24 additions & 12 deletions src/Mapster/Adapters/CollectionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Mapster.Adapters
internal class CollectionAdapter : BaseAdapter
{
protected override int Score => -125;
protected override bool UseTargetValue => false;
protected override bool UseTargetValue => true;
protected override ObjectType ObjectType => ObjectType.Collection;

protected override bool CanMap(PreCompileArgument arg)
Expand Down Expand Up @@ -76,20 +76,32 @@ protected override Expression CreateInstantiationExpression(Expression source, E
protected override Expression CreateBlockExpression(Expression source, Expression destination, CompileArgument arg)
{
var destinationElementType = destination.Type.ExtractCollectionType();
var listType = destination.Type.GetGenericEnumerableType() != null
? typeof(ICollection<>).MakeGenericType(destinationElementType)
: typeof(IList);
var shouldConvert = destination.Type.GetMethod("Add", new[] { destinationElementType }) == null;

if (listType == destination.Type)
return CreateListSet(source, destination, arg);
//var list = (ICollection<>)dest;
var actions = new List<Expression>();
var list = destination;
if (shouldConvert)
{
var listType = destination.Type.GetGenericEnumerableType() != null
? typeof(ICollection<>).MakeGenericType(destinationElementType)
: typeof(IList);
list = Expression.Variable(listType, "list");
actions.Add(ExpressionEx.Assign(list, destination)); //convert to list type
}

var tmp = Expression.Variable(listType, "list");
var actions = new List<Expression>
//list.Clear();
if (arg.MapType == MapType.MapToTarget)
{
ExpressionEx.Assign(tmp, destination), //convert to list type
CreateListSet(source, tmp, arg)
};
return Expression.Block(new[] { tmp }, actions);
var clear = list.Type.GetMethod("Clear", Type.EmptyTypes);
actions.Add(Expression.Call(list, clear));
}

actions.Add(CreateListSet(source, list, arg));

if (shouldConvert)
return Expression.Block(new[] { (ParameterExpression)list }, actions);
return Expression.Block(actions);
}

protected override Expression CreateInlineExpression(Expression source, CompileArgument arg)
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Adapters/DictionaryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace Mapster.Adapters
internal class DictionaryAdapter : ClassAdapter
{
public static int DefaultScore { get; } = -124;

protected override int Score => DefaultScore; //must do before CollectionAdapter
protected override ObjectType ObjectType => ObjectType.Collection;

protected override bool CanMap(PreCompileArgument arg)
{
Expand Down
1 change: 1 addition & 0 deletions src/Mapster/Adapters/MultiDimensionalArrayAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Mapster.Adapters
public class MultiDimensionalArrayAdapter : BaseAdapter
{
protected override int Score => -122;
protected override ObjectType ObjectType => ObjectType.Collection;

protected override bool CanMap(PreCompileArgument arg)
{
Expand Down

0 comments on commit 2131d5c

Please sign in to comment.