Skip to content

Commit

Permalink
fix #11 map to nullable enum
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Oct 9, 2017
1 parent d7d5900 commit 4a994dc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
32 changes: 32 additions & 0 deletions src/Mapster.Tests/WhenMappingEnums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ public void String_Is_Mapped_To_Enum()
dto.Department.ShouldBe(Departments.IT);
}

[TestMethod]
public void String_Is_Mapped_To_Nullable_Enum()
{
var department = Departments.IT.ToString();
var value = TypeAdapter.Adapt<string, Departments?>(department);
value.ShouldBe(Departments.IT);
}

[TestMethod]
public void Null_String_Is_Mapped_To_Nullable_Enum()
{
string department = null;
var value = TypeAdapter.Adapt<string, Departments?>(department);
value.ShouldBeNull();
}

[TestMethod]
public void Nullable_Enum_Is_Mapped_To_String()
{
var department = Departments.IT;
var value = TypeAdapter.Adapt<Departments?, string>(department);
value.ShouldBe(Departments.IT.ToString());
}

[TestMethod]
public void Null_Nullable_Enum_Is_Mapped_To_String()
{
Departments? department = null;
var value = TypeAdapter.Adapt<Departments?, string>(department);
value.ShouldBeNull();
}

[TestMethod]
public void Enum_Is_Mapped_To_Enum()
{
Expand Down
3 changes: 2 additions & 1 deletion src/Mapster/Adapters/EnumAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal class EnumAdapter : PrimitiveAdapter

protected override bool CanMap(PreCompileArgument arg)
{
return arg.SourceType.GetTypeInfo().IsEnum || arg.DestinationType.GetTypeInfo().IsEnum;
return arg.SourceType.UnwrapNullable().GetTypeInfo().IsEnum
|| arg.DestinationType.UnwrapNullable().GetTypeInfo().IsEnum;
}

protected override Expression ConvertType(Expression source, Type destinationType, CompileArgument arg)
Expand Down
4 changes: 1 addition & 3 deletions src/Mapster/Mapster.NetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>3.1.4</Version>
<Version>3.1.5</Version>
<RootNamespace>Mapster</RootNamespace>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
</ItemGroup>

Expand Down

0 comments on commit 4a994dc

Please sign in to comment.