-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support fot
ImmutableDictionary
and `ImmutableSortedDicti…
…onary` (#351)
- Loading branch information
1 parent
6ec7e80
commit 587abbc
Showing
26 changed files
with
338 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Riok.Mapperly/Descriptors/Mappings/LinqDictionaryMapping.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
using static Riok.Mapperly.Emit.SyntaxFactoryHelper; | ||
|
||
namespace Riok.Mapperly.Descriptors.Mappings; | ||
|
||
/// <summary> | ||
/// Represents an enumerable mapping which works by using linq (select + collect). | ||
/// </summary> | ||
public class LinqDicitonaryMapping : TypeMapping | ||
{ | ||
private const string LambdaParamName = "x"; | ||
|
||
private const string KeyPropertyName = nameof(KeyValuePair<object, object>.Key); | ||
private const string ValuePropertyName = nameof(KeyValuePair<object, object>.Value); | ||
|
||
private readonly IMethodSymbol _collectMethod; | ||
private readonly ITypeMapping _keyMapping; | ||
private readonly ITypeMapping _valueMapping; | ||
|
||
public LinqDicitonaryMapping( | ||
ITypeSymbol sourceType, | ||
ITypeSymbol targetType, | ||
IMethodSymbol collectMethod, | ||
ITypeMapping keyMapping, | ||
ITypeMapping valueMapping) | ||
: base(sourceType, targetType) | ||
{ | ||
_collectMethod = collectMethod; | ||
_keyMapping = keyMapping; | ||
_valueMapping = valueMapping; | ||
} | ||
|
||
public override ExpressionSyntax Build(TypeMappingBuildContext ctx) | ||
{ | ||
var lambdaParamName = ctx.NameBuilder.New(LambdaParamName); | ||
|
||
// 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); | ||
|
||
// create expressions mapping the key and value and then create the final expression | ||
// ie: source.ToImmutableDictionary(x => x.Key, x=> (int)x.Value); | ||
var keyMapExpression = _keyMapping.Build(ctx.WithSource(MemberAccess(lambdaParamName, KeyPropertyName))); | ||
var keyExpression = SimpleLambdaExpression(Parameter(Identifier(lambdaParamName))) | ||
.WithExpressionBody(keyMapExpression); | ||
|
||
var valueMapExpression = _valueMapping.Build(ctx.WithSource(MemberAccess(lambdaParamName, ValuePropertyName))); | ||
var valueExpression = SimpleLambdaExpression(Parameter(Identifier(lambdaParamName))) | ||
.WithExpressionBody(valueMapExpression); | ||
|
||
return StaticInvocation(_collectMethod, ctx.Source, keyExpression, valueExpression); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.