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 Dec 5, 2023
1 parent 574cef7 commit 6e01a7c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 10 deletions.
9 changes: 3 additions & 6 deletions src/Riok.Mapperly/SyntaxProvider.Roslyn4.0.cs
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
23 changes: 23 additions & 0 deletions test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs
@@ -0,0 +1,23 @@
using Riok.Mapperly.Abstractions;

// is used to test mapper defaults
namespace Riok.Mapperly.IntegrationTests.Mapper
{
[Mapper]
public static partial class EnumMapper
{
public static partial Enum2 Map(Enum1 e);
}

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

public enum Enum2
{
Value1,
Value2,
}
}
4 changes: 4 additions & 0 deletions test/Riok.Mapperly.IntegrationTests/Mapper/MapperDefaults.cs
@@ -0,0 +1,4 @@
using Riok.Mapperly.Abstractions;

// this is tested with EnumMapper in MapperDefaultsTest
[assembly: MapperDefaults(EnumMappingStrategy = EnumMappingStrategy.ByName)]
Expand Up @@ -5,7 +5,7 @@

namespace Riok.Mapperly.IntegrationTests.Mapper
{
[Mapper]
[Mapper(EnumMappingStrategy = EnumMappingStrategy.ByValue)]
public static partial class ProjectionMapper
{
public static partial IQueryable<TestObjectDtoProjection> ProjectToDto(this IQueryable<TestObjectProjection> q);
Expand Down
Expand Up @@ -6,7 +6,7 @@

namespace Riok.Mapperly.IntegrationTests.Mapper
{
[Mapper]
[Mapper(EnumMappingStrategy = EnumMappingStrategy.ByValue)]
public static partial class StaticTestMapper
{
public static partial int DirectInt(int value);
Expand Down
4 changes: 2 additions & 2 deletions test/Riok.Mapperly.IntegrationTests/Mapper/TestMapper.cs
Expand Up @@ -12,9 +12,9 @@
namespace Riok.Mapperly.IntegrationTests.Mapper
{
#if NET8_0_OR_GREATER
[Mapper(IncludedMembers = MemberVisibility.All)]
[Mapper(IncludedMembers = MemberVisibility.All, EnumMappingStrategy = EnumMappingStrategy.ByValue)]
#else
[Mapper]
[Mapper(EnumMappingStrategy = EnumMappingStrategy.ByValue)]
#endif
public partial class TestMapper
{
Expand Down
26 changes: 26 additions & 0 deletions test/Riok.Mapperly.IntegrationTests/MapperDefaultsTest.cs
@@ -0,0 +1,26 @@
using System.Threading.Tasks;
using FluentAssertions;
using Riok.Mapperly.IntegrationTests.Mapper;
using VerifyXunit;
using Xunit;

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

[Fact]
public void RunMappingShouldWork()
{
var enum2 = EnumMapper.Map(Enum1.Value1);
enum2.Should().Be(Enum2.Value1);
}
}
}
@@ -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(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 6e01a7c

Please sign in to comment.