Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@ private static IEnumerable<INewInstanceUserMapping> GetUserMappingCandidates(Map
.Where(x => x is not UserDefinedNewInstanceRuntimeTargetTypeMapping)
.OfType<INewInstanceUserMapping>();

// Generic dispatchers and generic user-implemented templates are open generic methods
// which cannot be invoked directly in a type switch arm.
// Concrete instantiations of the templates are registered as separate mappings once they are built.
private static IEnumerable<IExistingTargetUserMapping> GetExistingTargetUserMappingCandidates(MappingBuilderContext ctx) =>
ctx.ExistingTargetUserMappings.Where(x => x is not UserDefinedExistingTargetGenericTypeMapping);
ctx.ExistingTargetUserMappings.Where(x =>
x is not (UserDefinedExistingTargetGenericTypeMapping or GenericUserImplementedExistingTargetMethodMapping)
);

private static void BuildExistingTargetMappingBody(
MappingBuilderContext ctx,
Expand Down
22 changes: 22 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/UserMethodTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,28 @@ private void MapGeneric<TSource, TTarget>([MappingTarget] TTarget target, TSourc
);
}

[Fact]
public Task WithGenericUserImplementedExistingTargetAndExistingTargetDispatcherShouldWork()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
public partial void Map<TSource, TTarget>(TSource source, TTarget target);

public partial void MapA(A source, B target);

private void MapContainer<TSource, TTarget>(IContainer<TSource> source, IContainer<TTarget> target)
{
Map<IContainer<TSource>, IContainer<TTarget>>(source, target);
}
""",
"public interface IContainer<T> { T Value { get; set; } }",
"class A { public int Value { get; set; } }",
"class B { public int Value { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task WithGenericUserImplementedExistingTargetAndReferenceHandling()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//HintName: Mapper.g.cs
// <auto-generated />
#nullable enable
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public partial void Map<TSource, TTarget>(TSource source, TTarget target)
{
if (source == null || target == null)
return;
switch (source, target)
{
case (global::A source1, global::B target1):
MapA(source1, target1);
break;
default:
throw new global::System.ArgumentException($"Cannot map {source.GetType()} to {target.GetType()} as there is no known type mapping", nameof(source));
}
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public partial void MapA(global::A source, global::B target)
{
target.Value = source.Value;
}
}
Loading