diff --git a/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/001_PureMappingMethod.txt b/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/001_PureMappingMethod.txt index 4a591e5..2eda597 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/001_PureMappingMethod.txt +++ b/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/001_PureMappingMethod.txt @@ -47,6 +47,12 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public int? FourthAuthentication { get; set;} public AuthenticationKind? FifthAuthentication { get; set;} public Guid UniqueKey { get; set;} + public string YetAnotherSnakeCaseField { get; set;} + public string YetAnotherAddressCity { get; set; } + public string YetAnotherAddressZipCode { get; set; } + public string YetAnotherAddressStreet { get; set; } + public string YetAnotherAddressFlatNo { get; set; } + public string YetAnotherAddressBuildingNo { get; set; } } public class UserSourceDTO @@ -119,6 +125,8 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public AuthenticationKind? FourthAuthentication { get; set;} public int? FifthAuthentication { get; set;} public Guid UniqueKey { get; set;} + public string YET_ANOTHER_SNAKE_CASE_FIELD { get; set;} + public AddressEntity YetAnotherAddress { get; set;} } public class AddressEntity @@ -127,7 +135,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public string ZipCode { get; set; } public string Street { get; set; } public string FlatNo { get; set; } - public string BuildingNo { get; set; } + public string Building_No { get; set; } } public class UnitEntity diff --git a/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/001_PureMappingMethod_FIXED.txt b/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/001_PureMappingMethod_FIXED.txt index cbc0c84..ab026c7 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/001_PureMappingMethod_FIXED.txt +++ b/MappingGenerator/MappingGenerator/MappingGenerator.Test/MappingGenerator/TestCaseData/001_PureMappingMethod_FIXED.txt @@ -32,7 +32,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData ZipCode = entity.MainAddress.ZipCode, Street = entity.MainAddress.Street, FlatNo = entity.MainAddress.FlatNo, - BuildingNo = entity.MainAddress.BuildingNo + BuildingNo = entity.MainAddress.Building_No }, Addresses = entity.Addresses.ConvertAll(entityAddress => new AddressDTO { @@ -40,7 +40,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData ZipCode = entityAddress.ZipCode, Street = entityAddress.Street, FlatNo = entityAddress.FlatNo, - BuildingNo = entityAddress.BuildingNo + BuildingNo = entityAddress.Building_No }).AsReadOnly(), ExtraAddresses = entity.ExtraAddresses.Select(entityExtraAddress => new AddressDTO { @@ -48,7 +48,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData ZipCode = entityExtraAddress.ZipCode, Street = entityExtraAddress.Street, FlatNo = entityExtraAddress.FlatNo, - BuildingNo = entityExtraAddress.BuildingNo + BuildingNo = entityExtraAddress.Building_No }).ToImmutableArray(), UnitId = entity.Unit.Id, ExtraSavings = entity.ExtraSavings ?? throw new ArgumentNullException(nameof(entity), "The value of 'entity.ExtraSavings' should not be null"), @@ -60,6 +60,12 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData FourthAuthentication = (int?)entity.FourthAuthentication, FifthAuthentication = (AuthenticationKind?)entity.FifthAuthentication, UniqueKey = entity.UniqueKey, + YetAnotherSnakeCaseField = entity.YET_ANOTHER_SNAKE_CASE_FIELD, + YetAnotherAddressCity = entity.YetAnotherAddress.City, + YetAnotherAddressZipCode = entity.YetAnotherAddress.ZipCode, + YetAnotherAddressStreet = entity.YetAnotherAddress.Street, + YetAnotherAddressFlatNo = entity.YetAnotherAddress.FlatNo, + YetAnotherAddressBuildingNo = entity.YetAnotherAddress.Building_No, Version = entity.Version }; } @@ -98,6 +104,12 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public int? FourthAuthentication { get; set;} public AuthenticationKind? FifthAuthentication { get; set;} public Guid UniqueKey { get; set;} + public string YetAnotherSnakeCaseField { get; set;} + public string YetAnotherAddressCity { get; set; } + public string YetAnotherAddressZipCode { get; set; } + public string YetAnotherAddressStreet { get; set; } + public string YetAnotherAddressFlatNo { get; set; } + public string YetAnotherAddressBuildingNo { get; set; } } public class UserSourceDTO @@ -170,6 +182,8 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public AuthenticationKind? FourthAuthentication { get; set;} public int? FifthAuthentication { get; set;} public Guid UniqueKey { get; set;} + public string YET_ANOTHER_SNAKE_CASE_FIELD { get; set;} + public AddressEntity YetAnotherAddress { get; set;} } public class AddressEntity @@ -178,7 +192,7 @@ namespace MappingGenerator.Test.MappingGenerator.TestCaseData public string ZipCode { get; set; } public string Street { get; set; } public string FlatNo { get; set; } - public string BuildingNo { get; set; } + public string Building_No { get; set; } } public class UnitEntity diff --git a/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/SourceFinders/ObjectMembersMappingSourceFinder.cs b/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/SourceFinders/ObjectMembersMappingSourceFinder.cs index acfb04b..bc76558 100644 --- a/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/SourceFinders/ObjectMembersMappingSourceFinder.cs +++ b/MappingGenerator/MappingGenerator/MappingGenerator/Mappings/SourceFinders/ObjectMembersMappingSourceFinder.cs @@ -125,12 +125,12 @@ private MappingElement TryFindSource(string targetName, MappingContext mappingCo { //Direct 1-1 mapping var matchedSourceProperty = sourceProperties.Value - .Where(x => x.Name.Equals(targetName, StringComparison.OrdinalIgnoreCase) || $"{potentialPrefix}{x.Name}".Equals(targetName, StringComparison.OrdinalIgnoreCase)) + .Where(x => IsMatched(targetName, x, potentialPrefix)) .FirstOrDefault(p => p.CanBeGet(accessedVia.Type, mappingContext)); if (matchedSourceProperty != null) { - return new MappingElement() + return new MappingElement { Expression = SyntaxFactoryExtensions.CreateMemberAccessExpression((ExpressionSyntax)sourceGlobalAccessor, accessedVia.CanBeNull, matchedSourceProperty.Name), ExpressionType = new AnnotatedType(matchedSourceProperty.Type.Type, accessedVia.CanBeNull || matchedSourceProperty.Type.CanBeNull) @@ -170,6 +170,16 @@ private MappingElement TryFindSource(string targetName, MappingContext mappingCo return null; } + private static bool IsMatched(string targetName, IObjectField source, string sourceAccessPrefix) + { + var sanitizedName = SanitizeName(source.Name); + var sanitizedTargetName = SanitizeName(targetName); + var sanitizedNameWithPrefix = SanitizeName($"{sourceAccessPrefix}{source.Name}"); + return sanitizedName.Equals(sanitizedTargetName, StringComparison.OrdinalIgnoreCase) || sanitizedNameWithPrefix.Equals(sanitizedTargetName, StringComparison.OrdinalIgnoreCase); + } + + private static string SanitizeName(string name) => name.Replace("_", ""); + private readonly Regex acronymPattern = new Regex(@"(? targetName.StartsWith($"{prefix}{x.Name}", StringComparison.OrdinalIgnoreCase)) + var subProperty = properties.Where(x => SanitizeName(targetName).StartsWith(SanitizeName($"{prefix}{x.Name}"), StringComparison.OrdinalIgnoreCase)) .FirstOrDefault(p => p.CanBeGet(containingType, mappingContext)); if (subProperty != null) { var currentNamePart = $"{prefix}{subProperty.Name}"; var subPropertyAccessor = SyntaxFactoryExtensions.CreateMemberAccessExpression((ExpressionSyntax)currentAccessor, isCurrentAccessorNullable, subProperty.Name); var expressionCanBeNull = isCurrentAccessorNullable || subProperty.Type.CanBeNull; - if (targetName.Equals(currentNamePart, StringComparison.OrdinalIgnoreCase)) + if (SanitizeName(targetName).Equals(SanitizeName(currentNamePart), StringComparison.OrdinalIgnoreCase)) { return new MappingElement