Skip to content

Commit 26828bb

Browse files
authored
Fixed trim warnings in Object Persistence high level library introduced by PR adding polymorphic support. (#3749)
1 parent c618252 commit 26828bb

File tree

3 files changed

+67
-34
lines changed

3 files changed

+67
-34
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"services": [
3+
{
4+
"serviceName": "DynamoDBv2",
5+
"type": "patch",
6+
"changeLogMessages": [
7+
"Fixed trim warnings in Object Persistence high level library introduced by PR adding polymorphic support."
8+
]
9+
}
10+
]
11+
}

sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs

+23-20
Original file line numberDiff line numberDiff line change
@@ -538,13 +538,13 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB
538538
Document document = entry as Document;
539539
if (document != null)
540540
{
541-
if (TryFromMap(targetType, document, flatConfig, propertyStorage.DerivedTypeKeysDictionary, out output))
541+
if (TryFromMap(targetType, document, flatConfig, propertyStorage, out output))
542542
return output;
543543

544544
var typeAttributeName = flatConfig.DerivedTypeAttributeName;//"$type";
545545
var derivedType = document.ContainsKey(typeAttributeName) ? document[typeAttributeName].AsString() : null;
546546

547-
if (derivedType != null && propertyStorage.DerivedTypeKeysDictionary.TryGetValue(derivedType, out var value))
547+
if (derivedType != null && propertyStorage.TryGetDerivedType(derivedType, out var value))
548548
{
549549
targetType = value;
550550
}
@@ -554,7 +554,7 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB
554554

555555
DynamoDBList list = entry as DynamoDBList;
556556
if (list != null &&
557-
TryFromList(targetType, list, flatConfig, propertyStorage.DerivedTypeKeysDictionary, out output))
557+
TryFromList(targetType, list, flatConfig, propertyStorage, out output))
558558
{
559559
return output;
560560
}
@@ -565,17 +565,17 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB
565565
}
566566
}
567567
private bool TryFromList([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig,
568-
Dictionary<string, Type> derivedTypeKeysDictionary, out object output)
568+
SimplePropertyStorage parentPropertyStorage, out object output)
569569
{
570570
return targetType.IsArray ?
571-
TryFromListToArray(targetType, list, flatConfig, derivedTypeKeysDictionary, out output) : //targetType is Array
572-
TryFromListToIList(targetType, list, flatConfig, derivedTypeKeysDictionary, out output) ; //targetType is IList or has Add method.
571+
TryFromListToArray(targetType, list, flatConfig, parentPropertyStorage, out output) : //targetType is Array
572+
TryFromListToIList(targetType, list, flatConfig, parentPropertyStorage, out output) ; //targetType is IList or has Add method.
573573
}
574574

575575
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062",
576576
Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")]
577577

578-
private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig,Dictionary<string, Type> derivedTypeKeysDictionary, out object output)
578+
private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, SimplePropertyStorage parentPropertyStorage, out object output)
579579
{
580580
if ((!Utils.ImplementsInterface(targetType, typeof(ICollection<>)) &&
581581
!Utils.ImplementsInterface(targetType, typeof(IList))) ||
@@ -589,7 +589,7 @@ private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.Da
589589
var collection = Utils.Instantiate(targetType);
590590
IList ilist = collection as IList;
591591
bool useIListInterface = ilist != null;
592-
var propertyStorage = new SimplePropertyStorage(elementType, derivedTypeKeysDictionary);
592+
var propertyStorage = new SimplePropertyStorage(elementType, parentPropertyStorage);
593593

594594
MethodInfo collectionAdd = null;
595595
if (!useIListInterface)
@@ -611,7 +611,7 @@ private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.Da
611611
return true;
612612
}
613613

614-
private bool TryFromListToArray([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, Dictionary<string, Type> derivedTypeKeysDictionary, out object output)
614+
private bool TryFromListToArray([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig, SimplePropertyStorage parentPropertyStorage, out object output)
615615
{
616616
if (!Utils.CanInstantiateArray(targetType))
617617
{
@@ -621,7 +621,7 @@ private bool TryFromListToArray([DynamicallyAccessedMembers(InternalConstants.Da
621621

622622
var elementType = Utils.GetElementType(targetType);
623623
var array = (Array)Utils.InstantiateArray(targetType,list.Entries.Count);
624-
var propertyStorage = new SimplePropertyStorage(elementType, derivedTypeKeysDictionary);
624+
var propertyStorage = new SimplePropertyStorage(elementType, parentPropertyStorage);
625625

626626
for (int i = 0; i < list.Entries.Count; i++)
627627
{
@@ -636,7 +636,7 @@ private bool TryFromListToArray([DynamicallyAccessedMembers(InternalConstants.Da
636636

637637
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067",
638638
Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")]
639-
private bool TryFromMap([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, Document map, DynamoDBFlatConfig flatConfig, Dictionary<string, Type> derivedTypeKeysDictionary, out object output)
639+
private bool TryFromMap([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, Document map, DynamoDBFlatConfig flatConfig, SimplePropertyStorage parentPropertyStorage, out object output)
640640
{
641641
output = null;
642642

@@ -649,7 +649,7 @@ private bool TryFromMap([DynamicallyAccessedMembers(InternalConstants.DataModelM
649649

650650
var dictionary = Utils.Instantiate(targetType);
651651
var idictionary = dictionary as IDictionary;
652-
var propertyStorage = new SimplePropertyStorage(valueType, derivedTypeKeysDictionary);
652+
var propertyStorage = new SimplePropertyStorage(valueType, parentPropertyStorage);
653653

654654
foreach (var kvp in map)
655655
{
@@ -668,6 +668,9 @@ internal DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, ob
668668
{
669669
return ToDynamoDBEntry(propertyStorage, value, flatConfig, canReturnScalarInsteadOfList: false);
670670
}
671+
672+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2072",
673+
Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")]
671674
private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, object value, DynamoDBFlatConfig flatConfig, bool canReturnScalarInsteadOfList)
672675
{
673676
if (value == null)
@@ -685,9 +688,9 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj
685688

686689
Type type;
687690
string typeDiscriminator = null;
688-
if (propertyStorage.DerivedTypesDictionary.ContainsKey(value.GetType()))
691+
if (propertyStorage.TryGetDerivedTypeDiscriminator(value.GetType(), out var td))
689692
{
690-
typeDiscriminator = propertyStorage.DerivedTypesDictionary[value.GetType()];
693+
typeDiscriminator = td;
691694
type = value.GetType();
692695
}
693696
else
@@ -707,11 +710,11 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj
707710
else
708711
{
709712
Document map;
710-
if (TryToMap(value, type, flatConfig, propertyStorage.DerivedTypesDictionary, out map))
713+
if (TryToMap(value, type, flatConfig, propertyStorage, out map))
711714
return map;
712715

713716
DynamoDBList list;
714-
if (TryToList(value, type, flatConfig, propertyStorage.DerivedTypesDictionary, out list))
717+
if (TryToList(value, type, flatConfig, propertyStorage, out list))
715718
return list;
716719

717720
return SerializeToDocument(value, type, flatConfig, typeDiscriminator);
@@ -721,7 +724,7 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj
721724
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067",
722725
Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")]
723726
private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type, DynamoDBFlatConfig flatConfig,
724-
Dictionary<Type, string> derivedTypesDictionary, out Document output)
727+
SimplePropertyStorage parentPropertyStorage, out Document output)
725728
{
726729
output = null;
727730

@@ -734,7 +737,7 @@ private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstant
734737
return false;
735738

736739
output = new Document();
737-
SimplePropertyStorage propertyStorage = new SimplePropertyStorage(valueType,derivedTypesDictionary);
740+
SimplePropertyStorage propertyStorage = new SimplePropertyStorage(valueType, parentPropertyStorage);
738741

739742
foreach (object keyValue in idictionary.Keys)
740743
{
@@ -755,7 +758,7 @@ private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstant
755758
}
756759

757760
private bool TryToList(object value, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] Type type, DynamoDBFlatConfig flatConfig,
758-
Dictionary<Type, string> derivedTypesDictionary, out DynamoDBList output)
761+
SimplePropertyStorage parentPropertyStoragey, out DynamoDBList output)
759762
{
760763
if (!Utils.ImplementsInterface(type, typeof(ICollection<>)))
761764
{
@@ -774,7 +777,7 @@ private bool TryToList(object value, [DynamicallyAccessedMembers(DynamicallyAcce
774777

775778
Type elementType = Utils.GetElementType(type);
776779

777-
SimplePropertyStorage propertyStorage = new SimplePropertyStorage(elementType,derivedTypesDictionary);
780+
SimplePropertyStorage propertyStorage = new SimplePropertyStorage(elementType, parentPropertyStoragey);
778781
output = new DynamoDBList();
779782
foreach (var item in enumerable)
780783
{

sdk/src/Services/DynamoDBv2/Custom/DataModel/InternalModel.cs

+33-14
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,39 @@ internal class SimplePropertyStorage
5757
// Converter, if one is present
5858
public IPropertyConverter Converter { get; protected set; }
5959

60-
public void AddDerivedType(string typeDiscriminator, Type type)
60+
public void AddDerivedType(string typeDiscriminator, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type)
6161
{
62-
DerivedTypesDictionary[type] = typeDiscriminator;
63-
DerivedTypeKeysDictionary[typeDiscriminator] = type;
62+
_derivedTypesDictionary[type] = typeDiscriminator;
63+
_derivedTypeKeysDictionary[typeDiscriminator] = type;
6464
}
6565

6666
// derived type information used for polymorphic serialization
67-
public Dictionary<Type, string> DerivedTypesDictionary { get; private set; }
67+
private Dictionary<Type, string> _derivedTypesDictionary;
6868

6969
// derived type information used for polymorphic deserialization
70-
public Dictionary<string, Type> DerivedTypeKeysDictionary { get; private set; }
70+
private Dictionary<string, Type> _derivedTypeKeysDictionary;
71+
72+
public bool TryGetDerivedTypeDiscriminator([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type, out string typeDiscriminator)
73+
{
74+
if (_derivedTypesDictionary.TryGetValue(type, out typeDiscriminator))
75+
{
76+
return true;
77+
}
78+
79+
return false;
80+
}
81+
82+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067",
83+
Justification = "The user's type has been annotated with InternalConstants.DataModelModeledType with the public API into the library. At this point the type will not be trimmed.")]
84+
public bool TryGetDerivedType(string typeDiscriminator, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] out Type deriviedType)
85+
{
86+
if (_derivedTypeKeysDictionary.TryGetValue(typeDiscriminator, out deriviedType))
87+
{
88+
return true;
89+
}
90+
91+
return false;
92+
}
7193

7294
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2072",
7395
Justification = "The user's type has been annotated with DynamicallyAccessedMemberTypes.All with the public API into the library. At this point the type will not be trimmed.")]
@@ -81,18 +103,15 @@ internal SimplePropertyStorage(MemberInfo member)
81103
internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType)
82104
{
83105
MemberType = memberType;
84-
DerivedTypesDictionary = new Dictionary<Type, string>();
85-
DerivedTypeKeysDictionary = new Dictionary<string,Type>();
106+
_derivedTypesDictionary = new Dictionary<Type, string>();
107+
_derivedTypeKeysDictionary = new Dictionary<string,Type>();
86108
}
87-
internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, Dictionary<Type, string> derivedTypesDictionary)
88-
{
89-
MemberType = memberType;
90-
DerivedTypesDictionary = derivedTypesDictionary;
91-
}
92-
internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, Dictionary<string, Type> derivedTypeKeysDictionary)
109+
110+
internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, SimplePropertyStorage parentProeprtyStorage)
93111
{
94112
MemberType = memberType;
95-
DerivedTypeKeysDictionary = derivedTypeKeysDictionary;
113+
_derivedTypesDictionary = parentProeprtyStorage._derivedTypesDictionary;
114+
_derivedTypeKeysDictionary = parentProeprtyStorage._derivedTypeKeysDictionary;
96115
}
97116

98117
public override string ToString()

0 commit comments

Comments
 (0)