Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
Fix generating mapping lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Aug 18, 2018
1 parent 77653b8 commit d6c1b85
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,38 @@ private static async Task<Document> CreateMappingLambda(Document document, Metho

var sourceElementType = ((INamedTypeSymbol)mappingOverload.Parameters[0].Type).TypeArguments[0];
var targetElementType = GetExpressionType(semanticModel, invocation.SourceNode);
if (targetElementType == null)
{
return document;
}
var mappingEngine = new MappingEngine(semanticModel, syntaxGenerator);
var mappingLambda = mappingEngine.CreateMappingLambda("x", sourceElementType, targetElementType, new MappingPath());
return await document.ReplaceNodes(invocation.SourceNode, invocation.WithArgumentList(SyntaxFactory.ArgumentList().AddArguments(SyntaxFactory.Argument((ExpressionSyntax)mappingLambda))), cancellationToken);
}

private static ITypeSymbol GetExpressionType(SemanticModel semanticModel, SyntaxNode sourceNodeParent)
{
if (sourceNodeParent == null)
{
return null;
}

if (sourceNodeParent is MethodDeclarationSyntax methodDeclaration)
{
var returnTypeInfo = semanticModel.GetTypeInfo(methodDeclaration.ReturnType);
if (returnTypeInfo.Type != null && MappingHelper.IsCollection(returnTypeInfo.Type))
{
return MappingHelper.GetElementType(returnTypeInfo.Type);
}

return null;
}

if (sourceNodeParent is LocalDeclarationStatementSyntax localDeclaration && localDeclaration.Declaration.Type.IsVar)
{
return null;
}

var typeInfo = semanticModel.GetTypeInfo(sourceNodeParent);
if (typeInfo.ConvertedType != null && typeInfo.ConvertedType.Kind != SymbolKind.ErrorType && typeInfo.ConvertedType is INamedTypeSymbol nt)
return nt.TypeArguments[0];
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ CS7036 There is no argument given that corresponds to the required formal parame
- Provide local accessoble variables as parameters for method and constructor invocation
![locals as parameters](doc/localsforconstructor.gif)

- Create missing mapping lambda
![mapping lambda](doc/mapping_lambda.gif)


## Object scaffolding

Expand Down
Binary file added doc/mapping_lambda.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d6c1b85

Please sign in to comment.