-
-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve nullable handling (#15)
- Loading branch information
Showing
38 changed files
with
728 additions
and
354 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
57 changes: 57 additions & 0 deletions
57
src/Riok.Mapperly/Descriptors/MappingBuilder/NullableMappingBuilder.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,57 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Descriptors.TypeMappings; | ||
using Riok.Mapperly.Diagnostics; | ||
using Riok.Mapperly.Helpers; | ||
|
||
namespace Riok.Mapperly.Descriptors.MappingBuilder; | ||
|
||
public static class NullableMappingBuilder | ||
{ | ||
public static TypeMapping? TryBuildMapping(MappingBuilderContext ctx) | ||
{ | ||
var sourceIsNullable = ctx.Source.TryGetNonNullable(out var sourceNonNullable); | ||
var targetIsNullable = ctx.Target.TryGetNonNullable(out var targetNonNullable); | ||
if (!sourceIsNullable && !targetIsNullable) | ||
return null; | ||
|
||
var delegateMapping = ctx.BuildDelegateMapping(sourceNonNullable ?? ctx.Source, targetNonNullable ?? ctx.Target); | ||
return delegateMapping == null | ||
? null | ||
: BuildNullDelegateMapping(ctx, delegateMapping); | ||
} | ||
|
||
private static TypeMapping BuildNullDelegateMapping(MappingBuilderContext ctx, TypeMapping mapping) | ||
{ | ||
var nullFallback = GetNullFallbackValue(ctx); | ||
return mapping switch | ||
{ | ||
MethodMapping methodMapping => new NullDelegateMethodMapping( | ||
ctx.Source, | ||
ctx.Target, | ||
methodMapping, | ||
nullFallback), | ||
_ => new NullDelegateMapping(ctx.Source, ctx.Target, mapping, nullFallback), | ||
}; | ||
} | ||
|
||
private static NullFallbackValue GetNullFallbackValue(MappingBuilderContext ctx) | ||
{ | ||
if (ctx.Target.IsNullable()) | ||
return NullFallbackValue.Default; | ||
|
||
if (ctx.MapperConfiguration.ThrowOnMappingNullMismatch) | ||
return NullFallbackValue.ThrowArgumentNullException; | ||
|
||
if (!ctx.Target.IsReferenceType || ctx.Target.IsNullable()) | ||
return NullFallbackValue.Default; | ||
|
||
if (ctx.Target.SpecialType == SpecialType.System_String) | ||
return NullFallbackValue.EmptyString; | ||
|
||
if (ctx.Target.HasAccessibleParameterlessConstructor()) | ||
return NullFallbackValue.CreateInstance; | ||
|
||
ctx.ReportDiagnostic(DiagnosticDescriptors.NoParameterlessConstructorFound, ctx.Target); | ||
return NullFallbackValue.ThrowArgumentNullException; | ||
} | ||
} |
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
25 changes: 0 additions & 25 deletions
25
src/Riok.Mapperly/Descriptors/PropertyMappingDescriptor.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.