-
-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: accept destination type as mapping method parameter (#398)
- Loading branch information
Showing
44 changed files
with
1,004 additions
and
158 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
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
39 changes: 39 additions & 0 deletions
39
docs/docs/02-configuration/11-runtime-target-type-mapping.md
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,39 @@ | ||
# Runtime target type mapping | ||
|
||
If the target type of a mapping is not known at compile time, | ||
a mapping method with a `Type` parameter can be used. | ||
Mapperly implements this mapping method | ||
using all mappings the user defined in the mapper. | ||
|
||
```csharp | ||
[Mapper] | ||
public static partial class ModelMapper | ||
{ | ||
// highlight-start | ||
public static partial object Map(object source, Type targetType); | ||
// highlight-end | ||
private static partial BananaDto MapBanana(Banana source); | ||
private static partial AppleDto MapApple(Apple source); | ||
} | ||
|
||
class Banana {} | ||
class Apple {} | ||
|
||
class BananaDto {} | ||
class AppleDto {} | ||
``` | ||
|
||
If the source or target type of a runtime target type mapping is not `object`, | ||
only user mappings of which the source/target type is assignable to the source/target type of the mapping method are considered. | ||
|
||
Runtime target type mappings support [derived type mappings](./10-derived-type-mapping.md). | ||
The `MapDerivedTypeAttribute` can be directly applied to a runtime target type mapping method. | ||
|
||
:::info | ||
Mapperly runtime target type mappings | ||
only support source/target type combinations which are defined | ||
as mappings in the same mapper. | ||
If an unknown source/target type combination is provided at runtime, | ||
an `ArgumentException` is thrown. | ||
::: |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
40 changes: 40 additions & 0 deletions
40
src/Riok.Mapperly/Descriptors/MappingBodyBuilders/RuntimeTargetTypeMappingBodyBuilder.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,40 @@ | ||
using Riok.Mapperly.Descriptors.MappingBuilders; | ||
using Riok.Mapperly.Descriptors.Mappings; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBodyBuilders; | ||
|
||
public static class RuntimeTargetTypeMappingBodyBuilder | ||
{ | ||
public static void BuildMappingBody(MappingBuilderContext ctx, UserDefinedNewInstanceRuntimeTargetTypeMapping mapping) | ||
{ | ||
// source nulls are filtered out by the type switch arms, | ||
// therefore set source type always to nun-nullable | ||
// as non-nullables are also assignable to nullables. | ||
IEnumerable<ITypeMapping> mappings = ctx.CallableUserMappings.Where( | ||
x => | ||
x.SourceType.NonNullable().IsAssignableTo(ctx.Compilation, mapping.SourceType) | ||
&& x.TargetType.IsAssignableTo(ctx.Compilation, mapping.TargetType) | ||
); | ||
|
||
// include derived type mappings declared on this user defined method | ||
var derivedTypeMappings = DerivedTypeMappingBuilder.TryBuildContainedMappings(ctx, true); | ||
if (derivedTypeMappings != null) | ||
{ | ||
mappings = derivedTypeMappings.Concat(mappings); | ||
} | ||
|
||
// prefer non-nullable return types | ||
// and prefer types with a higher inheritance level | ||
// over types with a lower inheritance level | ||
// in the type switch | ||
// to use the most specific mapping | ||
mappings = mappings | ||
.OrderByDescending(x => x.SourceType.GetInheritanceLevel()) | ||
.ThenByDescending(x => x.TargetType.GetInheritanceLevel()) | ||
.ThenBy(x => x.TargetType.IsNullable()) | ||
.GroupBy(x => new TypeMappingKey(x, false)) | ||
.Select(x => x.First()); | ||
mapping.AddMappings(mappings); | ||
} | ||
} |
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
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
Oops, something went wrong.