Skip to content

Commit

Permalink
fix: expand target type new when inlining in queryable projection map…
Browse files Browse the repository at this point in the history
…pings (#1238)
  • Loading branch information
latonz committed Apr 15, 2024
1 parent b3f79e8 commit 840b6e1
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 7 deletions.
22 changes: 21 additions & 1 deletion src/Riok.Mapperly/Descriptors/InlineExpressionRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,33 @@ public override SyntaxNode VisitTypeArgumentList(TypeArgumentListSyntax node)
if (semanticModel.GetSymbolInfo(node.Expression).Symbol is ITypeSymbol namedTypeSymbol)
{
var expression = FullyQualifiedIdentifier(namedTypeSymbol).WithTriviaFrom(node.Expression);

return node.WithExpression(expression);
}

return base.VisitMemberAccessExpression(node);
}

public override SyntaxNode VisitImplicitObjectCreationExpression(ImplicitObjectCreationExpressionSyntax node)
{
if (
semanticModel.GetSymbolInfo(node).Symbol is IMethodSymbol
{
MethodKind: MethodKind.Constructor,
ReceiverType: not null
} ctorSymbol
)
{
return ObjectCreationExpression(
TrailingSpacedToken(SyntaxKind.NewKeyword),
FullyQualifiedIdentifier(ctorSymbol.ReceiverType),
(ArgumentListSyntax?)base.VisitArgumentList(node.ArgumentList),
node.Initializer == null ? null : (InitializerExpressionSyntax?)base.VisitInitializerExpression(node.Initializer)
);
}

return node;
}

public override SyntaxNode VisitArrayType(ArrayTypeSyntax node)
{
var fullyQualifiedElementType = FullyQualifiedType(node.ElementType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static partial class ProjectionMapper
} : default,
DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(x.DateTimeValueTargetDateOnly),
DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(x.DateTimeValueTargetTimeOnly),
ManuallyMapped = new(100) { StringValue = x.ManuallyMapped },
ManuallyMapped = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoManuallyMappedProjection(100) { StringValue = x.ManuallyMapped },
ManuallyMappedModified = x.ManuallyMappedModified + 10,
ManuallyMappedList = global::System.Linq.Enumerable.ToList(global::System.Linq.Enumerable.Select(x.ManuallyMappedList, x1 => x1.Value)),
IntegerValues = global::System.Linq.Enumerable.ToList(global::System.Linq.Enumerable.OrderBy(x.IntegerValues, x => x.Value)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static partial class ProjectionMapper
} : default,
DateTimeValueTargetDateOnly = global::System.DateOnly.FromDateTime(x.DateTimeValueTargetDateOnly),
DateTimeValueTargetTimeOnly = global::System.TimeOnly.FromDateTime(x.DateTimeValueTargetTimeOnly),
ManuallyMapped = new(100) { StringValue = x.ManuallyMapped },
ManuallyMapped = new global::Riok.Mapperly.IntegrationTests.Dto.TestObjectDtoManuallyMappedProjection(100) { StringValue = x.ManuallyMapped },
ManuallyMappedModified = x.ManuallyMappedModified + 10,
ManuallyMappedList = global::System.Linq.Enumerable.ToList(global::System.Linq.Enumerable.Select(x.ManuallyMappedList, x1 => x1.Value)),
IntegerValues = global::System.Linq.Enumerable.ToList(global::System.Linq.Enumerable.OrderBy(x.IntegerValues, x => x.Value)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,48 @@ public Task ClassToClassUserImplementedWithUsingMappings()
"""
private partial System.Linq.IQueryable<B> Map(System.Linq.IQueryable<A> source);

private static DateTimeOffset MapToDateTimeOffset(
DateTime dateTime
) => new(dateTime, TimeSpan.Zero);
private static DateTimeOffset MapToDateTimeOffset(DateTime dateTime)
=> new DateTimeOffset(dateTime, TimeSpan.Zero);
""",
"class A { public DateTime Value { get; set; } }",
"class B { public DateTimeOffset Value { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ClassToClassUserImplementedWithTargetTypeNew()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
private partial System.Linq.IQueryable<B> Map(System.Linq.IQueryable<A> source);

private static DateTimeOffset MapToDateTimeOffset(DateTime dateTime)
=> new(dateTime, TimeSpan.Zero);
""",
"class A { public DateTime Value { get; set; } }",
"class B { public DateTimeOffset Value { get; set; } }"
);

return TestHelper.VerifyGenerator(source);
}

[Fact]
public Task ClassToClassUserImplementedWithTargetTypeNewInitializer()
{
var source = TestSourceBuilder.MapperWithBodyAndTypes(
"""
private partial System.Linq.IQueryable<B> Map(System.Linq.IQueryable<A> source);

private static C MapIt(DateTime dateTime)
=> new(1) { Value2 = 10 };
""",
"class A { public DateTime Value { get; set; } }",
"class B { public C Value { get; set; } }",
"class C(int value1) { public int Value2 { set; } }"
);

return TestHelper.VerifyGenerator(source);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//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::System.Linq.IQueryable<global::B> Map(global::System.Linq.IQueryable<global::A> source)
{
#nullable disable
return System.Linq.Queryable.Select(source, x => new global::B()
{
Value = new global::System.DateTimeOffset(x.Value, global::System.TimeSpan.Zero),
});
#nullable enable
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//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::System.Linq.IQueryable<global::B> Map(global::System.Linq.IQueryable<global::A> source)
{
#nullable disable
return System.Linq.Queryable.Select(source, x => new global::B()
{
Value = new global::C(1) { Value2 = 10 },
});
#nullable enable
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class Mapper
#nullable disable
return System.Linq.Queryable.Select(source, x => new global::B()
{
Value = new(x.Value, global::System.TimeSpan.Zero),
Value = new global::System.DateTimeOffset(x.Value, global::System.TimeSpan.Zero),
});
#nullable enable
}
Expand Down

0 comments on commit 840b6e1

Please sign in to comment.