Skip to content

Commit

Permalink
fix: support multiple mappers with the same name in different namespa…
Browse files Browse the repository at this point in the history
…ces (#123)
  • Loading branch information
latonz authored Aug 25, 2022
1 parent 5c36717 commit 15d9ec8
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Riok.Mapperly/Descriptors/DescriptorBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public DescriptorBuilder(
_mapperSymbol = mapperSymbol;
_context = sourceContext;
Compilation = compilation;
_mapperDescriptor = new MapperDescriptor(mapperSymbol.Name, mapperSyntax, mapperSymbol.IsStatic);
_mapperDescriptor = new MapperDescriptor(mapperSyntax, mapperSymbol.IsStatic);
MapperConfiguration = Configure();
}

Expand Down
6 changes: 1 addition & 5 deletions src/Riok.Mapperly/Descriptors/MapperDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ namespace Riok.Mapperly.Descriptors;

public class MapperDescriptor
{
private const string FileNameSuffix = ".g.cs";

private readonly List<TypeMapping> _mappings = new();

public MapperDescriptor(string name, ClassDeclarationSyntax syntax, bool isStatic)
public MapperDescriptor(ClassDeclarationSyntax syntax, bool isStatic)
{
FileName = name + FileNameSuffix;
Syntax = syntax;
IsStatic = isStatic;
}

public string? Namespace { get; set; }

public string FileName { get; }

public ClassDeclarationSyntax Syntax { get; }

public bool IsStatic { get; }
Expand Down
4 changes: 3 additions & 1 deletion src/Riok.Mapperly/MapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Riok.Mapperly;
[Generator]
public class MapperGenerator : IIncrementalGenerator
{
private const string GeneratedFileSuffix = ".g.cs";
private static readonly string _mapperAttributeName = typeof(MapperAttribute).FullName;

public void Initialize(IncrementalGeneratorInitializationContext context)
Expand Down Expand Up @@ -61,6 +62,7 @@ private static void Execute(Compilation compilation, ImmutableArray<ClassDeclara
if (mapperAttributeSymbol == null)
return;

var uniqueNameBuilder = new UniqueNameBuilder();
foreach (var mapperSyntax in mappers.Distinct())
{
var mapperModel = compilation.GetSemanticModel(mapperSyntax.SyntaxTree);
Expand All @@ -74,7 +76,7 @@ private static void Execute(Compilation compilation, ImmutableArray<ClassDeclara
var descriptor = builder.Build();

ctx.AddSource(
descriptor.FileName,
uniqueNameBuilder.Build(mapperSymbol.Name) + GeneratedFileSuffix,
SourceText.From(SourceEmitter.Build(descriptor).ToFullString(), Encoding.UTF8));
}
}
Expand Down
33 changes: 33 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/MapperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Riok.Mapperly.Tests.Mapping;

[UsesVerify]
public class MapperTest
{
[Fact]
public Task SameMapperNameInMultipleNamespacesShouldWork()
{
var source = @"
using Riok.Mapperly.Abstractions;
namespace Test.A
{
[Mapper]
internal partial class FooBarMapper
{
internal partial string FooToBar(string value);
}
}
namespace Test.B
{
[Mapper]
internal partial class FooBarMapper
{
internal partial string FooToBar(string value);
}
}
";

return TestHelper.VerifyGenerator(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//HintName: FooBarMapper.g.cs
#nullable enable
namespace Test.A
{
internal partial class FooBarMapper
{
internal partial string FooToBar(string value)
{
return value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//HintName: FooBarMapper1.g.cs
#nullable enable
namespace Test.B
{
internal partial class FooBarMapper
{
internal partial string FooToBar(string value)
{
return value;
}
}
}

0 comments on commit 15d9ec8

Please sign in to comment.