Skip to content

Commit

Permalink
fix #4 mapping condition cascading wrong order
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Jun 19, 2017
1 parent 68435d7 commit 38cef73
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="AutoMapper, Version=6.0.2.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<HintPath>..\packages\AutoMapper.6.0.2\lib\net45\AutoMapper.dll</HintPath>
<Reference Include="AutoMapper, Version=6.1.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
<HintPath>..\packages\AutoMapper.6.1.0\lib\net45\AutoMapper.dll</HintPath>
</Reference>
<Reference Include="ExpressMapper, Version=1.9.1.0, Culture=neutral, PublicKeyToken=ac363faa09311ba0, processorArchitecture=MSIL">
<HintPath>..\packages\Expressmapper.1.9.1\lib\net45\ExpressMapper.dll</HintPath>
</Reference>
<Reference Include="Mapster, Version=3.0.2.0, Culture=neutral, PublicKeyToken=2f39883af23c29a8, processorArchitecture=MSIL">
<HintPath>..\packages\Mapster.3.0.2\lib\net45\Mapster.dll</HintPath>
<Reference Include="Mapster, Version=3.1.0.0, Culture=neutral, PublicKeyToken=2f39883af23c29a8, processorArchitecture=MSIL">
<HintPath>..\packages\Mapster.3.1.0\lib\net45\Mapster.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
4 changes: 2 additions & 2 deletions src/Benchmark/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AutoMapper" version="6.0.2" targetFramework="net451" />
<package id="AutoMapper" version="6.1.0" targetFramework="net451" />
<package id="Expressmapper" version="1.9.1" targetFramework="net451" />
<package id="Mapster" version="3.0.2" targetFramework="net451" />
<package id="Mapster" version="3.1.0" targetFramework="net451" />
</packages>
1 change: 0 additions & 1 deletion src/Mapster.Tests/WhenMappingArrays.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ public void List_To_Multi_Dimensional_Array_Is_Mapped()
public void Multi_Dimensional_Array_To_List_Is_Mapped()
{
var source = new [,] {{1, 2}, {3, 4}};
TypeAdapterConfig.GlobalSettings.EnableDebugging();
var target = source.Adapt<List<int>>();
target[0].ShouldBe(1);
target[1].ShouldBe(2);
Expand Down
21 changes: 21 additions & 0 deletions src/Mapster.Tests/WhenMappingConditionally.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

Expand Down Expand Up @@ -38,6 +39,26 @@ public void Failed_Condition_Primitive_Does_Not_Map()
dto.Name.ShouldBeNull();
}

[TestMethod]
public void Map_Multiple_Condition()
{
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.Map(dest => dest.Name, src => "1", cond => cond.Name == "1")
.Map(dest => dest.Name, src => "2", cond => cond.Name == "2")
.Map(dest => dest.Name, src => "3", cond => cond.Name == "3")
.Map(dest => dest.Name, src => "4", cond => cond.Name == "4")
.Map(dest => dest.Name, src => "5", cond => cond.Name == "5")
.Map(dest => dest.Name, src => "0");

var list = Enumerable.Range(0, 6).Select(i => new SimplePoco {Name = i.ToString()}).ToList();
var dtos = list.Adapt<List<SimpleDto>>();

for (var i = 0; i < list.Count; i++)
{
dtos[i].Name.ShouldBe(i.ToString());
}
}

[TestMethod]
public void Passed_Condition_Primitive_Does_Map()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Mapster.NetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>3.1.0</Version>
<Version>3.1.1</Version>
<RootNamespace>Mapster</RootNamespace>
</PropertyGroup>

Expand Down
33 changes: 25 additions & 8 deletions src/Mapster/Settings/ValueAccessingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,41 @@ private static Expression CustomResolverFn(Expression source, IMemberModel desti
if (resolvers == null || resolvers.Count <= 0)
return null;

var invokes = new List<Tuple<Expression, Expression>>();

Expression getter = null;
LambdaExpression lastCondition = null;
foreach (var resolver in resolvers)
{
if (!destinationMember.Name.Equals(resolver.DestinationMemberName))
continue;
Expression invoke = resolver.Invoker == null
var invoke = resolver.Invoker == null
? Expression.PropertyOrField(source, resolver.SourceMemberName)
: resolver.Invoker.Apply(source);
getter = lastCondition != null
? Expression.Condition(lastCondition.Apply(source), getter, invoke)
: invoke;
lastCondition = resolver.Condition;

if (resolver.Condition == null)
{
getter = invoke;
break;
}

var condition = resolver.Condition.Apply(source);
invokes.Add(Tuple.Create(condition, invoke));
}
if (lastCondition != null)
getter = Expression.Condition(lastCondition.Apply(source), getter, Expression.Constant(getter.Type.GetDefault(), getter.Type));

if (invokes.Count > 0)
{
invokes.Reverse();
if (getter == null)
{
var type = invokes[0].Item2.Type;
getter = Expression.Constant(type.GetDefault(), type);
}
foreach (var invoke in invokes)
{
getter = Expression.Condition(invoke.Item1, invoke.Item2, getter);
}
}

return getter;
}

Expand Down

0 comments on commit 38cef73

Please sign in to comment.