Skip to content

Commit 13a8ff5

Browse files
committed
Removing mapping plan enum mismatch warnings if enum pairs are configured
1 parent 8177fb3 commit 13a8ff5

File tree

5 files changed

+93
-28
lines changed

5 files changed

+93
-28
lines changed

AgileMapper.UnitTests/Configuration/WhenConfiguringEnumMapping.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ public void ShouldPairEnumMembers()
2828
}
2929
}
3030

31+
[Fact]
32+
public void ShouldRemovePairedEnumsFromEnumMismatchWarnings()
33+
{
34+
using (var mapper = Mapper.CreateNew())
35+
{
36+
mapper.WhenMapping
37+
.PairEnum(PaymentTypeUk.Cheque).With(PaymentTypeUs.Check);
38+
39+
var plan = mapper
40+
.GetPlanFor<PublicTwoFields<PaymentTypeUk, PaymentTypeUs>>()
41+
.OnTo<PublicTwoFields<PaymentTypeUs, PaymentTypeUk>>();
42+
43+
plan.ShouldNotContain("WARNING");
44+
}
45+
}
46+
3147
[Fact]
3248
public void ShouldErrorIfSourceValueIsNotAnEnum()
3349
{

AgileMapper/Api/Configuration/EnumPairSpecifier.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,14 @@ public MappingConfigContinuation<object, object> With<TSecondEnum>(TSecondEnum s
5555
ThrowIfNotEnumType<TSecondEnum>();
5656
ThrowIfSameTypes<TSecondEnum>();
5757

58-
var enumPairing = EnumMemberPair.For(_configInfo, _firstEnumMember, secondEnumMember);
59-
_configInfo.MapperContext.UserConfigurations.Add(enumPairing);
58+
var firstToSecondPairing = EnumMemberPair.For(_firstEnumMember, secondEnumMember);
59+
var secondToFirstPairing = EnumMemberPair.For(secondEnumMember, _firstEnumMember);
60+
61+
_configInfo.MapperContext.ValueConverters.Add(firstToSecondPairing.ValueConverter);
62+
_configInfo.MapperContext.ValueConverters.Add(secondToFirstPairing.ValueConverter);
63+
64+
_configInfo.MapperContext.UserConfigurations.Add(firstToSecondPairing);
65+
_configInfo.MapperContext.UserConfigurations.Add(secondToFirstPairing);
6066

6167
return new MappingConfigContinuation<object, object>(_configInfo);
6268
}
Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,55 @@
11
namespace AgileObjects.AgileMapper.Configuration
22
{
3+
using System;
34
using System.Linq.Expressions;
45
using TypeConversion;
56

6-
internal class EnumMemberPair : UserConfiguredItemBase
7+
internal class EnumMemberPair
78
{
8-
private readonly object _firstEnumMember;
9-
private readonly object _secondEnumMember;
9+
private readonly Type _firstEnumType;
10+
private readonly Type _secondEnumType;
1011

1112
public EnumMemberPair(
12-
MappingConfigInfo configInfo,
13-
object firstEnumMember,
14-
object secondEnumMember)
15-
: base(configInfo)
13+
Type firstEnumType,
14+
string firstEnumMemberName,
15+
Type secondEnumType,
16+
string secondEnumMemberName,
17+
IValueConverter valueConverter)
1618
{
17-
_firstEnumMember = firstEnumMember;
18-
_secondEnumMember = secondEnumMember;
19+
FirstEnumMemberName = firstEnumMemberName;
20+
SecondEnumMemberName = secondEnumMemberName;
21+
ValueConverter = valueConverter;
22+
_firstEnumType = firstEnumType;
23+
_secondEnumType = secondEnumType;
1924
}
2025

2126
public static EnumMemberPair For<TFirstEnum, TSecondEnum>(
22-
MappingConfigInfo configInfo,
2327
TFirstEnum firstEnumMember,
2428
TSecondEnum secondEnumMember)
2529
{
2630
var firstValue = Expression.Constant(firstEnumMember, typeof(TFirstEnum));
2731
var secondValue = Expression.Constant(secondEnumMember, typeof(TSecondEnum));
2832

29-
var firstToSecondValueConverter = new ConfiguredValueConverter(
33+
var valueConverter = new ConfiguredValueConverter(
3034
firstValue.Type,
3135
secondValue,
3236
(sourceValue, targetType) => Expression.Equal(sourceValue, firstValue));
3337

34-
var secondToFirstValueConverter = new ConfiguredValueConverter(
38+
return new EnumMemberPair(
39+
firstValue.Type,
40+
firstEnumMember.ToString(),
3541
secondValue.Type,
36-
firstValue,
37-
(sourceValue, targetType) => Expression.Equal(sourceValue, secondValue));
42+
secondEnumMember.ToString(),
43+
valueConverter);
44+
}
3845

39-
configInfo.MapperContext.ValueConverters.Add(firstToSecondValueConverter);
40-
configInfo.MapperContext.ValueConverters.Add(secondToFirstValueConverter);
46+
public string FirstEnumMemberName { get; }
4147

42-
return new EnumMemberPair(configInfo, firstEnumMember, secondEnumMember);
43-
}
48+
public string SecondEnumMemberName { get; }
49+
50+
public IValueConverter ValueConverter { get; }
51+
52+
public bool IsFor(Type sourceEnumType, Type targetEnumType)
53+
=> (_firstEnumType == sourceEnumType) && (_secondEnumType == targetEnumType);
4454
}
4555
}

AgileMapper/Configuration/UserConfigurationSet.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ internal class UserConfigurationSet
1414
private readonly ICollection<ObjectTrackingMode> _trackingModeSettings;
1515
private readonly ICollection<ConfiguredObjectFactory> _objectFactories;
1616
private readonly ICollection<ConfiguredIgnoredMember> _ignoredMembers;
17+
private readonly ICollection<EnumMemberPair> _enumParings;
1718
private readonly ICollection<ConfiguredDataSourceFactory> _dataSourceFactories;
1819
private readonly ICollection<MappingCallbackFactory> _mappingCallbackFactories;
1920
private readonly ICollection<ObjectCreationCallbackFactory> _creationCallbackFactories;
@@ -25,6 +26,7 @@ public UserConfigurationSet()
2526
_objectFactories = new List<ConfiguredObjectFactory>();
2627
Identifiers = new MemberIdentifierSet();
2728
_ignoredMembers = new List<ConfiguredIgnoredMember>();
29+
_enumParings = new List<EnumMemberPair>();
2830
_dataSourceFactories = new List<ConfiguredDataSourceFactory>();
2931
_mappingCallbackFactories = new List<MappingCallbackFactory>();
3032
_creationCallbackFactories = new List<ObjectCreationCallbackFactory>();
@@ -84,8 +86,11 @@ public ConfiguredIgnoredMember GetMemberIgnoreOrNull(IBasicMapperData mapperData
8486

8587
public void Add(EnumMemberPair enumPairing)
8688
{
89+
_enumParings.Add(enumPairing);
8790
}
8891

92+
public IEnumerable<EnumMemberPair> EnumParings => _enumParings;
93+
8994
#endregion
9095

9196
#region DataSources

AgileMapper/Plans/EnumMappingMismatchSet.cs

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ public static EnumMappingMismatch For(
106106
{
107107
var mismatches = GetMappingMismatches(
108108
sourceEnumType,
109-
targetEnumType.Name,
110-
targetEnumNames);
109+
targetEnumType,
110+
targetEnumNames,
111+
mapperData);
111112

112113
if (mismatches.None())
113114
{
@@ -119,27 +120,54 @@ public static EnumMappingMismatch For(
119120

120121
private static string[] GetMappingMismatches(
121122
Type sourceEnumType,
122-
string targetEnumTypeName,
123-
string[] targetEnumNames)
123+
Type targetEnumType,
124+
string[] targetEnumNames,
125+
IMemberMapperData mapperData)
124126
{
125127
var sourceEnumNames = Enum.GetNames(sourceEnumType);
126-
var unmatchedSourceValues = sourceEnumNames.Except(targetEnumNames).ToArray();
127-
var unmatchedTargetValues = targetEnumNames.Except(sourceEnumNames).ToArray();
128+
var unmatchedSourceValues = sourceEnumNames.Except(targetEnumNames).ToList();
129+
var unmatchedTargetValues = targetEnumNames.Except(sourceEnumNames).ToList();
130+
131+
Filter(unmatchedSourceValues, unmatchedTargetValues, sourceEnumType, targetEnumType, mapperData);
128132

129133
if (unmatchedSourceValues.None() && unmatchedTargetValues.None())
130134
{
131135
return Constants.EmptyStringArray;
132136
}
133137

134138
var unmatchedValues = unmatchedSourceValues
135-
.Select(value => $" - {sourceEnumType.Name}.{value} matches no {targetEnumTypeName}")
139+
.Select(value => $" - {sourceEnumType.Name}.{value} matches no {targetEnumType.Name}")
136140
.Concat(unmatchedTargetValues
137-
.Select(value => $" - {targetEnumTypeName}.{value} is matched by no {sourceEnumType.Name}"))
141+
.Select(value => $" - {targetEnumType.Name}.{value} is matched by no {sourceEnumType.Name}"))
138142
.ToArray();
139143

140144
return unmatchedValues;
141145
}
142146

147+
private static void Filter(
148+
ICollection<string> unmatchedSourceValues,
149+
ICollection<string> unmatchedTargetValues,
150+
Type sourceEnumType,
151+
Type targetEnumType,
152+
IMemberMapperData mapperData)
153+
{
154+
var relevantEnumPairings = mapperData
155+
.MapperContext
156+
.UserConfigurations
157+
.EnumParings
158+
.Where(ep => ep.IsFor(sourceEnumType, targetEnumType));
159+
160+
foreach (var enumParing in relevantEnumPairings)
161+
{
162+
if (unmatchedSourceValues.Contains(enumParing.FirstEnumMemberName) &&
163+
unmatchedTargetValues.Contains(enumParing.SecondEnumMemberName))
164+
{
165+
unmatchedSourceValues.Remove(enumParing.FirstEnumMemberName);
166+
unmatchedTargetValues.Remove(enumParing.SecondEnumMemberName);
167+
}
168+
}
169+
}
170+
143171
#endregion
144172

145173
public bool Any => Warning != null;

0 commit comments

Comments
 (0)