-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
37 changed files
with
1,387 additions
and
129 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
66 changes: 66 additions & 0 deletions
66
src/Riok.Mapperly/Descriptors/MappingBuilders/ConvertInstanceMethodMappingBuilder.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,66 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Descriptors.Mappings; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBuilders; | ||
|
||
public static class ConvertInstanceMethodMappingBuilder | ||
{ | ||
public static SourceObjectMethodMapping? TryBuildMapping(MappingBuilderContext ctx) | ||
{ | ||
if (!ctx.IsConversionEnabled(MappingConversionType.ToTargetMethod)) | ||
return null; | ||
|
||
var targetIsNullable = ctx.Target.NonNullable(out var nonNullableTarget); | ||
|
||
// ignore `ToString` mapping for backward compatibility | ||
if (nonNullableTarget.SpecialType == SpecialType.System_String) | ||
return null; | ||
|
||
foreach (var methodName in GetMappingMethodNames(ctx)) | ||
{ | ||
var methodCandidates = ctx | ||
.SymbolAccessor.GetAllMethods(ctx.Source) | ||
.Where(m => | ||
string.Equals(m.Name, methodName, StringComparison.OrdinalIgnoreCase) | ||
&& m is { IsStatic: false, ReturnsVoid: false, IsAsync: false, Parameters.Length: 0 } | ||
) | ||
.ToList(); | ||
|
||
// try to find method with equal nullability return type | ||
var method = methodCandidates.Find(x => SymbolEqualityComparer.IncludeNullability.Equals(x.ReturnType, ctx.Target)); | ||
if (method is not null) | ||
return new SourceObjectMethodMapping(ctx.Source, ctx.Target, method.Name); | ||
|
||
if (!targetIsNullable) | ||
continue; | ||
|
||
// otherwise try to find method ignoring the nullability | ||
method = methodCandidates.Find(x => SymbolEqualityComparer.Default.Equals(x.ReturnType, nonNullableTarget)); | ||
|
||
if (method is null) | ||
continue; | ||
|
||
return new SourceObjectMethodMapping(ctx.Source, ctx.Target, method.Name); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static IEnumerable<string> GetMappingMethodNames(MappingBuilderContext ctx) | ||
{ | ||
var nonNullableTarget = ctx.Target.NonNullable(); | ||
var hasKeyword = nonNullableTarget.HasKeyword(out var keywordName); | ||
if (!nonNullableTarget.IsArrayType(out var arrayType)) | ||
{ | ||
var methodName = $"To{nonNullableTarget.Name}"; | ||
return hasKeyword ? [methodName, $"To{keywordName}"] : [methodName]; | ||
} | ||
|
||
var nonNullableElementType = arrayType.ElementType.NonNullable(); | ||
|
||
var arrayMethodName = $"To{nonNullableElementType.Name}Array"; | ||
return hasKeyword ? [arrayMethodName, $"To{keywordName}Array"] : [arrayMethodName]; | ||
} | ||
} |
169 changes: 169 additions & 0 deletions
169
src/Riok.Mapperly/Descriptors/MappingBuilders/ConvertStaticMethodMappingBuilder.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,169 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Abstractions; | ||
using Riok.Mapperly.Descriptors.Mappings; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBuilders; | ||
|
||
public static class ConvertStaticMethodMappingBuilder | ||
{ | ||
public static StaticMethodMapping? TryBuildMapping(MappingBuilderContext ctx) | ||
{ | ||
if (!IsConversionEnabled(ctx)) | ||
return null; | ||
|
||
var targetIsNullable = ctx.Target.NonNullable(out var nonNullableTarget); | ||
|
||
var allTargetMethods = ctx.SymbolAccessor.GetAllMethods(nonNullableTarget).ToList(); | ||
|
||
var mapping = TryGetStaticMethodMapping( | ||
ctx.SymbolAccessor, | ||
allTargetMethods, | ||
GetTargetStaticMethodNames(ctx), | ||
ctx.Source, | ||
ctx.Target, | ||
nonNullableTarget, | ||
targetIsNullable | ||
); | ||
|
||
if (mapping is not null) | ||
{ | ||
return mapping; | ||
} | ||
|
||
var allSourceMethods = ctx.SymbolAccessor.GetAllMethods(ctx.Source); | ||
|
||
// collect also methods from source type generic argument, for example `TTarget ToTarget(List<A> source)` | ||
if (ctx.Source is INamedTypeSymbol { TypeArguments.Length: 1 } namedTypeSymbol) | ||
{ | ||
allSourceMethods = allSourceMethods.Concat(ctx.SymbolAccessor.GetAllMethods(namedTypeSymbol.TypeArguments[0])); | ||
} | ||
|
||
return TryGetStaticMethodMapping( | ||
ctx.SymbolAccessor, | ||
allSourceMethods.ToList(), | ||
GetSourceStaticMethodNames(ctx), | ||
ctx.Source, | ||
ctx.Target, | ||
nonNullableTarget, | ||
targetIsNullable | ||
); | ||
} | ||
|
||
private static bool IsConversionEnabled(MappingBuilderContext ctx) | ||
{ | ||
//checks DateTime type for backward compatibility | ||
return ctx.IsConversionEnabled( | ||
IsDateTimeToDateOnlyConversion(ctx) ? MappingConversionType.DateTimeToDateOnly | ||
: IsDateTimeToTimeOnlyConversion(ctx) ? MappingConversionType.DateTimeToTimeOnly | ||
: MappingConversionType.StaticConvertMethods | ||
); | ||
} | ||
|
||
private static bool IsDateTimeToDateOnlyConversion(MappingBuilderContext ctx) | ||
{ | ||
return ctx.Source.SpecialType == SpecialType.System_DateTime | ||
&& ctx.Types.DateOnly != null | ||
&& ctx.Target is INamedTypeSymbol namedSymbol | ||
&& SymbolEqualityComparer.Default.Equals(namedSymbol, ctx.Types.DateOnly); | ||
} | ||
|
||
private static bool IsDateTimeToTimeOnlyConversion(MappingBuilderContext ctx) | ||
{ | ||
return ctx.Source.SpecialType == SpecialType.System_DateTime | ||
&& ctx.Types.TimeOnly != null | ||
&& ctx.Target is INamedTypeSymbol namedSymbol | ||
&& SymbolEqualityComparer.Default.Equals(namedSymbol, ctx.Types.TimeOnly); | ||
} | ||
|
||
private static StaticMethodMapping? TryGetStaticMethodMapping( | ||
SymbolAccessor symbolAccessor, | ||
List<IMethodSymbol> allMethods, | ||
IEnumerable<string> methodNames, | ||
ITypeSymbol sourceType, | ||
ITypeSymbol targetType, | ||
ITypeSymbol nonNullableTargetType, | ||
bool targetIsNullable | ||
) | ||
{ | ||
// Get all methods with a single parameter whose type is suitable for assignment to the source type, group them by name, | ||
// and convert them to a dictionary whose key is the method name. | ||
// The keys in the dictionary are compared case-insensitively to handle possible `Uint` vs `UInt` cases, etc. | ||
var allMethodCandidates = allMethods | ||
.Where(m => m is { IsStatic: true, ReturnsVoid: false, IsAsync: false, Parameters.Length: 1 }) | ||
.GroupBy(x => x.Name, x => x, StringComparer.OrdinalIgnoreCase) | ||
.ToDictionary(x => x.Key, x => x.ToList(), StringComparer.OrdinalIgnoreCase); | ||
|
||
foreach (var methodName in methodNames) | ||
{ | ||
if (!allMethodCandidates.TryGetValue(methodName, out var candidates)) | ||
continue; | ||
|
||
if (targetIsNullable) | ||
{ | ||
continue; | ||
} | ||
|
||
var method = candidates.Find(x => symbolAccessor.ValidateSignature(x, nonNullableTargetType, sourceType)); | ||
|
||
if (method != null) | ||
return new StaticMethodMapping(method); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static IEnumerable<string> GetTargetStaticMethodNames(MappingBuilderContext ctx) | ||
{ | ||
if (ctx.Source.IsArrayType(out var arrayType)) | ||
{ | ||
yield return $"CreateFrom{arrayType.ElementType.Name}Array"; | ||
yield return $"From{arrayType.ElementType.Name}Array"; | ||
|
||
if (!arrayType.ElementType.HasKeyword(out var keywordName)) | ||
yield break; | ||
|
||
yield return $"CreateFrom{keywordName}Array"; | ||
|
||
yield return $"From{keywordName}Array"; | ||
|
||
yield return "FromArray"; | ||
yield return "CreateFromArray"; | ||
yield return "CreateFrom"; | ||
yield return "Create"; | ||
yield break; | ||
} | ||
|
||
yield return $"CreateFrom{ctx.Source.Name}"; | ||
yield return $"From{ctx.Source.Name}"; | ||
|
||
if (ctx.Source.HasKeyword(out var sourceKeyword)) | ||
{ | ||
yield return $"CreateFrom{sourceKeyword}"; | ||
yield return $"From{sourceKeyword}"; | ||
} | ||
|
||
yield return "CreateFrom"; | ||
yield return "Create"; | ||
} | ||
|
||
private static IEnumerable<string> GetSourceStaticMethodNames(MappingBuilderContext ctx) | ||
{ | ||
var nonNullableTarget = ctx.Target.NonNullable(); | ||
|
||
yield return $"To{nonNullableTarget.Name}"; | ||
|
||
if (nonNullableTarget.HasKeyword(out var keywordName)) | ||
yield return $"To{keywordName}"; | ||
|
||
if (!nonNullableTarget.IsArrayType(out var arrayTypeSymbol)) | ||
yield break; | ||
|
||
var nonNullableElementType = arrayTypeSymbol.ElementType.NonNullable(); | ||
|
||
yield return $"To{nonNullableElementType.Name}Array"; | ||
|
||
if (nonNullableElementType.HasKeyword(out var elementTypeName)) | ||
yield return $"To{elementTypeName}Array"; | ||
} | ||
} |
33 changes: 0 additions & 33 deletions
33
src/Riok.Mapperly/Descriptors/MappingBuilders/DateTimeToDateOnlyMappingBuilder.cs
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
src/Riok.Mapperly/Descriptors/MappingBuilders/DateTimeToTimeOnlyMappingBuilder.cs
This file was deleted.
Oops, something went wrong.
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.