Skip to content

Commit

Permalink
Merge pull request MapsterMapper#555 from stormaref/master
Browse files Browse the repository at this point in the history
Applying clean code principles
  • Loading branch information
andrerav committed Mar 4, 2023
2 parents 1cf8d19 + a14eb62 commit 862bc8b
Show file tree
Hide file tree
Showing 17 changed files with 482 additions and 500 deletions.
784 changes: 384 additions & 400 deletions src/ExpressionTranslator/ExpressionTranslator.cs

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions src/Mapster/Adapters/BaseAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public abstract class BaseAdapter
{
protected virtual int Score => 0;
protected virtual ObjectType ObjectType => ObjectType.Primitive;
protected virtual bool CheckExplicitMapping => this.ObjectType == ObjectType.Class;
protected virtual bool CheckExplicitMapping => ObjectType == ObjectType.Class;
protected virtual bool UseTargetValue => false;

public virtual int? Priority(PreCompileArgument arg)
{
return CanMap(arg) ? this.Score : (int?)null;
return CanMap(arg) ? Score : (int?)null;
}

protected abstract bool CanMap(PreCompileArgument arg);
Expand Down Expand Up @@ -45,11 +45,11 @@ public TypeAdapterRule CreateRule()
{
var rule = new TypeAdapterRule
{
Priority = this.Priority,
Priority = Priority,
Settings = new TypeAdapterSettings
{
ConverterFactory = this.CreateAdaptFunc,
ConverterToTargetFactory = this.CreateAdaptToTargetFunc,
ConverterFactory = CreateAdaptFunc,
ConverterToTargetFactory = CreateAdaptToTargetFunc,
}
};
DecorateRule(rule);
Expand Down Expand Up @@ -82,7 +82,7 @@ protected virtual bool CanInline(Expression source, Expression? destination, Com

protected virtual Expression CreateExpressionBody(Expression source, Expression? destination, CompileArgument arg)
{
if (this.CheckExplicitMapping && arg.Context.Config.RequireExplicitMapping && !arg.ExplicitMapping)
if (CheckExplicitMapping && arg.Context.Config.RequireExplicitMapping && !arg.ExplicitMapping)
throw new InvalidOperationException("Implicit mapping is not allowed (check GlobalSettings.RequireExplicitMapping) and no configuration exists");

var oldMaxDepth = arg.Context.MaxDepth;
Expand All @@ -96,9 +96,9 @@ protected virtual Expression CreateExpressionBody(Expression source, Expression?
}
if (arg.Context.MaxDepth.HasValue)
{
if (this.ObjectType != ObjectType.Primitive && arg.Context.Depth >= arg.Context.MaxDepth.Value)
if (ObjectType != ObjectType.Primitive && arg.Context.Depth >= arg.Context.MaxDepth.Value)
return arg.DestinationType.CreateDefault();
if (this.ObjectType == ObjectType.Class)
if (ObjectType == ObjectType.Class)
arg.Context.Depth++;
}

Expand Down Expand Up @@ -196,7 +196,7 @@ protected Expression CreateBlockExpressionBody(Expression source, Expression? de
transformedSource = src;
}
var set = CreateInstantiationExpression(transformedSource, destination, arg);
if (destination != null && (this.UseTargetValue || arg.UseDestinationValue) && arg.GetConstructUsing()?.Parameters.Count != 2)
if (destination != null && (UseTargetValue || arg.UseDestinationValue) && arg.GetConstructUsing()?.Parameters.Count != 2)
{
if (destination.CanBeNull())
{
Expand Down
6 changes: 3 additions & 3 deletions src/Mapster/Adapters/BaseAfterMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class BaseAfterMapper

public virtual int? Priority(PreCompileArgument arg)
{
return CanMap(arg) ? this.Score : (int?)null;
return CanMap(arg) ? Score : (int?)null;
}

protected abstract bool CanMap(PreCompileArgument arg);
Expand All @@ -26,10 +26,10 @@ public LambdaExpression CreateAfterMapFunc(CompileArgument arg)
public TypeAdapterRule CreateRule()
{
var settings = new TypeAdapterSettings();
settings.AfterMappingFactories.Add(this.CreateAfterMapFunc);
settings.AfterMappingFactories.Add(CreateAfterMapFunc);
var rule = new TypeAdapterRule
{
Priority = this.Priority,
Priority = Priority,
Settings = settings,
};
DecorateRule(rule);
Expand Down
8 changes: 3 additions & 5 deletions src/Mapster/Compile/CompileArgument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@ internal HashSet<string> GetDestinationNames()
private LambdaExpression? _constructUsing;
internal LambdaExpression? GetConstructUsing()
{
if (!_fetchConstructUsing)
{
_constructUsing = Settings.ConstructUsingFactory?.Invoke(this);
_fetchConstructUsing = true;
}
if (_fetchConstructUsing) return _constructUsing;
_constructUsing = Settings.ConstructUsingFactory?.Invoke(this);
_fetchConstructUsing = true;
return _constructUsing;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/Mapster/Compile/CompileContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace Mapster
{
public class CompileContext
{
public HashSet<TypeTuple> Running { get; } = new HashSet<TypeTuple>();
public Stack<TypeAdapterConfig> Configs { get; } = new Stack<TypeAdapterConfig>();
public HashSet<TypeTuple> Running { get; } = new();
public Stack<TypeAdapterConfig> Configs { get; } = new();
public TypeAdapterConfig Config => Configs.Peek();
public int? MaxDepth { get; set; }
public int Depth { get; set; }
public HashSet<ParameterExpression> ExtraParameters { get; } = new HashSet<ParameterExpression>();
public HashSet<ParameterExpression> ExtraParameters { get; } = new();

internal bool IsSubFunction()
{
return this.MaxDepth.HasValue || this.ExtraParameters.Count > 0;
return MaxDepth.HasValue || ExtraParameters.Count > 0;
}

public CompileContext(TypeAdapterConfig config)
Expand Down
8 changes: 4 additions & 4 deletions src/Mapster/Compile/CompileException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ public class CompileException : InvalidOperationException
{
public CompileException(CompileArgument argument, Exception innerException) : base(null, innerException)
{
this.Argument = argument;
Argument = argument;
}

public override string Message =>
"Error while compiling\n" +
$"source={this.Argument.SourceType}\n" +
$"destination={this.Argument.DestinationType}\n" +
$"type={this.Argument.MapType}";
$"source={Argument.SourceType}\n" +
$"destination={Argument.DestinationType}\n" +
$"type={Argument.MapType}";

public CompileArgument Argument { get; }
public LambdaExpression? Expression { get; internal set; }
Expand Down
34 changes: 17 additions & 17 deletions src/Mapster/Models/InvokerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ public class InvokerModel

public InvokerModel? Next(ParameterExpression source, string destMemberName)
{
if (!this.DestinationMemberName.StartsWith(destMemberName + "."))
if (!DestinationMemberName.StartsWith(destMemberName + "."))
return null;

return new InvokerModel
{
DestinationMemberName = this.DestinationMemberName.Substring(destMemberName.Length + 1),
Condition = this.IsChildPath || this.Condition == null
? this.Condition
: Expression.Lambda(this.Condition.Apply(source), source),
Invoker = this.IsChildPath
? this.Invoker
: Expression.Lambda(this.GetInvokingExpression(source), source),
SourceMemberName = this.SourceMemberName,
DestinationMemberName = DestinationMemberName.Substring(destMemberName.Length + 1),
Condition = IsChildPath || Condition == null
? Condition
: Expression.Lambda(Condition.Apply(source), source),
Invoker = IsChildPath
? Invoker
: Expression.Lambda(GetInvokingExpression(source), source),
SourceMemberName = SourceMemberName,
IsChildPath = true,
};
}

public Expression GetInvokingExpression(Expression exp, MapType mapType = MapType.Map)
{
if (this.IsChildPath)
return this.Invoker!.Body;
return this.SourceMemberName != null
? ExpressionEx.PropertyOrFieldPath(exp, this.SourceMemberName)
: this.Invoker!.Apply(mapType, exp);
if (IsChildPath)
return Invoker!.Body;
return SourceMemberName != null
? ExpressionEx.PropertyOrFieldPath(exp, SourceMemberName)
: Invoker!.Apply(mapType, exp);
}

public Expression? GetConditionExpression(Expression exp, MapType mapType = MapType.Map)
{
return this.IsChildPath
? this.Condition?.Body
: this.Condition?.Apply(mapType, exp);
return IsChildPath
? Condition?.Body
: Condition?.Apply(mapType, exp);
}
}
}
4 changes: 2 additions & 2 deletions src/Mapster/Models/KeyValuePairModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ public IEnumerable<CustomAttributeData> GetCustomAttributesData() =>

public Expression GetExpression(Expression source)
{
return _getFn(source, Expression.Constant(this.Name));
return _getFn(source, Expression.Constant(Name));
}
public Expression SetExpression(Expression source, Expression value)
{
return _setFn(source, Expression.Constant(this.Name), value);
return _setFn(source, Expression.Constant(Name), value);
}
}
}
2 changes: 1 addition & 1 deletion src/Mapster/Models/ParameterModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public ParameterModel(ParameterInfo parameterInfo)

public Expression GetExpression(Expression source)
{
return Expression.Variable(this.Type, _parameterInfo.Name);
return Expression.Variable(Type, _parameterInfo.Name);
}
public Expression SetExpression(Expression source, Expression value)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Mapster/Models/TypeTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public override int GetHashCode()

public TypeTuple(Type source, Type destination)
{
this.Source = source;
this.Destination = destination;
Source = source;
Destination = destination;
}
}
}
2 changes: 1 addition & 1 deletion src/Mapster/Settings/DestinationTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public DestinationTransform WithCondition(Func<Type, bool> condition)
return new DestinationTransform
{
Condition = condition,
TransformFunc = this.TransformFunc
TransformFunc = TransformFunc
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Mapster/Settings/IgnoreDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public void Apply(IgnoreDictionary other)
{
foreach (var member in other)
{
this.Merge(member.Key, member.Value);
Merge(member.Key, member.Value);
}
}

internal void Merge(string name, in IgnoreItem src)
{
if (src.Condition != null && this.TryGetValue(name, out var item))
if (src.Condition != null && TryGetValue(name, out var item))
{
if (item.Condition == null)
return;
Expand Down
4 changes: 2 additions & 2 deletions src/Mapster/Settings/NameMatchingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public void Apply(object other)
[SuppressMessage("ReSharper", "ConstantNullCoalescingCondition")]
public void Apply(NameMatchingStrategy other)
{
this.SourceMemberNameConverter ??= other.SourceMemberNameConverter;
this.DestinationMemberNameConverter ??= other.DestinationMemberNameConverter;
SourceMemberNameConverter ??= other.SourceMemberNameConverter;
DestinationMemberNameConverter ??= other.DestinationMemberNameConverter;
}

public static readonly NameMatchingStrategy Exact = new NameMatchingStrategy
Expand Down
34 changes: 17 additions & 17 deletions src/Mapster/TypeAdapterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ namespace Mapster
public class TypeAdapterBuilder<TSource> : IAdapterBuilder<TSource>
{
TSource Source { get; }
TSource IAdapterBuilder<TSource>.Source => this.Source;
TSource IAdapterBuilder<TSource>.Source => Source;
TypeAdapterConfig Config { get; set; }
TypeAdapterConfig IAdapterBuilder.Config => this.Config;
TypeAdapterConfig IAdapterBuilder.Config => Config;

private Dictionary<string, object>? _parameters;
Dictionary<string, object> Parameters => _parameters ??= new Dictionary<string, object>();
Dictionary<string, object> IAdapterBuilder.Parameters => this.Parameters;
Dictionary<string, object> IAdapterBuilder.Parameters => Parameters;
bool IAdapterBuilder.HasParameter => _parameters != null && _parameters.Count > 0;

internal TypeAdapterBuilder(TSource source, TypeAdapterConfig config)
{
this.Source = source;
this.Config = config;
Source = source;
Config = config;
}

[SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")]
Expand All @@ -36,21 +36,21 @@ public TypeAdapterBuilder<TSource> ForkConfig(Action<TypeAdapterConfig> action,
#endif
int key2 = 0)
{
this.Config = this.Config.Fork(action, key1, key2);
Config = Config.Fork(action, key1, key2);
return this;
}

public TypeAdapterBuilder<TSource> AddParameters(string name, object value)
{
this.Parameters.Add(name, value);
Parameters.Add(name, value);
return this;
}

private MapContextScope CreateMapContextScope()
{
var scope = new MapContextScope();
var parameters = scope.Context.Parameters;
foreach (var kvp in this.Parameters)
foreach (var kvp in Parameters)
{
parameters[kvp.Key] = kvp.Value;
}
Expand All @@ -65,51 +65,51 @@ public TDestination AdaptToType<TDestination>()
if (_parameters == null)
return Map<TDestination>();

using (this.CreateMapContextScope())
using (CreateMapContextScope())
{
return Map<TDestination>();
}
}

private TDestination Map<TDestination>()
{
var fn = this.Config.GetMapFunction<TSource, TDestination>();
return fn(this.Source);
var fn = Config.GetMapFunction<TSource, TDestination>();
return fn(Source);
}

public TDestination AdaptTo<TDestination>(TDestination destination)
{
if (_parameters == null)
return MapToTarget(destination);

using (this.CreateMapContextScope())
using (CreateMapContextScope())
{
return MapToTarget(destination);
}
}

private TDestination MapToTarget<TDestination>(TDestination destination)
{
var fn = this.Config.GetMapToTargetFunction<TSource, TDestination>();
return fn(this.Source, destination);
var fn = Config.GetMapToTargetFunction<TSource, TDestination>();
return fn(Source, destination);
}

public Expression<Func<TSource, TDestination>> CreateMapExpression<TDestination>()
{
var tuple = new TypeTuple(typeof(TSource), typeof(TDestination));
return (Expression<Func<TSource, TDestination>>) this.Config.CreateMapExpression(tuple, MapType.Map);
return (Expression<Func<TSource, TDestination>>) Config.CreateMapExpression(tuple, MapType.Map);
}

public Expression<Func<TSource, TDestination, TDestination>> CreateMapToTargetExpression<TDestination>()
{
var tuple = new TypeTuple(typeof(TSource), typeof(TDestination));
return (Expression<Func<TSource, TDestination, TDestination>>) this.Config.CreateMapExpression(tuple, MapType.MapToTarget);
return (Expression<Func<TSource, TDestination, TDestination>>) Config.CreateMapExpression(tuple, MapType.MapToTarget);
}

public Expression<Func<TSource, TDestination>> CreateProjectionExpression<TDestination>()
{
var tuple = new TypeTuple(typeof(TSource), typeof(TDestination));
return (Expression<Func<TSource, TDestination>>) this.Config.CreateMapExpression(tuple, MapType.Projection);
return (Expression<Func<TSource, TDestination>>) Config.CreateMapExpression(tuple, MapType.Projection);
}
}
}
Loading

0 comments on commit 862bc8b

Please sign in to comment.