Skip to content

Commit

Permalink
fix: get mapper defaults for roslyn version smaller 4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ni507 committed Nov 29, 2023
1 parent 574cef7 commit 961d5d0
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static IncrementalValuesProvider<MapperDeclaration> GetMapperDeclarations
.SyntaxProvider
.CreateSyntaxProvider(
static (s, _) => s is CompilationUnitSyntax { AttributeLists.Count: > 0 },
static (ctx, ct) => GetMapperDefaultDeclarations(ctx, ct)
static (ctx, ct) => GetMapperDefaultDeclarations(ctx)
)
.Collect()
.Select((x, _) => x.FirstOrDefault());
Expand All @@ -45,12 +45,9 @@ public static IncrementalValuesProvider<MapperDeclaration> GetMapperDeclarations
return HasAttribute(symbol, MapperGenerator.MapperAttributeName) ? new MapperDeclaration(symbol, declaration) : null;
}

private static IAssemblySymbol? GetMapperDefaultDeclarations(GeneratorSyntaxContext ctx, CancellationToken cancellationToken)
private static IAssemblySymbol? GetMapperDefaultDeclarations(GeneratorSyntaxContext ctx)
{
var declaration = (CompilationUnitSyntax)ctx.Node;
if (ctx.SemanticModel.GetDeclaredSymbol(declaration, cancellationToken) is not IAssemblySymbol symbol)
return null;

var symbol = ctx.SemanticModel.Compilation.Assembly;
return HasAttribute(symbol, MapperGenerator.MapperDefaultsAttributeName) ? symbol : null;
}

Expand Down
28 changes: 28 additions & 0 deletions test/Riok.Mapperly.IntegrationTests/EnumMapperTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading.Tasks;
using Riok.Mapperly.IntegrationTests.Helpers;
using Riok.Mapperly.IntegrationTests.Mapper;
using VerifyXunit;
using Xunit;

namespace Riok.Mapperly.IntegrationTests
{
[UsesVerify]
public class EnumMapperTest : BaseMapperTest
{
[Fact]
[VersionedSnapshot(Versions.NET7_0)]
public Task SnapshotGeneratedSource()
{
var path = GetGeneratedMapperFilePath(nameof(EnumMapper));
return Verifier.VerifyFile(path);
}

[Fact]
[VersionedSnapshot(Versions.NET7_0)]
public Task RunMappingShouldWork()
{
var enum2 = Enum1.Value1.Map();
return Verifier.Verify(enum2);
}
}
}
24 changes: 24 additions & 0 deletions test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Riok.Mapperly.Abstractions;

[assembly: MapperDefaults(EnumMappingStrategy = EnumMappingStrategy.ByName)]

namespace Riok.Mapperly.IntegrationTests.Mapper
{
[Mapper]
public static partial class EnumMapper
{
public static partial Enum2 Map(this Enum1 e);
}

public enum Enum1
{
Value1 = 4,
Value2 = 7,
}

public enum Enum2
{
Value1,
Value2,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Value1
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// <auto-generated />
#nullable enable
namespace Riok.Mapperly.IntegrationTests.Mapper
{
public static partial class EnumMapper
{
public static partial global::Riok.Mapperly.IntegrationTests.Mapper.Enum2 Map(this global::Riok.Mapperly.IntegrationTests.Mapper.Enum1 e)
{
return e switch
{
global::Riok.Mapperly.IntegrationTests.Mapper.Enum1.Value1 => global::Riok.Mapperly.IntegrationTests.Mapper.Enum2.Value1,
global::Riok.Mapperly.IntegrationTests.Mapper.Enum1.Value2 => global::Riok.Mapperly.IntegrationTests.Mapper.Enum2.Value2,
_ => throw new System.ArgumentOutOfRangeException(nameof(e), e, "The value of enum Enum1 is not supported"),
};
}
}
}

0 comments on commit 961d5d0

Please sign in to comment.