Skip to content

Commit 9769c4a

Browse files
committed
cezarypiatek#161: Add support for nested collection types
1 parent 2865c66 commit 9769c4a

File tree

7 files changed

+105
-1
lines changed

7 files changed

+105
-1
lines changed

MappingGenerator/MappingGenerator/MappingGenerator.Test/ExplicitConversions/ExplicitConversionTestCases.Designer.cs

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MappingGenerator/MappingGenerator/MappingGenerator.Test/ExplicitConversions/ExplicitConversionTestCases.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,10 @@
154154
<data name="_006_ExplicitConversionForInvalidAssigmentInInitBlockReuseInstance_FIXED" type="System.Resources.ResXFileRef, System.Windows.Forms">
155155
<value>TestCaseData\006_ExplicitConversionForInvalidAssigmentInInitBlockReuseInstance_FIXED.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
156156
</data>
157+
<data name="_007_ExplicitConversionForValueCollection" type="System.Resources.ResXFileRef, System.Windows.Forms">
158+
<value>TestCaseData\007_ExplicitConversionForValueCollection.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
159+
</data>
160+
<data name="_007_ExplicitConversionForValueCollection_FIXED" type="System.Resources.ResXFileRef, System.Windows.Forms">
161+
<value>TestCaseData\007_ExplicitConversionForValueCollection_FIXED.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
162+
</data>
157163
</root>

MappingGenerator/MappingGenerator/MappingGenerator.Test/ExplicitConversions/ExplicitConversionTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public void should_be_able_to_generate_conversion_by_re_using_instance_inside_in
4646
{
4747
TestCodeFix(ExplicitConversionTestCases._006_ExplicitConversionForInvalidAssigmentInInitBlockReuseInstance, ExplicitConversionTestCases._006_ExplicitConversionForInvalidAssigmentInInitBlockReuseInstance_FIXED, ExplicitConversionCodeFixProvider.CS0029, 1);
4848
}
49+
50+
[Test]
51+
public void should_be_able_to_generate_conversion_for_value_collection()
52+
{
53+
TestCodeFix(ExplicitConversionTestCases._007_ExplicitConversionForValueCollection, ExplicitConversionTestCases._007_ExplicitConversionForValueCollection_FIXED, ExplicitConversionCodeFixProvider.CS0029, 0);
54+
}
4955
protected override string LanguageName => LanguageNames.CSharp;
5056

5157
protected override CodeFixProvider CreateProvider()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MappingGenerator.Test.ExplicitConversions.TestCaseData
6+
{
7+
public class Source { }
8+
public class Destination { }
9+
10+
public class SampleMapper
11+
{
12+
public IEnumerable<Destination> Example(Dictionary<string, Source> source)
13+
{
14+
return [|source.Values|];
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MappingGenerator.Test.ExplicitConversions.TestCaseData
6+
{
7+
public class Source { }
8+
public class Destination { }
9+
10+
public class SampleMapper
11+
{
12+
public IEnumerable<Destination> Example(Dictionary<string, Source> source)
13+
{
14+
return source.Values.Select(sourceValue => new Destination());
15+
}
16+
}
17+
}

MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingEngine.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,11 @@ public async Task<ExpressionSyntax> CreateMappingLambdaAsync(string lambdaParame
438438

439439
private static SyntaxNode AddMaterializeCollectionInvocation(SyntaxGenerator generator, SyntaxNode sourceAccess, ITypeSymbol targetListType, bool isSourceNullable)
440440
{
441+
if (targetListType.TypeKind == TypeKind.Interface && targetListType.Name == "IEnumerable")
442+
{
443+
return sourceAccess;
444+
}
445+
441446
var materializeFunction = GetCollectionMaterializeFunction(targetListType);
442447
var toListAccess = SyntaxFactoryExtensions.CreateMemberAccessExpression((ExpressionSyntax)sourceAccess, isSourceNullable, materializeFunction);
443448
return generator.InvocationExpression(toListAccess);

MappingGenerator/MappingGenerator/MappingGenerator/Mappings/MappingHelper.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Linq;
33
using MappingGenerator.Features.Refactorings;
4-
using MappingGenerator.Mappings.SourceFinders;
54
using MappingGenerator.RoslynHelpers;
65
using Microsoft.CodeAnalysis;
76
using Microsoft.CodeAnalysis.CSharp;
@@ -45,6 +44,16 @@ public static AnnotatedType GetElementType(ITypeSymbol collectionType)
4544
}
4645
return GetElementType(namedType.BaseType);
4746
}
47+
48+
//INFO: Type is reported as generic when containing type is generic
49+
if (namedType.Arity == 0 && namedType.ContainingType?.TypeKind is {} containerType && (containerType == TypeKind.Class || containerType == TypeKind.Structure))
50+
{
51+
if (namedType.Interfaces.FirstOrDefault(x => x.Name == "IEnumerable" && x.IsGenericType) is {} enumerable)
52+
{
53+
return new AnnotatedType(enumerable.TypeParameters[0]);
54+
}
55+
}
56+
4857
return new AnnotatedType(namedType.TypeArguments[0]);
4958
case IArrayTypeSymbol arrayType:
5059
return new AnnotatedType(arrayType.ElementType);

0 commit comments

Comments
 (0)