Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support queryable projection mappings for derived type mappings (#…
…412)
- Loading branch information
Showing
19 changed files
with
344 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Mapperly PR | ||
# <Title> | ||
|
||
## Description | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/Riok.Mapperly/Descriptors/Mappings/DerivedTypeIfExpressionMapping.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
using static Riok.Mapperly.Emit.SyntaxFactoryHelper; | ||
|
||
namespace Riok.Mapperly.Descriptors.Mappings; | ||
|
||
/// <summary> | ||
/// A derived type mapping maps one base type or interface to another | ||
/// by implementing a if with instance checks over known types and performs the provided mapping for each type. | ||
/// </summary> | ||
public class DerivedTypeIfExpressionMapping : TypeMapping | ||
{ | ||
private readonly IReadOnlyCollection<ITypeMapping> _typeMappings; | ||
|
||
public DerivedTypeIfExpressionMapping(ITypeSymbol sourceType, ITypeSymbol targetType, IReadOnlyCollection<ITypeMapping> typeMappings) | ||
: base(sourceType, targetType) | ||
{ | ||
_typeMappings = typeMappings; | ||
} | ||
|
||
public override ExpressionSyntax Build(TypeMappingBuildContext ctx) | ||
{ | ||
// source is A x ? MapToA(x) : <other cases> | ||
var typeExpressions = _typeMappings | ||
.Reverse() | ||
.Aggregate<ITypeMapping, ExpressionSyntax>(DefaultLiteral(), (aggregate, current) => BuildConditional(ctx, aggregate, current)); | ||
|
||
// cast to target type, to ensure the compiler picks the correct type | ||
// (B)(<ifs...> | ||
return CastExpression(FullyQualifiedIdentifier(TargetType), ParenthesizedExpression(typeExpressions)); | ||
} | ||
|
||
private ConditionalExpressionSyntax BuildConditional(TypeMappingBuildContext ctx, ExpressionSyntax notMatched, ITypeMapping mapping) | ||
{ | ||
// cannot use is pattern matching is operator due to expression limitations | ||
// use is with a cast instead | ||
// source is A ? MapToB((A)x) : <other cases> | ||
var castedSourceContext = ctx.WithSource( | ||
ParenthesizedExpression(CastExpression(FullyQualifiedIdentifier(mapping.SourceType), ctx.Source)) | ||
); | ||
return ConditionalExpression( | ||
BinaryExpression(SyntaxKind.IsExpression, ctx.Source, FullyQualifiedIdentifier(mapping.SourceType)), | ||
mapping.Build(castedSourceContext), | ||
notMatched | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
test/Riok.Mapperly.IntegrationTests/Dto/TestObjectDtoProjectionBaseType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Riok.Mapperly.IntegrationTests.Dto | ||
{ | ||
public abstract class TestObjectDtoProjectionBaseType | ||
{ | ||
public int Id { get; set; } | ||
|
||
public int BaseValue { get; set; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/Riok.Mapperly.IntegrationTests/Dto/TestObjectDtoProjectionTypeA.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Riok.Mapperly.IntegrationTests.Dto | ||
{ | ||
public class TestObjectDtoProjectionTypeA : TestObjectDtoProjectionBaseType | ||
{ | ||
public int ValueA { get; set; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/Riok.Mapperly.IntegrationTests/Dto/TestObjectDtoProjectionTypeB.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Riok.Mapperly.IntegrationTests.Dto | ||
{ | ||
public class TestObjectDtoProjectionTypeB : TestObjectDtoProjectionBaseType | ||
{ | ||
public int ValueB { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
test/Riok.Mapperly.IntegrationTests/Models/TestObjectProjectionBaseType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Riok.Mapperly.IntegrationTests.Models | ||
{ | ||
public abstract class TestObjectProjectionBaseType | ||
{ | ||
public int Id { get; set; } | ||
|
||
public int BaseValue { get; set; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/Riok.Mapperly.IntegrationTests/Models/TestObjectProjectionTypeA.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Riok.Mapperly.IntegrationTests.Models | ||
{ | ||
public class TestObjectProjectionTypeA : TestObjectProjectionBaseType | ||
{ | ||
public int ValueA { get; set; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
test/Riok.Mapperly.IntegrationTests/Models/TestObjectProjectionTypeB.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Riok.Mapperly.IntegrationTests.Models | ||
{ | ||
public class TestObjectProjectionTypeB : TestObjectProjectionBaseType | ||
{ | ||
public int ValueB { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
..._4_5/ProjectionMapperTest.DerivedTypesProjectionShouldTranslateToQuery_query.verified.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
SELECT "b"."type" = 'A', "b"."ValueA", "b"."Id", "b"."BaseValue", "b"."type" = 'B', "b"."ValueB" | ||
FROM "BaseTypeObjects" AS "b" | ||
ORDER BY "b"."BaseValue" |
12 changes: 12 additions & 0 deletions
12
...4_5/ProjectionMapperTest.DerivedTypesProjectionShouldTranslateToQuery_result.verified.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
ValueA: 10, | ||
Id: 1, | ||
BaseValue: 10 | ||
}, | ||
{ | ||
ValueB: 20, | ||
Id: 2, | ||
BaseValue: 20 | ||
} | ||
] |
Oops, something went wrong.