Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get mapper defaults for roslyn version smaller 4.4 #958

Merged
merged 1 commit into from
Dec 6, 2023
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
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
23 changes: 23 additions & 0 deletions test/Riok.Mapperly.IntegrationTests/Mapper/EnumMapper.cs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using Riok.Mapperly.Abstractions;

// this is tested with EnumMapper in MapperDefaultsTest
[assembly: MapperDefaults(EnumMappingStrategy = EnumMappingStrategy.ByName)]
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
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(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"),
};
}
}
}