-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
678 additions
and
347 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
54 changes: 54 additions & 0 deletions
54
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,54 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Riok.Mapperly.Descriptors.TypeMappings; | ||
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, ctx.MapperConfiguration.ThrowOnNull); | ||
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, bool throwOnNull) | ||
{ | ||
if (ctx.Target.IsNullable()) | ||
return NullFallbackValue.Default; | ||
|
||
if (throwOnNull) | ||
return NullFallbackValue.ThrowArgumentNullException; | ||
|
||
if (!ctx.Target.IsReferenceType || ctx.Target.IsNullable()) | ||
return NullFallbackValue.Default; | ||
|
||
if (ctx.Target.SpecialType == SpecialType.System_String) | ||
return NullFallbackValue.EmptyString; | ||
|
||
return ctx.Target.HasAccessibleParameterlessConstructor() | ||
? NullFallbackValue.CreateInstance | ||
: NullFallbackValue.ThrowArgumentNullException; // TODO diagnostic | ||
} | ||
} |
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.