-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add MapperIgnoreMemberAttribute to ignore members at declaration (
#1143) * feat: Added a MapperIgnoreMemberAttribute to allow mapping to be ignored from within a source/target class. Works the same as the ObsoleteAttribute without the configuration due to being explicitly specified.
- Loading branch information
Showing
6 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Diagnostics; | ||
|
||
namespace Riok.Mapperly.Abstractions; | ||
|
||
/// <summary> | ||
/// Ignores a member from the mapping. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] | ||
[Conditional("MAPPERLY_ABSTRACTIONS_SCOPE_RUNTIME")] | ||
public sealed class MapperIgnoreAttribute : Attribute; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Riok.Mapperly.Diagnostics; | ||
|
||
namespace Riok.Mapperly.Tests.Mapping; | ||
|
||
[UsesVerify] | ||
public class IgnoreAttributeTest | ||
{ | ||
private readonly string _classA = TestSourceBuilder.CSharp( | ||
""" | ||
class A | ||
{ | ||
public int Value { get; set; } | ||
[MapperIgnore] | ||
public int Ignored { get; set; } | ||
} | ||
""" | ||
); | ||
|
||
private readonly string _classB = TestSourceBuilder.CSharp( | ||
""" | ||
class B | ||
{ | ||
public int Value { get; set; } | ||
[MapperIgnore] | ||
public int Ignored { get; set; } | ||
} | ||
""" | ||
); | ||
|
||
[Fact] | ||
public void ClassAttributeIgnoreMember() | ||
{ | ||
var source = TestSourceBuilder.Mapping("A", "B", TestSourceBuilderOptions.Default, _classA, _classB); | ||
|
||
TestHelper | ||
.GenerateMapper(source) | ||
.Should() | ||
.HaveSingleMethodBody( | ||
""" | ||
var target = new global::B(); | ||
target.Value = source.Value; | ||
return target; | ||
""" | ||
); | ||
} | ||
|
||
[Fact] | ||
public void MapPropertyOverridesIgnoreMember() | ||
{ | ||
var source = TestSourceBuilder.MapperWithBodyAndTypes( | ||
""" | ||
[MapProperty("Ignored", "Ignored")] | ||
partial B Map(A source); | ||
""", | ||
_classA, | ||
_classB | ||
); | ||
|
||
TestHelper | ||
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics) | ||
.Should() | ||
.HaveSingleMethodBody( | ||
""" | ||
var target = new global::B(); | ||
target.Value = source.Value; | ||
target.Ignored = source.Ignored; | ||
return target; | ||
""" | ||
) | ||
.HaveDiagnostic( | ||
DiagnosticDescriptors.IgnoredSourceMemberExplicitlyMapped, | ||
"The source member Ignored on A is ignored, but is also mapped by the MapPropertyAttribute" | ||
) | ||
.HaveDiagnostic( | ||
DiagnosticDescriptors.IgnoredTargetMemberExplicitlyMapped, | ||
"The target member Ignored on B is ignored, but is also mapped by the MapPropertyAttribute" | ||
) | ||
.HaveAssertedAllDiagnostics(); | ||
} | ||
} |