Skip to content

Allow mapping to a dictionary with a key containing periods #780

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

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open
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
37 changes: 36 additions & 1 deletion src/Mapster.Tests/WhenMappingWithDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public void Object_To_Dictionary()
dict["Name"].ShouldBe(poco.Name);
}


[TestMethod]
public void Object_To_Dictionary_Map()
{
Expand All @@ -57,6 +56,25 @@ public void Object_To_Dictionary_Map()
dict["Name"].ShouldBe(poco.Name);
}

[TestMethod]
public void Object_To_Dictionary_Map_With_Periods()
{
var poco = new SimplePoco
{
Id = Guid.NewGuid(),
Name = "test",
};

var config = new TypeAdapterConfig();
config.NewConfig<SimplePoco, Dictionary<string, object>>()
.Map("Key.With.Periods", c => c.Id);
var dict = poco.Adapt<Dictionary<string, object>>(config);

dict.Count.ShouldBe(2);
dict["Key.With.Periods"].ShouldBe(poco.Id);
dict["Name"].ShouldBe(poco.Name);
}

[TestMethod]
public void Object_To_Dictionary_CamelCase()
{
Expand Down Expand Up @@ -154,6 +172,23 @@ public void Dictionary_To_Object_Map()
poco.Name.ShouldBeNull();
}

[TestMethod]
public void Dictionary_To_Object_Map_With_Periods()
{
var dict = new Dictionary<string, object>
{
["Key.With.Periods"] = Guid.NewGuid(),
["Foo"] = "test",
};

TypeAdapterConfig<Dictionary<string, object>, SimplePoco>.NewConfig()
.Map(c => c.Id, "Key.With.Periods");

var poco = TypeAdapter.Adapt<SimplePoco>(dict);
poco.Id.ShouldBe(dict["Key.With.Periods"]);
poco.Name.ShouldBeNull();
}

[TestMethod]
public void Dictionary_To_Object_CamelCase()
{
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 @@ -219,7 +219,7 @@ protected override Expression CreateInlineExpression(Expression source, CompileA
protected override ClassModel GetSetterModel(CompileArgument arg)
{
//get member name from map
var destNames = arg.GetDestinationNames().AsEnumerable();
var destNames = arg.GetDestinationNames(false).AsEnumerable();

//get member name from properties
if (arg.SourceType.GetDictionaryType() == null)
Expand Down
13 changes: 7 additions & 6 deletions src/Mapster/Compile/CompileArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Mapster.Utils;

namespace Mapster
{
Expand All @@ -20,16 +19,18 @@ public class CompileArgument
internal HashSet<string> GetSourceNames()
{
return _srcNames ??= (from it in Settings.Resolvers
where it.SourceMemberName != null
select it.SourceMemberName!.Split('.').First()).ToHashSet();
where it.SourceMemberName != null
select it.SourceMemberName!.Split('.').First()).ToHashSet();
}

private HashSet<string>? _destNames;
internal HashSet<string> GetDestinationNames()
internal HashSet<string> GetDestinationNames(bool split = true)
{
return _destNames ??= (from it in Settings.Resolvers
where it.DestinationMemberName != null
select it.DestinationMemberName.Split('.').First()).ToHashSet();
where it.DestinationMemberName != null
select split
? it.DestinationMemberName.Split('.').First()
: it.DestinationMemberName).ToHashSet();
}

private bool _fetchConstructUsing;
Expand Down