forked from MapsterMapper/Mapster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix MapsterMapper#227: EmptyCollectionIfNull, CreateNewIfNull Destina…
…tionTransform
- Loading branch information
Showing
7 changed files
with
144 additions
and
85 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace Mapster | ||
{ | ||
public class DestinationTransform | ||
{ | ||
public static readonly DestinationTransform EmptyCollectionIfNull = new DestinationTransform | ||
{ | ||
Condition = type => type.IsArray | ||
|| type.IsListCompatible() | ||
|| type.GetDictionaryType() != null, | ||
TransformFunc = type => | ||
{ | ||
Expression newExp; | ||
if (type.IsArray) | ||
{ | ||
var rank = type.GetArrayRank(); | ||
var elemType = type.GetElementType()!; | ||
newExp = Expression.NewArrayBounds(elemType, Enumerable.Repeat(Expression.Constant(0), rank)); | ||
} | ||
else if (type.GetTypeInfo().IsInterface) | ||
{ | ||
if (type.GetDictionaryType() != null) | ||
{ | ||
var dict = type.GetDictionaryType()!; | ||
var dictArgs = dict.GetGenericArguments(); | ||
var dictType = typeof(Dictionary<,>).MakeGenericType(dictArgs); | ||
newExp = Expression.New(dictType); | ||
} | ||
else | ||
{ | ||
var elemType = type.ExtractCollectionType(); | ||
var listType = typeof(List<>).MakeGenericType(elemType); | ||
newExp = Expression.New(listType); | ||
} | ||
} | ||
else | ||
newExp = Expression.New(type); | ||
var p = Expression.Parameter(type); | ||
return Expression.Lambda(Expression.Coalesce(p, newExp), p); | ||
} | ||
}; | ||
|
||
public static readonly DestinationTransform CreateNewIfNull = new DestinationTransform | ||
{ | ||
Condition = type => | ||
{ | ||
if (!type.CanBeNull()) | ||
return false; | ||
if (type.GetTypeInfo().IsInterface || type.GetTypeInfo().IsAbstract) | ||
return false; | ||
var ci = type.GetConstructors(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).SingleOrDefault(c => c.GetParameters().Length == 0); | ||
return ci != null; | ||
}, | ||
TransformFunc = type => | ||
{ | ||
var p = Expression.Parameter(type); | ||
return Expression.Lambda( | ||
Expression.Coalesce(p, Expression.New(type))); | ||
} | ||
}; | ||
|
||
public Func<Type, bool> Condition { get; set; } | ||
public Func<Type, LambdaExpression> TransformFunc { get; set; } | ||
|
||
public DestinationTransform WithCondition(Func<Type, bool> condition) | ||
{ | ||
return new DestinationTransform | ||
{ | ||
Condition = condition, | ||
TransformFunc = this.TransformFunc | ||
}; | ||
} | ||
} | ||
} |
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
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