Skip to content

Commit

Permalink
fix: existing target unmapped required member should not lead to erro…
Browse files Browse the repository at this point in the history
…r diagnostic
  • Loading branch information
latonz committed Oct 31, 2024
1 parent f72d1b1 commit 832cb08
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Riok.Mapperly.Descriptors.MappingBodyBuilders.BuilderContext;

internal static class MemberMappingDiagnosticReporter
{
public static void ReportDiagnostics(MappingBuilderContext ctx, MembersMappingState state)
public static void ReportDiagnostics(MappingBuilderContext ctx, MembersMappingState state, bool requiredMembersNeedToBeMapped)
{
AddUnusedTargetMembersDiagnostics(ctx, state);
AddUnmappedSourceMembersDiagnostics(ctx, state);
AddUnmappedTargetMembersDiagnostics(ctx, state);
AddUnmappedTargetMembersDiagnostics(ctx, state, requiredMembersNeedToBeMapped);
AddUnmappedAdditionalSourceMembersDiagnostics(ctx, state);
AddNoMemberMappedDiagnostic(ctx, state);
}
Expand All @@ -33,11 +33,15 @@ private static void AddUnmappedSourceMembersDiagnostics(MappingBuilderContext ct
}
}

private static void AddUnmappedTargetMembersDiagnostics(MappingBuilderContext ctx, MembersMappingState state)
private static void AddUnmappedTargetMembersDiagnostics(
MappingBuilderContext ctx,
MembersMappingState state,
bool requiredMembersNeedToBeMapped
)
{
foreach (var targetMember in state.EnumerateUnmappedTargetMembers())
{
if (targetMember.IsRequired)
if (targetMember.IsRequired && requiredMembersNeedToBeMapped)
{
ctx.ReportDiagnostic(DiagnosticDescriptors.RequiredMemberNotMapped, targetMember.Name, ctx.Target, ctx.Source);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public abstract class MembersMappingBuilderContext<T>(MappingBuilderContext buil

public T Mapping { get; } = mapping;

public void AddDiagnostics()
public void AddDiagnostics(bool requiredMembersNeedToBeMapped)
{
MemberMappingDiagnosticReporter.ReportDiagnostics(BuilderContext, _state);
MemberMappingDiagnosticReporter.ReportDiagnostics(BuilderContext, _state, requiredMembersNeedToBeMapped);
_nestedMappingsContext.ReportDiagnostics();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void BuildMappingBody(MappingBuilderContext ctx, INewInstanceEnume
BuildConstructorMapping(mappingCtx);
NewInstanceObjectMemberMappingBodyBuilder.BuildInitMemberMappings(mappingCtx);
ObjectMemberMappingBodyBuilder.BuildMappingBody(mappingCtx);
mappingCtx.AddDiagnostics();
mappingCtx.AddDiagnostics(true);
}

public static void BuildMappingBody(MappingBuilderContext ctx, IEnumerableMapping mapping)
Expand All @@ -45,7 +45,7 @@ public static void BuildMappingBody(MappingBuilderContext ctx, IEnumerableMappin
}

ObjectMemberMappingBodyBuilder.BuildMappingBody(mappingCtx);
mappingCtx.AddDiagnostics();
mappingCtx.AddDiagnostics(false);
}

private static void InitContext<T>(MembersMappingBuilderContext<T> ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void BuildMappingBody(MappingBuilderContext ctx, NewInstanceObject
var mappingCtx = new NewInstanceBuilderContext<NewInstanceObjectMemberMapping>(ctx, mapping);
BuildConstructorMapping(mappingCtx);
BuildInitMemberMappings(mappingCtx, true);
mappingCtx.AddDiagnostics();
mappingCtx.AddDiagnostics(true);
}

public static void BuildMappingBody(MappingBuilderContext ctx, NewInstanceObjectMemberMethodMapping mapping)
Expand All @@ -29,7 +29,7 @@ public static void BuildMappingBody(MappingBuilderContext ctx, NewInstanceObject
BuildConstructorMapping(mappingCtx);
BuildInitMemberMappings(mappingCtx);
ObjectMemberMappingBodyBuilder.BuildMappingBody(mappingCtx);
mappingCtx.AddDiagnostics();
mappingCtx.AddDiagnostics(true);
}

public static IReadOnlyList<ConstructorParameterMapping> BuildConstructorMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public static void BuildMappingBody(MappingBuilderContext ctx, NewValueTupleCons
{
var mappingCtx = new NewValueTupleConstructorBuilderContext<NewValueTupleConstructorMapping>(ctx, expressionMapping);
BuildTupleConstructorMapping(mappingCtx);
mappingCtx.AddDiagnostics();
mappingCtx.AddDiagnostics(true);
}

public static void BuildMappingBody(MappingBuilderContext ctx, NewValueTupleExpressionMapping expressionMapping)
{
var mappingCtx = new NewValueTupleExpressionBuilderContext<NewValueTupleExpressionMapping>(ctx, expressionMapping);
BuildTupleConstructorMapping(mappingCtx);
ObjectMemberMappingBodyBuilder.BuildMappingBody(mappingCtx);
mappingCtx.AddDiagnostics();
mappingCtx.AddDiagnostics(true);
}

private static void BuildTupleConstructorMapping(INewValueTupleBuilderContext<INewValueTupleMapping> ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static void BuildMappingBody(MappingBuilderContext ctx, IMemberAssignment
// do not report "no member mapping" for existing target mappings
mappingCtx.MappingAdded();

mappingCtx.AddDiagnostics();
mappingCtx.AddDiagnostics(false);
}

public static void BuildMappingBody(IMembersContainerBuilderContext<IMemberAssignmentTypeMapping> ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,27 @@ public class B
"""
);
}

[Fact]
public Task UnmappedRequiredPropertyShouldDiagnostic()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"public partial void Update(A source, B target);",
"""
public class A
{
public required string Value { get; set; }
public required string OtherValue { get; set; }
}
""",
"""
public class B
{
public required string Value2 { get; set; }
public required string OtherValue { get; set; }
}
"""
);
return TestHelper.VerifyGenerator(source);
}
}

0 comments on commit 832cb08

Please sign in to comment.