Skip to content
This repository has been archived by the owner on Jan 16, 2022. It is now read-only.

Commit

Permalink
#145: Ignore underscore while matching field names
Browse files Browse the repository at this point in the history
  • Loading branch information
cezarypiatek committed Nov 11, 2020
1 parent 0cd376f commit 53a77b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,23 @@ 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
{
City = entityAddress.City,
ZipCode = entityAddress.ZipCode,
Street = entityAddress.Street,
FlatNo = entityAddress.FlatNo,
BuildingNo = entityAddress.BuildingNo
BuildingNo = entityAddress.Building_No
}).AsReadOnly(),
ExtraAddresses = entity.ExtraAddresses.Select(entityExtraAddress => new AddressDTO
{
City = entityExtraAddress.City,
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"),
Expand All @@ -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
};
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(@"(?<!^)(?=[A-Z])", RegexOptions.Compiled);

private string GetAcronym(string targetName)
Expand All @@ -185,14 +195,14 @@ private MappingElement FindSubPropertySource(string targetName, ITypeSymbol cont
return null;
}

var subProperty = properties.Where(x => 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
Expand Down

0 comments on commit 53a77b2

Please sign in to comment.