Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions generator/.DevConfigs/FD891AA9-310E-4F80-B97A-00C8B7B97BA3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"services": [
{
"serviceName": "DynamoDBv2",
"type": "patch",
"changeLogMessages": [
"Fixed trim warnings in Object Persistence high level library introduced by PR adding polymorphic support."
]
}
]
}
43 changes: 23 additions & 20 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -538,13 +538,13 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB
Document document = entry as Document;
if (document != null)
{
if (TryFromMap(targetType, document, flatConfig, propertyStorage.DerivedTypeKeysDictionary, out output))
if (TryFromMap(targetType, document, flatConfig, propertyStorage, out output))
return output;

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

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

DynamoDBList list = entry as DynamoDBList;
if (list != null &&
TryFromList(targetType, list, flatConfig, propertyStorage.DerivedTypeKeysDictionary, out output))
TryFromList(targetType, list, flatConfig, propertyStorage, out output))
{
return output;
}
Expand All @@ -565,17 +565,17 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB
}
}
private bool TryFromList([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, DynamoDBList list, DynamoDBFlatConfig flatConfig,
Dictionary<string, Type> derivedTypeKeysDictionary, out object output)
SimplePropertyStorage parentPropertyStorage, out object output)
{
return targetType.IsArray ?
TryFromListToArray(targetType, list, flatConfig, derivedTypeKeysDictionary, out output) : //targetType is Array
TryFromListToIList(targetType, list, flatConfig, derivedTypeKeysDictionary, out output) ; //targetType is IList or has Add method.
TryFromListToArray(targetType, list, flatConfig, parentPropertyStorage, out output) : //targetType is Array
TryFromListToIList(targetType, list, flatConfig, parentPropertyStorage, out output) ; //targetType is IList or has Add method.
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2062",
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.")]

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

MethodInfo collectionAdd = null;
if (!useIListInterface)
Expand All @@ -611,7 +611,7 @@ private bool TryFromListToIList([DynamicallyAccessedMembers(InternalConstants.Da
return true;
}

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

var elementType = Utils.GetElementType(targetType);
var array = (Array)Utils.InstantiateArray(targetType,list.Entries.Count);
var propertyStorage = new SimplePropertyStorage(elementType, derivedTypeKeysDictionary);
var propertyStorage = new SimplePropertyStorage(elementType, parentPropertyStorage);

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

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067",
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.")]
private bool TryFromMap([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, Document map, DynamoDBFlatConfig flatConfig, Dictionary<string, Type> derivedTypeKeysDictionary, out object output)
private bool TryFromMap([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type targetType, Document map, DynamoDBFlatConfig flatConfig, SimplePropertyStorage parentPropertyStorage, out object output)
{
output = null;

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

var dictionary = Utils.Instantiate(targetType);
var idictionary = dictionary as IDictionary;
var propertyStorage = new SimplePropertyStorage(valueType, derivedTypeKeysDictionary);
var propertyStorage = new SimplePropertyStorage(valueType, parentPropertyStorage);

foreach (var kvp in map)
{
Expand All @@ -668,6 +668,9 @@ internal DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, ob
{
return ToDynamoDBEntry(propertyStorage, value, flatConfig, canReturnScalarInsteadOfList: false);
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2072",
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.")]
private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, object value, DynamoDBFlatConfig flatConfig, bool canReturnScalarInsteadOfList)
{
if (value == null)
Expand All @@ -685,9 +688,9 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj

Type type;
string typeDiscriminator = null;
if (propertyStorage.DerivedTypesDictionary.ContainsKey(value.GetType()))
if (propertyStorage.TryGetDerivedTypeDiscriminator(value.GetType(), out var td))
{
typeDiscriminator = propertyStorage.DerivedTypesDictionary[value.GetType()];
typeDiscriminator = td;
type = value.GetType();
}
else
Expand All @@ -707,11 +710,11 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj
else
{
Document map;
if (TryToMap(value, type, flatConfig, propertyStorage.DerivedTypesDictionary, out map))
if (TryToMap(value, type, flatConfig, propertyStorage, out map))
return map;

DynamoDBList list;
if (TryToList(value, type, flatConfig, propertyStorage.DerivedTypesDictionary, out list))
if (TryToList(value, type, flatConfig, propertyStorage, out list))
return list;

return SerializeToDocument(value, type, flatConfig, typeDiscriminator);
Expand All @@ -721,7 +724,7 @@ private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, obj
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067",
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.")]
private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type, DynamoDBFlatConfig flatConfig,
Dictionary<Type, string> derivedTypesDictionary, out Document output)
SimplePropertyStorage parentPropertyStorage, out Document output)
{
output = null;

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

output = new Document();
SimplePropertyStorage propertyStorage = new SimplePropertyStorage(valueType,derivedTypesDictionary);
SimplePropertyStorage propertyStorage = new SimplePropertyStorage(valueType, parentPropertyStorage);

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

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

Type elementType = Utils.GetElementType(type);

SimplePropertyStorage propertyStorage = new SimplePropertyStorage(elementType,derivedTypesDictionary);
SimplePropertyStorage propertyStorage = new SimplePropertyStorage(elementType, parentPropertyStoragey);
output = new DynamoDBList();
foreach (var item in enumerable)
{
Expand Down
47 changes: 33 additions & 14 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/InternalModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,39 @@ internal class SimplePropertyStorage
// Converter, if one is present
public IPropertyConverter Converter { get; protected set; }

public void AddDerivedType(string typeDiscriminator, Type type)
public void AddDerivedType(string typeDiscriminator, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type)
{
DerivedTypesDictionary[type] = typeDiscriminator;
DerivedTypeKeysDictionary[typeDiscriminator] = type;
_derivedTypesDictionary[type] = typeDiscriminator;
_derivedTypeKeysDictionary[typeDiscriminator] = type;
}

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

// derived type information used for polymorphic deserialization
public Dictionary<string, Type> DerivedTypeKeysDictionary { get; private set; }
private Dictionary<string, Type> _derivedTypeKeysDictionary;

public bool TryGetDerivedTypeDiscriminator([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type type, out string typeDiscriminator)
{
if (_derivedTypesDictionary.TryGetValue(type, out typeDiscriminator))
{
return true;
}

return false;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2067",
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.")]
public bool TryGetDerivedType(string typeDiscriminator, [DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] out Type deriviedType)
{
if (_derivedTypeKeysDictionary.TryGetValue(typeDiscriminator, out deriviedType))
{
return true;
}

return false;
}

[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2072",
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.")]
Expand All @@ -81,18 +103,15 @@ internal SimplePropertyStorage(MemberInfo member)
internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType)
{
MemberType = memberType;
DerivedTypesDictionary = new Dictionary<Type, string>();
DerivedTypeKeysDictionary = new Dictionary<string,Type>();
_derivedTypesDictionary = new Dictionary<Type, string>();
_derivedTypeKeysDictionary = new Dictionary<string,Type>();
}
internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, Dictionary<Type, string> derivedTypesDictionary)
{
MemberType = memberType;
DerivedTypesDictionary = derivedTypesDictionary;
}
internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, Dictionary<string, Type> derivedTypeKeysDictionary)

internal SimplePropertyStorage([DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] Type memberType, SimplePropertyStorage parentProeprtyStorage)
{
MemberType = memberType;
DerivedTypeKeysDictionary = derivedTypeKeysDictionary;
_derivedTypesDictionary = parentProeprtyStorage._derivedTypesDictionary;
_derivedTypeKeysDictionary = parentProeprtyStorage._derivedTypeKeysDictionary;
}

public override string ToString()
Expand Down