Skip to content

Commit 1e2dc53

Browse files
committed
Integrate MaybeCompatible into TopicMappingService
Only evaluate whether there's a compatible or convertible member on the source `Topic` if either the target member is set to `MaybeCompatible` _or_ the source `Topic` is not `Topic`. In the vast majority of scenarios, this helps weed out extra reflection tests. We apply uncertainty in the case of derived `Topic` objects as we can't be sure they're the type that `TypeAccessor` evaluated for compatibility; regardless, this functionality is very rarely used and shouldn't be a considerable factor. This completes the implementation of #102. Unit tests will follow.
1 parent 359eaae commit 1e2dc53

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

OnTopic/Mapping/TopicMappingService.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,12 +643,20 @@ await MapAsync(
643643
\-----------------------------------------------------------------------------------------------------------------------*/
644644
var typeAccessor = GetTopicAccessor(source.GetType());
645645

646-
var attributeValue = typeAccessor.GetMethodValue(source, $"Get{configuration.GetCompositeAttributeKey(attributePrefix)}")?.ToString();
646+
var attributeValue = (string?)null;
647+
var maybeCompatible = source.GetType() != typeof(Topic) || itemMetadata.MaybeCompatible;
647648

648649
/*------------------------------------------------------------------------------------------------------------------------
649650
| Attempt to retrieve value from topic.{Property}
650651
\-----------------------------------------------------------------------------------------------------------------------*/
651-
if (attributeValue is null) {
652+
if (maybeCompatible) {
653+
attributeValue = typeAccessor.GetMethodValue(source, $"Get{configuration.GetCompositeAttributeKey(attributePrefix)}")?.ToString();
654+
}
655+
656+
/*------------------------------------------------------------------------------------------------------------------------
657+
| Attempt to retrieve value from topic.{Property}
658+
\-----------------------------------------------------------------------------------------------------------------------*/
659+
if (maybeCompatible && attributeValue is null) {
652660
attributeValue = typeAccessor.GetPropertyValue(source, configuration.GetCompositeAttributeKey(attributePrefix))?.ToString();
653661
}
654662

@@ -888,7 +896,7 @@ sourcePropertyValue[0] is Topic
888896
| Provide local function for evaluating current collection
889897
\-----------------------------------------------------------------------------------------------------------------------*/
890898
IList<Topic> getCollection(CollectionType collection, Func<string, bool> contains, Func<IList<Topic>> getTopics) {
891-
var targetAssociations = AssociationMap.Mappings[collection];
899+
var targetAssociations = AssociationMap.Mappings[collection];
892900
var preconditionsMet =
893901
listSource.Count == 0 &&
894902
(collectionType is CollectionType.Any || collectionType.Equals(collection)) &&
@@ -1129,6 +1137,14 @@ private static bool TryGetCompatibleProperty(Topic source, Type targetType, Item
11291137
\-----------------------------------------------------------------------------------------------------------------------*/
11301138
var configuration = itemMetadata.Configuration;
11311139

1140+
/*------------------------------------------------------------------------------------------------------------------------
1141+
| Rely on MaybeCompatible to bypass known incompatible types
1142+
\-----------------------------------------------------------------------------------------------------------------------*/
1143+
if (source.GetType() == typeof(Topic) && !itemMetadata.MaybeCompatible) {
1144+
value = null;
1145+
return false;
1146+
};
1147+
11321148
/*------------------------------------------------------------------------------------------------------------------------
11331149
| Attempt to retrieve value from topic.{Property}
11341150
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)