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

Commit

Permalink
#161: Add support for nested collection types
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Jan 20, 2021
1 parent 2865c66 commit 9769c4a
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,10 @@
<data name="_006_ExplicitConversionForInvalidAssigmentInInitBlockReuseInstance_FIXED" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TestCaseData\006_ExplicitConversionForInvalidAssigmentInInitBlockReuseInstance_FIXED.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="_007_ExplicitConversionForValueCollection" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TestCaseData\007_ExplicitConversionForValueCollection.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
<data name="_007_ExplicitConversionForValueCollection_FIXED" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>TestCaseData\007_ExplicitConversionForValueCollection_FIXED.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
</data>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public void should_be_able_to_generate_conversion_by_re_using_instance_inside_in
{
TestCodeFix(ExplicitConversionTestCases._006_ExplicitConversionForInvalidAssigmentInInitBlockReuseInstance, ExplicitConversionTestCases._006_ExplicitConversionForInvalidAssigmentInInitBlockReuseInstance_FIXED, ExplicitConversionCodeFixProvider.CS0029, 1);
}

[Test]
public void should_be_able_to_generate_conversion_for_value_collection()
{
TestCodeFix(ExplicitConversionTestCases._007_ExplicitConversionForValueCollection, ExplicitConversionTestCases._007_ExplicitConversionForValueCollection_FIXED, ExplicitConversionCodeFixProvider.CS0029, 0);
}
protected override string LanguageName => LanguageNames.CSharp;

protected override CodeFixProvider CreateProvider()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace MappingGenerator.Test.ExplicitConversions.TestCaseData
{
public class Source { }
public class Destination { }

public class SampleMapper
{
public IEnumerable<Destination> Example(Dictionary<string, Source> source)
{
return [|source.Values|];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace MappingGenerator.Test.ExplicitConversions.TestCaseData
{
public class Source { }
public class Destination { }

public class SampleMapper
{
public IEnumerable<Destination> Example(Dictionary<string, Source> source)
{
return source.Values.Select(sourceValue => new Destination());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ public async Task<ExpressionSyntax> CreateMappingLambdaAsync(string lambdaParame

private static SyntaxNode AddMaterializeCollectionInvocation(SyntaxGenerator generator, SyntaxNode sourceAccess, ITypeSymbol targetListType, bool isSourceNullable)
{
if (targetListType.TypeKind == TypeKind.Interface && targetListType.Name == "IEnumerable")
{
return sourceAccess;
}

var materializeFunction = GetCollectionMaterializeFunction(targetListType);
var toListAccess = SyntaxFactoryExtensions.CreateMemberAccessExpression((ExpressionSyntax)sourceAccess, isSourceNullable, materializeFunction);
return generator.InvocationExpression(toListAccess);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Linq;
using MappingGenerator.Features.Refactorings;
using MappingGenerator.Mappings.SourceFinders;
using MappingGenerator.RoslynHelpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -45,6 +44,16 @@ public static AnnotatedType GetElementType(ITypeSymbol collectionType)
}
return GetElementType(namedType.BaseType);
}

//INFO: Type is reported as generic when containing type is generic
if (namedType.Arity == 0 && namedType.ContainingType?.TypeKind is {} containerType && (containerType == TypeKind.Class || containerType == TypeKind.Structure))
{
if (namedType.Interfaces.FirstOrDefault(x => x.Name == "IEnumerable" && x.IsGenericType) is {} enumerable)
{
return new AnnotatedType(enumerable.TypeParameters[0]);
}
}

return new AnnotatedType(namedType.TypeArguments[0]);
case IArrayTypeSymbol arrayType:
return new AnnotatedType(arrayType.ElementType);
Expand Down

0 comments on commit 9769c4a

Please sign in to comment.