Skip to content

Commit

Permalink
fix: correctly handle mapping to read-only target arrays (#1562)
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz authored Oct 26, 2024
1 parent 52649c3 commit 55e3691
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Riok.Mapperly/Descriptors/Mappings/NoOpMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Riok.Mapperly.Descriptors.Mappings;

public class NoOpMapping(ITypeSymbol sourceType, ITypeSymbol targetType) : MethodMapping(sourceType, targetType), IExistingTargetMapping
public class NoOpMapping(ITypeSymbol sourceType, ITypeSymbol targetType) : IExistingTargetMapping
{
public override IEnumerable<StatementSyntax> BuildBody(TypeMappingBuildContext ctx) => [];
public ITypeSymbol SourceType => sourceType;
public ITypeSymbol TargetType => targetType;
public bool IsSynthetic => true;

public IEnumerable<StatementSyntax> Build(TypeMappingBuildContext ctx, ExpressionSyntax target) => [];
}
12 changes: 12 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/EnumerableExistingTargetTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,16 @@ public void MapToExistingInitOnlyCollection()
"""
);
}

[Fact]
public Task MapArrayToReadOnlyArrayShouldDiagnostic()
{
var source = TestSourceBuilder.Mapping(
"A",
"B",
"class A { public int[] Values { get; set; } public int IntValue { get; } }",
"class B { public int[] Values { get; } public int IntValue { set; } }"
);
return TestHelper.VerifyGenerator(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//HintName: Mapper.g.cs
// <auto-generated />
#nullable enable
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
private partial global::B Map(global::A source)
{
var target = new global::B();
target.IntValue = source.IntValue;
return target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Diagnostics: [
{
Id: RMG083,
Title: Cannot map to read only type,
Severity: Info,
WarningLevel: 1,
Location: : (11,4)-(11,36),
MessageFormat: Cannot map to read-only type {0},
Message: Cannot map to read-only type int[],
Category: Mapper
}
]
}

1 comment on commit 55e3691

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'SourceGeneratorBenchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 3.

Benchmark suite Current: 55e3691 Previous: e9abaf4 Ratio
Riok.Mapperly.Benchmarks.SourceGeneratorBenchmarks.Compile 2112441.565 ns (± 176879.59315719226) 120741.05671574519 ns (± 1678.1529391966976) 17.50
Riok.Mapperly.Benchmarks.SourceGeneratorBenchmarks.LargeCompile 47027562.26060606 ns (± 599948.1403286234) 1277208.939174107 ns (± 7926.104374412328) 36.82

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.