Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
667a3d5
use Enum.Parse<TEnum>
lbargaoanu Jul 30, 2022
77086e8
less allocations
lbargaoanu Jul 31, 2022
e3aecd3
reuse the execution plan between user Map calls and internal Map calls
lbargaoanu Aug 1, 2022
6581906
remove IObjectMapperInfo
lbargaoanu Aug 2, 2022
5f0cebd
use the more efficient Expression.Invoke instead of ConvertReplacePar…
lbargaoanu Aug 3, 2022
9fc7cdf
less allocations
lbargaoanu Aug 3, 2022
7fdf915
less null checks
lbargaoanu Aug 4, 2022
4b507ee
unreachable, As maps are sealed by default
lbargaoanu Aug 6, 2022
84e3fff
less allocations
lbargaoanu Aug 6, 2022
b5cc0a6
don't try to match ignored members
lbargaoanu Aug 7, 2022
df48e72
more efficient
lbargaoanu Aug 7, 2022
12eaef9
drop RegEx
lbargaoanu Aug 8, 2022
06b0713
docs
lbargaoanu Aug 8, 2022
dc7e58f
cosmetic
lbargaoanu Aug 8, 2022
9356388
less allocations
lbargaoanu Aug 8, 2022
39fccd9
cosmetic
lbargaoanu Aug 9, 2022
a3ae964
less allocations
lbargaoanu Aug 9, 2022
00d29ee
we don't need to create As type maps
lbargaoanu Aug 11, 2022
c9bceef
remove ITypeMapConfiguration
lbargaoanu Aug 11, 2022
a0988d7
cosmetic
lbargaoanu Aug 11, 2022
0b9cf8c
try to show a better error message when mapping records through const…
lbargaoanu Aug 11, 2022
b9699dd
less allocations
lbargaoanu Aug 13, 2022
ed9097f
special case the defaults for naming conventions
lbargaoanu Aug 13, 2022
93576ef
make TypeMapPlanBuilder a struct
lbargaoanu Aug 15, 2022
3adb02f
use readonly record struct
lbargaoanu Aug 15, 2022
7720ac8
assume that a protected constructor with CompilerGeneratedAttribute m…
lbargaoanu Aug 15, 2022
4e1117f
less allocations
lbargaoanu Aug 15, 2022
dac3062
rename configurationProvider to the shorter configuration in most places
lbargaoanu Aug 17, 2022
b24560f
less allocations
lbargaoanu Aug 17, 2022
953d66d
converters with no source should throw
lbargaoanu Aug 17, 2022
ebd2c43
less allocations
lbargaoanu Aug 18, 2022
c5d7c36
cosmetic
lbargaoanu Aug 18, 2022
12676dd
less allocations
lbargaoanu Aug 19, 2022
85ff705
make MapFrom name more like the built-in mapping
lbargaoanu Aug 19, 2022
c71f168
less allocations
lbargaoanu Aug 20, 2022
3de255f
cosmetic
lbargaoanu Aug 21, 2022
6925737
use profiles in the benchmark
lbargaoanu Aug 21, 2022
08ec72a
simplify conventions
lbargaoanu Aug 21, 2022
e9287a2
merge the profile member configuration with the global one
lbargaoanu Aug 21, 2022
0d2aa50
cosmetic
lbargaoanu Aug 22, 2022
ec0a832
test reusing the execution plan
lbargaoanu Aug 22, 2022
593ad53
refactor test base classes
lbargaoanu Aug 22, 2022
806e73f
cosmetic
lbargaoanu Aug 23, 2022
d0e0e24
avoid changing the resolver configuration objects because they're reu…
lbargaoanu Aug 31, 2022
77e0015
cosmetic
lbargaoanu Aug 31, 2022
20c283a
less allocations
lbargaoanu Sep 3, 2022
66f0020
cosmetic
lbargaoanu Sep 3, 2022
f1655e1
file scoped namespaces
lbargaoanu Sep 3, 2022
288c4ad
global usings
lbargaoanu Sep 3, 2022
e6f2ca3
cosmetic
lbargaoanu Sep 4, 2022
daacd80
less allocations
lbargaoanu Sep 10, 2022
c608787
NullMappingOrderComesFirst
lbargaoanu Sep 10, 2022
434703c
less allocations
lbargaoanu Sep 10, 2022
4ba0e5f
we could include a different constructor
lbargaoanu Sep 10, 2022
b98273e
less allocations
lbargaoanu Sep 10, 2022
7eeaf59
cosmetic
lbargaoanu Sep 11, 2022
efd1df9
allocate _runtimeTypeDetails lazily
lbargaoanu Sep 12, 2022
3a491a3
create the projection builder lazily
lbargaoanu Sep 12, 2022
7da36f6
cosmetic
lbargaoanu Sep 12, 2022
970ab2d
an adhoc fix for including open generic config
lbargaoanu Sep 14, 2022
e7ba023
cosmetic
lbargaoanu Sep 18, 2022
2b99e96
MapFrom should override ignore
lbargaoanu Sep 18, 2022
3f8448a
UseDestinationValueNullable
lbargaoanu Sep 19, 2022
8b83aa7
cosmetic
lbargaoanu Sep 21, 2022
5e0066e
avoid LINQ
lbargaoanu Sep 23, 2022
7d65b78
the destination type name is already in the error message
lbargaoanu Sep 24, 2022
3c2d8cd
cosmetic
lbargaoanu Sep 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
less allocations
  • Loading branch information
lbargaoanu committed Aug 13, 2022
commit b9699dd95fa453bf58d98bc84ab89d4033f7f9de
2 changes: 1 addition & 1 deletion src/AutoMapper/ApiCompatBaseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ MembersMustExist : Member 'public void AutoMapper.IMappingExpressionBase<TSource
InterfacesShouldHaveSameMembers : Interface member 'public System.Text.RegularExpressions.Regex AutoMapper.INamingConvention.SplittingExpression' is present in the contract but not in the implementation.
InterfacesShouldHaveSameMembers : Interface member 'public System.String AutoMapper.INamingConvention.ReplaceValue(System.Text.RegularExpressions.Match)' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.String AutoMapper.INamingConvention.ReplaceValue(System.Text.RegularExpressions.Match)' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.String[] AutoMapper.INamingConvention.Split(System.String)' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.List<System.ReadOnlyMemory<System.Char>> AutoMapper.INamingConvention.Split(System.String)' is present in the implementation but not in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Text.RegularExpressions.Regex AutoMapper.INamingConvention.SplittingExpression.get()' is present in the contract but not in the implementation.
MembersMustExist : Member 'public System.Text.RegularExpressions.Regex AutoMapper.INamingConvention.SplittingExpression.get()' does not exist in the implementation but it does exist in the contract.
InterfacesShouldHaveSameMembers : Interface member 'public System.Collections.Generic.IReadOnlyCollection<AutoMapper.Configuration.TypeMapConfiguration> AutoMapper.IProfileConfiguration.OpenTypeMapConfigs.get()' is present in the implementation but not in the contract.
Expand Down
58 changes: 45 additions & 13 deletions src/AutoMapper/Configuration/Conventions/MemberConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace AutoMapper.Configuration.Conventions
Expand Down Expand Up @@ -82,30 +83,61 @@ public bool MapDestinationPropertyToSource(ProfileMap options, TypeDetails sourc
{
var destinationMemberNamingConvention = isReverseMap ? SourceMemberNamingConvention : DestinationMemberNamingConvention;
var matches = destinationMemberNamingConvention.Split(nameToSearch);
var length = matches.Length;
if (length < 2)
if (matches == null)
{
return false;
}
var sourceMemberNamingConvention = isReverseMap ? DestinationMemberNamingConvention : SourceMemberNamingConvention;
var separator = sourceMemberNamingConvention.SeparatorCharacter;
for (var index = 1; index <= length; index++)
var length = matches.Count - 1;
for (var index = 0; index <= length; index++)
{
var first = string.Join(separator, matches, 0, index);
var first = Join(separator, matches, 0, index);
var matchingMemberInfo = parent.NameMapper.GetMatchingMemberInfo(sourceType, destType, destMemberType, first);
if (matchingMemberInfo != null)
if (matchingMemberInfo == null)
{
resolvers.Add(matchingMemberInfo);
var details = options.CreateTypeDetails(matchingMemberInfo.GetMemberType());
var second = string.Join(separator, matches, index, length - index);
if (parent.MapDestinationPropertyToSource(options, details, destType, destMemberType, second, resolvers, isReverseMap))
{
return true;
}
resolvers.RemoveAt(resolvers.Count - 1);
continue;
}
resolvers.Add(matchingMemberInfo);
if (index == length)
{
return true;
}
var details = options.CreateTypeDetails(matchingMemberInfo.GetMemberType());
var second = Join(separator, matches, index + 1, length);
if (parent.MapDestinationPropertyToSource(options, details, destType, destMemberType, second, resolvers, isReverseMap))
{
return true;
}
resolvers.RemoveAt(resolvers.Count - 1);
}
return false;
static string Join(string separator, List<ReadOnlyMemory<char>> matches, int startIndex, int endIndex)
{
Debug.Assert(startIndex <= endIndex);
int resultLength = 0;
var separatorLength = separator.Length;
for (int index = startIndex; index <= endIndex; index++)
{
resultLength += matches[index].Length + separatorLength;
}
resultLength -= separatorLength;
return string.Create(resultLength, (matches, startIndex, endIndex, separator), static (span, state) =>
{
var separatorSpan = state.separator.AsSpan();
for (int index = state.startIndex, destinationIndex = 0; index <= state.endIndex; index++)
{
var match = state.matches[index].Span;
match.CopyTo(span[destinationIndex..]);
destinationIndex += match.Length;
if (index != state.endIndex)
{
separatorSpan.CopyTo(span[destinationIndex..]);
destinationIndex += separatorSpan.Length;
}
}
});
}
}
}
}
19 changes: 10 additions & 9 deletions src/AutoMapper/Configuration/INamingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,50 @@
using System.Collections.Generic;
namespace AutoMapper
{
using StringChars = ReadOnlyMemory<char>;
/// <summary>
/// Defines a naming convention strategy
/// </summary>
public interface INamingConvention
{
string[] Split(string input);
List<StringChars> Split(string input);
string SeparatorCharacter { get; }
}
public class ExactMatchNamingConvention : INamingConvention
{
public static readonly ExactMatchNamingConvention Instance = new();
public string[] Split(string _) => Array.Empty<string>();
public List<StringChars> Split(string _) => null;
public string SeparatorCharacter => null;
}
public class PascalCaseNamingConvention : INamingConvention
{
public static readonly PascalCaseNamingConvention Instance = new();
public string SeparatorCharacter => "";
public string[] Split(string input)
public List<StringChars> Split(string input)
{
List<string> result = null;
List<StringChars> result = null;
int lower = 0;
for(int index = 1; index < input.Length; index++)
{
if (char.IsUpper(input[index]))
{
result ??= new();
result.Add(input[lower..index]);
result.Add(input.AsMemory(lower, index - lower));
lower = index;
}
}
if (result == null)
{
return Array.Empty<string>();
return null;
}
result.Add(input[lower..]);
return result.ToArray();
result.Add(input.AsMemory(lower));
return result;
}
}
public class LowerUnderscoreNamingConvention : INamingConvention
{
public static readonly LowerUnderscoreNamingConvention Instance = new();
public string SeparatorCharacter => "_";
public string[] Split(string input) => input.Split('_', StringSplitOptions.RemoveEmptyEntries);
public List<StringChars> Split(string input) => new(Array.ConvertAll(input.Split('_', StringSplitOptions.RemoveEmptyEntries), MemoryExtensions.AsMemory));
}
}
25 changes: 6 additions & 19 deletions src/UnitTests/Internal/TypeMapFactorySpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,13 @@ namespace AutoMapper.UnitTests.Tests
{
using AutoMapper.Internal;
using System;
using System.Collections.Generic;

public class StubNamingConvention : INamingConvention
{
private readonly Func<Match, string> _replaceFunc;

public StubNamingConvention(Func<Match, string> replaceFunc)
{
_replaceFunc = replaceFunc;
SeparatorCharacter = "";
}

public Regex SplittingExpression { get; set; }
public string SeparatorCharacter { get; set; }

public string[] Split(string input) => SplittingExpression.Matches(input).Select(m=>m.Value).ToArray();

public string ReplaceValue(Match match)
{
return _replaceFunc(match);
}
public List<ReadOnlyMemory<char>> Split(string input) => SplittingExpression.Matches(input).Select(m => m.Value.AsMemory()).ToList();
}

public class When_constructing_type_maps_with_matching_property_names : SpecBase
Expand Down Expand Up @@ -69,7 +56,7 @@ public class When_using_a_custom_source_naming_convention : SpecBase
{
private TypeMap _map;
private ProfileMap _mappingOptions;

private class Source
{
public SubSource some__source { get; set; }
Expand All @@ -91,7 +78,7 @@ private class TestProfile : Profile
}
protected override void Establish_context()
{
var namingConvention = new StubNamingConvention(s => s.Value.ToLower()){SeparatorCharacter = "__", SplittingExpression = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=__?)")};
var namingConvention = new StubNamingConvention{ SeparatorCharacter = "__", SplittingExpression = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=__?)") };

var profile = new TestProfile();
profile.Internal().AddMemberConfiguration().AddMember<NameSplitMember>(_ =>
Expand Down Expand Up @@ -141,7 +128,7 @@ private class TestProfile : Profile

protected override void Establish_context()
{
var namingConvention = new StubNamingConvention(s => s.Value.ToLower()) { SeparatorCharacter = "__", SplittingExpression = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=__?)") };
var namingConvention = new StubNamingConvention{ SeparatorCharacter = "__", SplittingExpression = new Regex(@"[\p{Ll}\p{Lu}0-9]+(?=__?)") };

var profile = new TestProfile();
profile.Internal().AddMemberConfiguration().AddMember<NameSplitMember>(_ =>
Expand Down Expand Up @@ -192,7 +179,7 @@ public void Should_map_properties_with_different_names()
});

var mapper = config.CreateMapper();
var dest = mapper.Map<Destination>(new Source {Ävíator = 3, SubAirlinaFlight = 4, Value = 5});
var dest = mapper.Map<Destination>(new Source { Ävíator = 3, SubAirlinaFlight = 4, Value = 5 });
dest.Aviator.ShouldBe(3);
dest.SubAirlineFlight.ShouldBe(4);
dest.Value.ShouldBe(5);
Expand Down
24 changes: 13 additions & 11 deletions src/UnitTests/ReverseMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using System.Linq;
using System;
using System.Text.RegularExpressions;
using System.Reflection;
using AutoMapper.Internal;
using System.Collections.Generic;

namespace AutoMapper.UnitTests
{
Expand All @@ -18,7 +18,7 @@ class Destination
{
public Guid Id { get; set; }
}
protected override MapperConfiguration CreateConfiguration() => new(c=>
protected override MapperConfiguration CreateConfiguration() => new(c =>
c.CreateMap<Destination, Source>().ForMember(src => src.Id, opt => opt.MapFrom(_ => Guid.Empty)).ReverseMap());
[Fact]
public void Validate() => AssertConfigurationIsValid();
Expand All @@ -45,7 +45,7 @@ public class Three2
{
}

protected override MapperConfiguration CreateConfiguration() => new(cfg=>
protected override MapperConfiguration CreateConfiguration() => new(cfg =>
{
cfg.CreateMap<One, Two>()
.ForMember(d => d.Name, o => o.MapFrom(s => "name"))
Expand Down Expand Up @@ -118,7 +118,7 @@ class OrderDto
public int OrderItemsCount { get; set; }
}

protected override MapperConfiguration CreateConfiguration() => new(c=>
protected override MapperConfiguration CreateConfiguration() => new(c =>
{
c.CreateMap<Order, OrderDto>().ReverseMap();
});
Expand Down Expand Up @@ -165,10 +165,12 @@ public class OrderDto
[Fact]
public void Should_flatten()
{
var model = new Order {
CustomerHolder = new CustomerHolder {
Customer = new Customer { Name = "George Costanza", Total = 74.85m }
}
var model = new Order
{
CustomerHolder = new CustomerHolder
{
Customer = new Customer { Name = "George Costanza", Total = 74.85m }
}
};
var dto = Mapper.Map<OrderDto>(model);
dto.CustomerName.ShouldBe("George Costanza");
Expand Down Expand Up @@ -306,8 +308,8 @@ public class OrderDto
{
cfg.CreateMap<Order, OrderDto>()
.ReverseMap()
.ForMember(d=>d.Customerholder, o=>o.Ignore())
.ForPath(d=>d.Customerholder.Customer.Total, o=>o.MapFrom(s=>s.CustomerholderCustomerTotal));
.ForMember(d => d.Customerholder, o => o.Ignore())
.ForPath(d => d.Customerholder.Customer.Total, o => o.MapFrom(s => s.CustomerholderCustomerTotal));
});

[Fact]
Expand Down Expand Up @@ -384,7 +386,7 @@ public class UnderscoreNamingConvention : INamingConvention
public Regex SplittingExpression { get; } = new Regex(@"\p{Lu}[a-z0-9]*(?=_?)");

public string SeparatorCharacter => "_";
public string[] Split(string input) => SplittingExpression.Matches(input).Select(m => m.Value).ToArray();
public List<ReadOnlyMemory<char>> Split(string input) => SplittingExpression.Matches(input).Select(m => m.Value.AsMemory()).ToList();
}

protected override MapperConfiguration CreateConfiguration() => new(cfg =>
Expand Down