Skip to content

Commit

Permalink
feat: use .Net 8 ToDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Sep 15, 2023
1 parent b54b9a5 commit 3ba19c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public static class DictionaryMappingBuilder
{
private const string SetterIndexerPropertyName = "set_Item";

private const string ToDictionaryName = "ToDictionary";
private const string ToDictionaryMethodName = "global::System.Collections.Generic.Enumerable.ToDictionary";
private const string ToImmutableDictionaryMethodName = "global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary";
private const string ToImmutableSortedDictionaryMethodName =
"global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary";
Expand All @@ -37,6 +39,32 @@ or CollectionType.IDictionary
or CollectionType.IReadOnlyDictionary
)
{
// use .NET 8 method .ToDictionary() if value and key mapping is synthetic
if (keyMapping.IsSynthetic && valueMapping.IsSynthetic && !keyMapping.TargetType.IsNullable())
{
var enumerableType = ctx.Types.Get(typeof(IEnumerable<>));
var dictionaryType = ctx.Types.Get(typeof(Dictionary<,>));

var toDictionaryMethod = ctx.Types
.Get(typeof(Enumerable))
.GetMembers(ToDictionaryName)
.OfType<IMethodSymbol>()
.FirstOrDefault(
x =>
x is { IsStatic: true, Parameters.Length: 1, IsGenericMethod: true }
&& SymbolEqualityComparer.Default.Equals(x.ReturnType.OriginalDefinition, dictionaryType)
&& SymbolEqualityComparer.Default.Equals(
((INamedTypeSymbol)x.Parameters[0].Type).ConstructedFrom,
enumerableType
)
);

if (toDictionaryMethod != null)
{
return new LinqDictionaryMapping(ctx.Source, ctx.Target, ToDictionaryMethodName, keyMapping, valueMapping);
}
}

var targetDictionarySymbol = ctx.Types.Get(typeof(Dictionary<,>)).Construct(keyMapping.TargetType, valueMapping.TargetType);
ctx.ObjectFactories.TryFindObjectFactory(ctx.Source, ctx.Target, out var dictionaryObjectFactory);
return new ForEachSetDictionaryMapping(
Expand Down
44 changes: 4 additions & 40 deletions test/Riok.Mapperly.Tests/Mapping/DictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,7 @@ public void DictionaryToSameDictionaryDeepCloning()
TestHelper
.GenerateMapper(source)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::System.Collections.Generic.Dictionary<string, long>(source.Count);
foreach (var item in source)
{
target[item.Key] = item.Value;
}
return target;
"""
);
.HaveSingleMethodBody("return global::System.Collections.Generic.Enumerable.ToDictionary(source);");
}

[Fact]
Expand Down Expand Up @@ -126,16 +117,7 @@ public void KeyValueEnumerableToIDictionary()
TestHelper
.GenerateMapper(source)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::System.Collections.Generic.Dictionary<string, int>();
foreach (var item in source)
{
target[item.Key] = item.Value;
}
return target;
"""
);
.HaveSingleMethodBody("return global::System.Collections.Generic.Enumerable.ToDictionary(source);");
}

[Fact]
Expand All @@ -145,16 +127,7 @@ public void CustomDictionaryToIDictionary()
TestHelper
.GenerateMapper(source)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::System.Collections.Generic.Dictionary<string, int>(source.Count);
foreach (var item in source)
{
target[item.Key] = item.Value;
}
return target;
"""
);
.HaveSingleMethodBody("return global::System.Collections.Generic.Enumerable.ToDictionary(source);");
}

[Fact]
Expand All @@ -164,16 +137,7 @@ public void CustomKeyValueListToIDictionary()
TestHelper
.GenerateMapper(source)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::System.Collections.Generic.Dictionary<string, int>(source.Count);
foreach (var item in source)
{
target[item.Key] = item.Value;
}
return target;
"""
);
.HaveSingleMethodBody("return global::System.Collections.Generic.Enumerable.ToDictionary(source);");
}

[Fact]
Expand Down

0 comments on commit 3ba19c4

Please sign in to comment.