-
-
Notifications
You must be signed in to change notification settings - Fork 201
Add support for From and To method conversions (#1117)
#1616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
latonz
merged 12 commits into
riok:main
from
TonEnfer:#1117_Support_From_and_To_static_factory_methods
Dec 9, 2024
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2d82323
Add support for `From` and `To` method conversions (#1117)
TonEnfer 426df91
Merge branch 'main' into #1117_Support_From_and_To_static_factory_met…
TonEnfer b4193af
Merge branch 'main' into #1117_Support_From_and_To_static_factory_met…
TonEnfer c7acd2d
Add support for `From` and `To` method conversions (#1117)
TonEnfer 8858811
Merge remote-tracking branch 'origin/#1117_Support_From_and_To_static…
TonEnfer 987f4f3
Merge remote-tracking branch 'origin/main' into #1117_Support_From_an…
TonEnfer 2b51b9d
Review fixes
TonEnfer 79b4f3e
Fix build and docs lint
TonEnfer 5937091
Fix build for Roslyn 4.0
TonEnfer c9c8845
Fix build for net6.0 and net 7.0
TonEnfer 2016b07
Fix tests for net48, net6.0, net 7.0
TonEnfer 4610d61
Code style fix
TonEnfer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.