Skip to content

Commit

Permalink
feat: semi fix nullable value type constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison committed Oct 10, 2023
1 parent c4eed62 commit dbdda06
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ public static class CtorMappingBuilder
// resolve ctors which have the source as single argument
var ctorMethod = namedTarget.InstanceConstructors
.Where(ctx.SymbolAccessor.IsAccessible)
.FirstOrDefault(
m =>
m.Parameters.Length == 1
&& SymbolEqualityComparer.Default.Equals(m.Parameters[0].Type, ctx.Source)
&& ctx.Source.HasSameOrStricterNullability(m.Parameters[0].Type)
);
.FirstOrDefault(m => SameWithStricterNullability(m, ctx.Source));

return ctorMethod == null ? null : new CtorMapping(ctx.Source, ctx.Target);
}

private static bool SameWithStricterNullability(IMethodSymbol m, ITypeSymbol src)
{
var parameter = m.Parameters[0].Type;
var paramType = parameter.IsNullableValueType() ? ((INamedTypeSymbol)parameter).TypeArguments[0] : parameter;
return m.Parameters.Length == 1
&& SymbolEqualityComparer.Default.Equals(paramType, src)
&& src.HasSameOrStricterNullability(paramType);
}
}
26 changes: 26 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/CtorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@ public void PrimaryCtorCustomClass()
TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return new global::A(source);");
}

[Fact]
public void CtorClassNullableSource()
{
var source = TestSourceBuilder.Mapping("int?", "A", "class A { public A(int x) {} }");
TestHelper
.GenerateMapper(source)
.Should()
.HaveSingleMethodBody(
"return source == null ? throw new System.ArgumentNullException(nameof(source)) : new global::A(source.Value);"
);
}

[Fact]
public void CtorClassNullableParameter()
{
var source = TestSourceBuilder.Mapping("int?", "A", "class A { public A(int? x) {} }");
TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return new global::A(source);");
}

[Fact]
public void CtorClassNonNullSourceNullableParameter()
{
var source = TestSourceBuilder.Mapping("int", "A", "class A { public A(int? x) {} }");
TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return new global::A(source);");
}

[Fact]
public void CtorMappingDisabledShouldDiagnostic()
{
Expand Down

0 comments on commit dbdda06

Please sign in to comment.