Skip to content

Commit

Permalink
fix: allow same-name members with different casings (#1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
latonz authored Jun 20, 2024
1 parent 4c9df37 commit 0dd4a97
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public static MembersMappingState Build(MappingBuilderContext ctx)
unmappedSourceMemberNames.ExceptWith(ignoredSourceMemberNames);
targetMembers.RemoveRange(ignoredTargetMemberNames);

var targetMemberCaseMapping = targetMembers.Keys.ToDictionary(x => x, x => x, StringComparer.OrdinalIgnoreCase);
var targetMemberCaseMapping = targetMembers
.Keys.GroupBy(x => x, StringComparer.OrdinalIgnoreCase)
.ToDictionary(x => x.Key, x => x.First(), StringComparer.OrdinalIgnoreCase);
var unmappedTargetMemberNames = targetMembers.Keys.ToHashSet();
return new MembersMappingState(
unmappedSourceMemberNames,
Expand Down
29 changes: 29 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,35 @@ public void UnmappedReadOnlyTargetPropertyShouldNotDiagnostic()
);
}

[Fact]
public Task PropertiesWithCaseInsensitiveEqualNamesShouldWork()
{
var source = TestSourceBuilder.Mapping(
"A",
"B",
"class A { public int Value { get; set; } }",
"class B { public int value { get; set; } public int Value { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task PropertyConfigurationShouldPreferExactCasing()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
[MapProperty("Value", "value")]
[MapProperty("value", "Value")]
public partial B Map(A source);
""",
"class A { public int value { get; set; } public int Value { get; set; } }",
"class B { public int value { get; set; } public int Value { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public void ShouldIgnoreStaticProperty()
{
Expand Down
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.Value = source.Value;
return target;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Diagnostics: [
{
Id: RMG012,
Title: Source member was not found for target member,
Severity: Info,
WarningLevel: 1,
Location: : (11,4)-(11,36),
MessageFormat: The member {0} on the mapping target type {1} was not found on the mapping source type {2},
Message: The member value on the mapping target type B was not found on the mapping source type A,
Category: Mapper
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//HintName: Mapper.g.cs
// <auto-generated />
#nullable enable
public partial class Mapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public partial global::B Map(global::A source)
{
var target = new global::B();
target.value = source.Value;
target.Value = source.value;
return target;
}
}

0 comments on commit 0dd4a97

Please sign in to comment.