Skip to content

Commit 9e1f410

Browse files
committed
feat: start map Override
1 parent 11853b3 commit 9e1f410

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/Mapster/TypeAdapterSetter.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public TypeAdapterSetter(TypeAdapterSettings settings, TypeAdapterConfig config)
2222
Settings = settings;
2323
Config = config;
2424
}
25+
26+
internal static TypeAdapterSetter CreateMapTypeOverride()
27+
{
28+
return new TypeAdapterSetter(new TypeAdapterSettings(), null);
29+
}
30+
31+
internal static TypeAdapterSetter<TSource, TDestination> CreateMapTypeOverride<TSource, TDestination>()
32+
{
33+
return new TypeAdapterSetter<TSource, TDestination>(new TypeAdapterSettings(), null);
34+
}
2535
}
2636
public static class TypeAdapterSetterExtensions
2737
{
@@ -629,6 +639,40 @@ public TypeAdapterSetter<TDestination> IgnoredRemove(params Expression<Func<TDes
629639

630640
#endregion
631641

642+
public TypeAdapterSetter<TSource, TDestination> MapOverride<TDestinationMember, TSourceMember>(
643+
Expression<Func<TDestination, TDestinationMember>> member,
644+
Expression<Func<TSource, TSourceMember>> source,
645+
Action<TypeAdapterSetter<TSourceMember, TDestinationMember>>? configAction = null)
646+
{
647+
this.CheckCompiled();
648+
649+
var invoker = Expression.Lambda(source.Body, Expression.Parameter(typeof(object)));
650+
if (member.IsIdentity())
651+
{
652+
Settings.ExtraSources.Add(invoker);
653+
return this;
654+
}
655+
656+
if (configAction != null)
657+
{
658+
var settings = TypeAdapterSetter.CreateMapTypeOverride<TSourceMember, TDestinationMember>();
659+
660+
configAction(settings);
661+
662+
}
663+
664+
665+
Settings.Resolvers.Add(new InvokerModel
666+
{
667+
DestinationMemberName = member.GetMemberPath()!,
668+
Invoker = invoker,
669+
Condition = null
670+
});
671+
return this;
672+
}
673+
674+
675+
632676
public TypeAdapterSetter<TSource, TDestination> IgnoreIf(
633677
Expression<Func<TSource, TDestination, bool>> condition,
634678
params Expression<Func<TDestination, object>>[] members)

0 commit comments

Comments
 (0)