Skip to content

Commit

Permalink
fix: required/init properties not using PropertyNameMappingStrategy (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianUrbina committed Jul 11, 2023
1 parent f19b833 commit de4a900
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static void BuildMappingBody(MappingBuilderContext ctx, NewInstanceObject

private static void BuildInitOnlyMemberMappings(INewInstanceBuilderContext<IMapping> ctx, bool includeAllMembers = false)
{
var memberNameComparer =
ctx.BuilderContext.MapperConfiguration.PropertyNameMappingStrategy == PropertyNameMappingStrategy.CaseSensitive
? StringComparer.Ordinal
: StringComparer.OrdinalIgnoreCase;

var initOnlyTargetMembers = includeAllMembers
? ctx.TargetMembers.Values.ToArray()
: ctx.TargetMembers.Values.Where(x => x.CanOnlySetViaInitializer()).ToArray();
Expand All @@ -52,6 +57,7 @@ private static void BuildInitOnlyMemberMappings(INewInstanceBuilderContext<IMapp
ctx.Mapping.SourceType,
MemberPathCandidateBuilder.BuildMemberPathCandidates(targetMember.Name),
ctx.IgnoredSourceMemberNames,
memberNameComparer,
ctx.BuilderContext.SymbolAccessor,
out var sourceMemberPath
)
Expand Down
8 changes: 0 additions & 8 deletions src/Riok.Mapperly/Symbols/MemberPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,6 @@ public override int GetHashCode()

private bool Equals(MemberPath other) => Path.SequenceEqual(other.Path);

public static bool TryFind(
ITypeSymbol type,
IEnumerable<IEnumerable<string>> pathCandidates,
IReadOnlyCollection<string> ignoredNames,
SymbolAccessor symbolAccessor,
[NotNullWhen(true)] out MemberPath? memberPath
) => TryFind(type, pathCandidates, ignoredNames, StringComparer.Ordinal, symbolAccessor, out memberPath);

public static bool TryFind(
ITypeSymbol type,
IEnumerable<IEnumerable<string>> pathCandidates,
Expand Down
9 changes: 6 additions & 3 deletions test/Riok.Mapperly.Tests/Mapping/ObjectPropertyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,19 @@ public void WithPropertyNameMappingStrategyCaseInsensitive()
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"partial B Map(A source);",
new TestSourceBuilderOptions { PropertyNameMappingStrategy = PropertyNameMappingStrategy.CaseInsensitive },
"class A { public string StringValue { get; set; } }",
"class B { public string stringvalue { get; set; } }"
"class A { public string StringValue { get; set; } public int Value { get; set; } }",
"class B { public string stringvalue { get; set; } public required int value { get; init; } }"
);

TestHelper
.GenerateMapper(source)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::B();
var target = new global::B()
{
value = source.Value
};
target.stringvalue = source.StringValue;
return target;
"""
Expand Down

0 comments on commit de4a900

Please sign in to comment.