-
-
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: use IEnumerable constructors where possible (#342)
- Loading branch information
1 parent
b5f4559
commit 74d0b52
Showing
9 changed files
with
293 additions
and
459 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
50 changes: 50 additions & 0 deletions
50
src/Riok.Mapperly/Descriptors/Mappings/LinqConstructorMapping.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,50 @@ | ||
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 where the target type accepts IEnumerable as a single argument. | ||
/// </summary> | ||
public class LinqConstructorMapping : TypeMapping | ||
{ | ||
private const string LambdaParamName = "x"; | ||
|
||
private readonly ITypeMapping _elementMapping; | ||
private readonly IMethodSymbol? _selectMethod; | ||
|
||
public LinqConstructorMapping( | ||
ITypeSymbol sourceType, | ||
ITypeSymbol targetType, | ||
ITypeMapping elementMapping, | ||
IMethodSymbol? selectMethod) | ||
: base(sourceType, targetType) | ||
{ | ||
_elementMapping = elementMapping; | ||
_selectMethod = selectMethod; | ||
} | ||
|
||
public override ExpressionSyntax Build(TypeMappingBuildContext ctx) | ||
{ | ||
var lambdaParamName = ctx.NameBuilder.New(LambdaParamName); | ||
|
||
ExpressionSyntax mappedSource; | ||
|
||
// Select / Map if needed | ||
if (_selectMethod != null) | ||
{ | ||
var sourceMapExpression = _elementMapping.Build(ctx.WithSource(lambdaParamName)); | ||
var convertLambda = SimpleLambdaExpression(Parameter(Identifier(lambdaParamName))) | ||
.WithExpressionBody(sourceMapExpression); | ||
mappedSource = StaticInvocation(_selectMethod, ctx.Source, convertLambda); | ||
} | ||
else | ||
{ | ||
mappedSource = _elementMapping.Build(ctx); | ||
} | ||
|
||
return CreateInstance(TargetType, mappedSource); | ||
} | ||
} |
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.