-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add user implemented existing target mapping
- Loading branch information
1 parent
e8fc876
commit 1cff8e3
Showing
33 changed files
with
367 additions
and
13 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
75 changes: 75 additions & 0 deletions
75
....Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.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,75 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Riok.Mapperly.Descriptors.Mappings.ExistingTarget; | ||
using Riok.Mapperly.Emit; | ||
using Riok.Mapperly.Helpers; | ||
using Riok.Mapperly.Symbols; | ||
|
||
namespace Riok.Mapperly.Descriptors.Mappings.UserMappings; | ||
|
||
/// <summary> | ||
/// Represents an existing target type mapper which is implemented by the user. | ||
/// </summary> | ||
public class UserImplementedExistingTargetMethodMapping : ExistingTargetMapping, IUserMapping | ||
{ | ||
private readonly MethodParameter _sourceParameter; | ||
private readonly MethodParameter _targetParameter; | ||
private readonly MethodParameter? _referenceHandlerParameter; | ||
|
||
public UserImplementedExistingTargetMethodMapping( | ||
IMethodSymbol method, | ||
MethodParameter sourceParameter, | ||
MethodParameter targetParameter, | ||
MethodParameter? referenceHandlerParameter | ||
) | ||
: base(method.Parameters[0].Type.UpgradeNullable(), targetParameter.Type.UpgradeNullable()) | ||
{ | ||
Method = method; | ||
_sourceParameter = sourceParameter; | ||
_targetParameter = targetParameter; | ||
_referenceHandlerParameter = referenceHandlerParameter; | ||
} | ||
|
||
public IMethodSymbol Method { get; } | ||
|
||
public bool CallableByOtherMappings => true; | ||
public bool IsSynthetic => false; | ||
Check warning on line 37 in src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.cs Codecov / codecov/patchsrc/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.cs#L37
|
||
|
||
public ExpressionSyntax Build(TypeMappingBuildContext ctx) => | ||
throw new InvalidOperationException( | ||
$"{nameof(UserImplementedExistingTargetMethodMapping)} {ctx.Source}, {ctx.ReferenceHandler} does not support {nameof(Build)}" | ||
); | ||
Check warning on line 42 in src/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.cs Codecov / codecov/patchsrc/Riok.Mapperly/Descriptors/Mappings/UserMappings/UserImplementedExistingTargetMethodMapping.cs#L40-L42
|
||
|
||
public override IEnumerable<StatementSyntax> Build(TypeMappingBuildContext ctx, ExpressionSyntax target) | ||
{ | ||
// if the user implemented method is on an interface, | ||
// we explicitly cast to be able to use the default interface implementation or explicit implementations | ||
if (Method.ReceiverType?.TypeKind != TypeKind.Interface) | ||
{ | ||
yield return SyntaxFactory.ExpressionStatement( | ||
SyntaxFactoryHelper.Invocation( | ||
Method.Name, | ||
_sourceParameter.WithArgument(ctx.Source), | ||
_targetParameter.WithArgument(target), | ||
_referenceHandlerParameter?.WithArgument(ctx.ReferenceHandler) | ||
) | ||
); | ||
yield break; | ||
} | ||
|
||
var castedThis = SyntaxFactory.CastExpression( | ||
SyntaxFactoryHelper.FullyQualifiedIdentifier(Method.ReceiverType!), | ||
SyntaxFactory.ThisExpression() | ||
); | ||
var method = SyntaxFactoryHelper.MemberAccess(SyntaxFactory.ParenthesizedExpression(castedThis), Method.Name); | ||
yield return SyntaxFactory.ExpressionStatement( | ||
SyntaxFactoryHelper.Invocation( | ||
method, | ||
_sourceParameter.WithArgument(ctx.Source), | ||
_targetParameter.WithArgument(target), | ||
_referenceHandlerParameter?.WithArgument(ctx.ReferenceHandler) | ||
) | ||
); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,11 @@ | |
2, | ||
3 | ||
], | ||
ExistingList: [ | ||
1, | ||
2, | ||
3 | ||
], | ||
ISet: [ | ||
1, | ||
2, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,11 @@ | |
2, | ||
3 | ||
], | ||
ExistingList: [ | ||
1, | ||
2, | ||
3 | ||
], | ||
ISet: [ | ||
1, | ||
2, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,6 +139,11 @@ | |
2, | ||
3 | ||
], | ||
ExistingList: [ | ||
1, | ||
2, | ||
3 | ||
], | ||
ISet: [ | ||
1, | ||
2, | ||
|
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 |
---|---|---|
|
@@ -144,6 +144,11 @@ | |
2, | ||
3 | ||
], | ||
ExistingList: [ | ||
1, | ||
2, | ||
3 | ||
], | ||
ISet: [ | ||
1, | ||
2, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,6 +128,11 @@ | |
2, | ||
3 | ||
], | ||
ExistingList: [ | ||
1, | ||
2, | ||
3 | ||
], | ||
ISet: [ | ||
1, | ||
2, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,11 @@ | |
2, | ||
3 | ||
], | ||
ExistingList: [ | ||
1, | ||
2, | ||
3 | ||
], | ||
ISet: [ | ||
1, | ||
2, | ||
|
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.