Skip to content

Commit

Permalink
Merge pull request MapsterMapper#218 from SergerGood/use-readonly-struct
Browse files Browse the repository at this point in the history
use readonly struct and in modifier
  • Loading branch information
chaowlert committed Jan 11, 2020
2 parents 6456bd5 + 48c577f commit b8075fe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
35 changes: 19 additions & 16 deletions src/Mapster/Settings/IgnoreDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ namespace Mapster
{
public class IgnoreDictionary : Dictionary<string, IgnoreDictionary.IgnoreItem>, IApplyable<IgnoreDictionary>
{
public struct IgnoreItem
public readonly struct IgnoreItem
{
public LambdaExpression Condition { get; set; }
public bool IsChildPath { get; set; }
public IgnoreItem(LambdaExpression condition, bool isChildPath)
{
Condition = condition;
IsChildPath = isChildPath;
}

public LambdaExpression Condition { get; }
public bool IsChildPath { get; }
}

public void Apply(object other)
Expand All @@ -26,7 +32,7 @@ public void Apply(IgnoreDictionary other)
}
}

internal void Merge(string name, IgnoreDictionary.IgnoreItem src)
internal void Merge(string name, in IgnoreItem src)
{
if (src.Condition != null && this.TryGetValue(name, out var item))
{
Expand All @@ -35,11 +41,9 @@ internal void Merge(string name, IgnoreDictionary.IgnoreItem src)

var param = src.Condition.Parameters.ToArray();
var body = item.IsChildPath ? item.Condition.Body : item.Condition.Apply(param[0], param[1]);
this[name] = new IgnoreItem
{
Condition = Expression.Lambda(Expression.OrElse(src.Condition.Body, body), param),
IsChildPath = src.IsChildPath
};
var condition = Expression.Lambda(Expression.OrElse(src.Condition.Body, body), param);

this[name] = new IgnoreItem(condition, src.IsChildPath);
}
else
this[name] = src;
Expand All @@ -53,13 +57,12 @@ internal IgnoreDictionary Next(ParameterExpression source, ParameterExpression?
{
if (!member.Key.StartsWith(destMemberName + "."))
continue;
var next = new IgnoreItem
{
Condition = member.Value.IsChildPath || member.Value.Condition == null
? member.Value.Condition
: Expression.Lambda(member.Value.Condition.Apply(source, destination), source, destination),
IsChildPath = true
};

var condition = member.Value.IsChildPath || member.Value.Condition == null
? member.Value.Condition
: Expression.Lambda(member.Value.Condition.Apply(source, destination), source, destination);

var next = new IgnoreItem(condition, true);
result.Merge(member.Key.Substring(destMemberName.Length + 1), next);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Mapster/TypeAdapterSetter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ internal TypeAdapterSetter(TypeAdapterSettings settings, TypeAdapterConfig paren
foreach (var member in members)
{
var name = ReflectionUtils.GetMemberPath(member);
Settings.Ignore.Merge(name, new IgnoreDictionary.IgnoreItem {Condition = condition});
Settings.Ignore.Merge(name, new IgnoreDictionary.IgnoreItem(condition, false));
}
return this;
}
Expand All @@ -418,7 +418,7 @@ internal TypeAdapterSetter(TypeAdapterSettings settings, TypeAdapterConfig paren

foreach (var member in members)
{
Settings.Ignore.Merge(member, new IgnoreDictionary.IgnoreItem {Condition = condition});
Settings.Ignore.Merge(member, new IgnoreDictionary.IgnoreItem(condition, false));
}
return this;
}
Expand Down

0 comments on commit b8075fe

Please sign in to comment.