Skip to content

Commit

Permalink
fix: prevent generation of empty if not null statements
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Jul 18, 2023
1 parent b238481 commit e5811d5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ public override IEnumerable<StatementSyntax> Build(TypeMappingBuildContext ctx,
if (!SourceType.IsNullable() && !TargetType.IsNullable())
return body;

var enumerated = body.ToArray();

// if body is empty don't generate an if statement
if (!enumerated.Any())
{
return Enumerable.Empty<StatementSyntax>();
}

// if (source != null && target != null) { body }
return new[] { IfStatement(IfNoneNull((SourceType, ctx.Source), (TargetType, target)), Block(body)), };
;
return new[] { IfStatement(IfNoneNull((SourceType, ctx.Source), (TargetType, target)), Block(enumerated)), };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,6 @@ public partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,6 @@ public static partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public partial int ParseableInt(string value)
private partial global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto MapToDtoInternal(global::Riok.Mapperly.IntegrationTests.Models.TestObject testObject)
{
var target = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto(DirectInt(testObject.CtorValue), ctorValue2: DirectInt(testObject.CtorValue2))
{IntInitOnlyValue = DirectInt(testObject.IntInitOnlyValue), RequiredValue = DirectInt(testObject.RequiredValue)};
{ IntInitOnlyValue = DirectInt(testObject.IntInitOnlyValue), RequiredValue = DirectInt(testObject.RequiredValue) };
if (testObject.NullableFlattening != null)
{
target.NullableFlatteningIdValue = CastIntNullable(testObject.NullableFlattening.IdValue);
Expand Down Expand Up @@ -142,7 +142,7 @@ public partial int ParseableInt(string value)
public partial global::Riok.Mapperly.IntegrationTests.Models.TestObject MapFromDto(global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDto dto)
{
var target = new global::Riok.Mapperly.IntegrationTests.Models.TestObject(DirectInt(dto.CtorValue), ctorValue2: DirectInt(dto.CtorValue2))
{IntInitOnlyValue = DirectInt(dto.IntInitOnlyValue), RequiredValue = DirectInt(dto.RequiredValue)};
{ IntInitOnlyValue = DirectInt(dto.IntInitOnlyValue), RequiredValue = DirectInt(dto.RequiredValue) };
if (dto.NullableUnflattening != null)
{
target.NullableUnflatteningIdValue = CastIntNullable(dto.NullableUnflattening.IdValue);
Expand Down Expand Up @@ -174,14 +174,9 @@ public partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
target.ImmutableArrayValue = global::System.Collections.Immutable.ImmutableArray.ToImmutableArray(global::System.Linq.Enumerable.Select(dto.ImmutableArrayValue, x => x.ToString()));
target.ImmutableListValue = global::System.Collections.Immutable.ImmutableList.ToImmutableList(global::System.Linq.Enumerable.Select(dto.ImmutableListValue, x => x.ToString()));
target.ImmutableHashSetValue = global::System.Collections.Immutable.ImmutableHashSet.ToImmutableHashSet(global::System.Linq.Enumerable.Select(dto.ImmutableHashSetValue, x => x.ToString()));
target.ImmutableQueueValue = global::System.Collections.Immutable.ImmutableQueue.CreateRange(global::System.Linq.Enumerable.Select(dto.ImmutableQueueValue, x => x.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,6 @@ public static partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ public partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down Expand Up @@ -444,4 +440,4 @@ private string MapToString1(global::Riok.Mapperly.IntegrationTests.Dto.TestEnumD
return target;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,6 @@ public static partial int ParseableInt(string value)
target.NestedNullableTargetNotNullable = MapToTestObjectNested(dto.NestedNullableTargetNotNullable);
target.StringNullableTargetNotNullable = dto.StringNullableTargetNotNullable;
target.SourceTargetSameObjectType = dto.SourceTargetSameObjectType;
if (dto.SpanValue != null)
{
}

target.MemoryValue = MapToStringArray(dto.MemoryValue.Span);
target.StackValue = new global::System.Collections.Generic.Stack<string>(global::System.Linq.Enumerable.Select(dto.StackValue, x => x.ToString()));
target.QueueValue = new global::System.Collections.Generic.Queue<string>(global::System.Linq.Enumerable.Select(dto.QueueValue, x => x.ToString()));
Expand Down
36 changes: 36 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/NullableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,42 @@ public void WithExistingInstanceNullableSource()
);
}

[Fact]
public void ToTargetShouldNotGenerateEmptyIfStatement()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"partial B Map(A source)",
"class A { public C Value { get; set; } }",
"class B { public D? Value { get; } }",
"class C { public int Id { get; set; } }",
"class D { public int Id { get; } }"
);

TestHelper
.GenerateMapper(source, TestHelperOptions.AllowDiagnostics)
.Should()
.HaveSingleMethodBody(
"""
var target = new global::B();
return target;
"""
);
}

[Fact]
public void ExistingTargetShouldNotGenerateEmptyIfStatement()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"partial void Map(A source, B destination)",
"class A { public C Value { get; set; } }",
"class B { public D? Value { get; } }",
"class C { public int Id { get; set; } }",
"class D { public int Id { get; } }"
);

TestHelper.GenerateMapper(source, TestHelperOptions.AllowDiagnostics).Should().HaveSingleMethodBody("");
}

[Fact]
public Task ShouldUpgradeNullabilityInDisabledNullableContext()
{
Expand Down

0 comments on commit e5811d5

Please sign in to comment.