Skip to content

Commit

Permalink
chore: remove IMethodSymbol from Dictionary and use strings
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Jul 9, 2023
1 parent 1edb08c commit ade2620
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Riok.Mapperly.Abstractions;
using Riok.Mapperly.Descriptors.Enumerables;
Expand All @@ -15,8 +14,9 @@ public static class DictionaryMappingBuilder
private const string CountPropertyName = nameof(IDictionary<object, object>.Count);
private const string SetterIndexerPropertyName = "set_Item";

private const string ToImmutableDictionaryMethodName = nameof(ImmutableDictionary.ToImmutableDictionary);
private const string ToImmutableSortedDictionaryMethodName = nameof(ImmutableSortedDictionary.ToImmutableSortedDictionary);
private const string ToImmutableDictionaryMethodName = "global::System.Collections.Immutable.ImmutableDictionary.ToImmutableDictionary";
private const string ToImmutableSortedDictionaryMethodName =
"global::System.Collections.Immutable.ImmutableSortedDictionary.ToImmutableSortedDictionary";

public static ITypeMapping? TryBuildMapping(MappingBuilderContext ctx)
{
Expand Down Expand Up @@ -155,34 +155,21 @@ out var isExplicit
return typedInter;
}

private static LinqDicitonaryMapping? ResolveImmutableCollectMethod(
private static LinqDictionaryMapping? ResolveImmutableCollectMethod(
MappingBuilderContext ctx,
ITypeMapping keyMapping,
ITypeMapping valueMapping
)
{
if (SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.Get(typeof(ImmutableSortedDictionary<,>))))
return new LinqDicitonaryMapping(
ctx.Source,
ctx.Target,
ctx.Types.Get(typeof(ImmutableSortedDictionary)).GetStaticGenericMethod(ToImmutableSortedDictionaryMethodName)!,
keyMapping,
valueMapping
);

// if target is an ImmutableDictionary or IImmutableDictionary
if (
SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.Get(typeof(IImmutableDictionary<,>)))
|| SymbolEqualityComparer.Default.Equals(ctx.Target.OriginalDefinition, ctx.Types.Get(typeof(ImmutableDictionary<,>)))
)
return new LinqDicitonaryMapping(
ctx.Source,
ctx.Target,
ctx.Types.Get(typeof(ImmutableDictionary)).GetStaticGenericMethod(ToImmutableDictionaryMethodName)!,
keyMapping,
valueMapping
);

return null;
return ctx.CollectionInfos!.Target.CollectionType switch
{
CollectionType.ImmutableSortedDictionary
=> new LinqDictionaryMapping(ctx.Source, ctx.Target, ToImmutableSortedDictionaryMethodName, keyMapping, valueMapping),
CollectionType.ImmutableDictionary
or CollectionType.IImmutableDictionary
=> new LinqDictionaryMapping(ctx.Source, ctx.Target, ToImmutableDictionaryMethodName, keyMapping, valueMapping),

_ => null,
};
}
}
12 changes: 6 additions & 6 deletions src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ namespace Riok.Mapperly.Descriptors.Mappings;
/// <summary>
/// Represents an enumerable mapping which works by using linq (select + collect).
/// </summary>
public class LinqDicitonaryMapping : TypeMapping
public class LinqDictionaryMapping : TypeMapping
{
private const string KeyPropertyName = nameof(KeyValuePair<object, object>.Key);
private const string ValuePropertyName = nameof(KeyValuePair<object, object>.Value);

private readonly IMethodSymbol _collectMethod;
private readonly string _collectMethod;
private readonly ITypeMapping _keyMapping;
private readonly ITypeMapping _valueMapping;

public LinqDicitonaryMapping(
public LinqDictionaryMapping(
ITypeSymbol sourceType,
ITypeSymbol targetType,
IMethodSymbol collectMethod,
string collectMethod,
ITypeMapping keyMapping,
ITypeMapping valueMapping
)
Expand All @@ -36,7 +36,7 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
// if key and value types do not change then use a simple call
// ie: source.ToImmutableDictionary();
if (_keyMapping.IsSynthetic && _valueMapping.IsSynthetic)
return StaticInvocation(_collectMethod, ctx.Source);
return Invocation(_collectMethod, ctx.Source);

// create expressions mapping the key and value and then create the final expression
// ie: source.ToImmutableDictionary(x => x.Key, x => (int)x.Value);
Expand All @@ -48,6 +48,6 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
var valueMapExpression = _valueMapping.Build(valueLambdaCtx);
var valueExpression = SimpleLambdaExpression(Parameter(Identifier(valueLambdaParamName))).WithExpressionBody(valueMapExpression);

return StaticInvocation(_collectMethod, ctx.Source, keyExpression, valueExpression);
return Invocation(_collectMethod, ctx.Source, keyExpression, valueExpression);
}
}

0 comments on commit ade2620

Please sign in to comment.