Skip to content

Implement DynamoDbFlatten annotation #3833

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 5, 2025
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/89c824fe-c511-4f08-b98f-95619c4d19a9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"services": [
{
"serviceName": "DynamoDBv2",
"type": "patch",
"changeLogMessages": [
"Introduce support for the [DynamoDbFlatten] attribute in the DynamoDB Object Persistence Model`"
]
}
]
}
29 changes: 29 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ public DynamoDBPolymorphicTypeAttribute(string typeDiscriminator,
}
}

/// <summary>
/// Indicates that the properties of the decorated field or property type should be "flattened"
/// into the parent object's attribute structure in DynamoDB. When applied, all public properties
/// of the referenced type are serialized as individual top-level attributes of the parent item,
/// rather than as a nested object or map.
/// <para>
/// Example:
/// <code>
/// public class Address
/// {
/// public string Street { get; set; }
/// public string City { get; set; }
/// }
///
/// public class Person
/// {
/// public string Name { get; set; }
/// [DynamoDBFlatten]
/// public Address Address { get; set; }
/// }
/// </code>
/// In this example, the <c>Person</c> table will have top-level attributes for <c>Name</c>, <c>Street</c>, and <c>City</c>.
/// </para>
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class DynamoDBFlattenAttribute : DynamoDBAttribute
{
}

/// <summary>
/// DynamoDB attribute that directs the specified attribute not to
/// be included when saving or loading objects.
Expand Down
99 changes: 71 additions & 28 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,26 +444,52 @@ private void PopulateInstance(ItemStorage storage, object instance, DynamoDBFlat
{
foreach (PropertyStorage propertyStorage in storageConfig.AllPropertyStorage)
{
if(propertyStorage.IsFlattened) continue;
string attributeName = propertyStorage.AttributeName;

DynamoDBEntry entry;
if (document.TryGetValue(attributeName, out entry))
if (propertyStorage.ShouldFlattenChildProperties)
{
if (ShouldSave(entry, true))
//create instance of the flatten property
var targetType = propertyStorage.MemberType;
object flattenedPropertyInstance = Utils.InstantiateConverter(targetType, this);

//populate the flatten properties
foreach (var flattenPropertyStorage in propertyStorage.FlattenProperties)
{
object value = FromDynamoDBEntry(propertyStorage, entry, flatConfig);
string flattenedAttributeName = flattenPropertyStorage.AttributeName;

if (!TrySetValue(instance, propertyStorage.Member, value))
{
throw new InvalidOperationException("Unable to retrieve value from " + attributeName);
}
PopulateProperty(storage, flatConfig, document, flattenedAttributeName, flattenPropertyStorage, flattenedPropertyInstance);
}
if (!TrySetValue(instance, propertyStorage.Member, flattenedPropertyInstance))
{
throw new InvalidOperationException("Unable to retrieve value from " + attributeName);
}

if (propertyStorage.IsVersion)
storage.CurrentVersion = entry as Primitive;
}
else
{
PopulateProperty(storage, flatConfig, document, attributeName, propertyStorage, instance);
}
}
}
}

private void PopulateProperty(ItemStorage storage, DynamoDBFlatConfig flatConfig, Document document,
string attributeName, PropertyStorage propertyStorage, object instance)
{
DynamoDBEntry entry;
if (!document.TryGetValue(attributeName, out entry)) return;

if (ShouldSave(entry, true))
{
object value = FromDynamoDBEntry(propertyStorage, entry, flatConfig);

if (!TrySetValue(instance, propertyStorage.Member, value))
{
throw new InvalidOperationException("Unable to retrieve value from " + attributeName);
}
}

if (propertyStorage.IsVersion)
storage.CurrentVersion = entry as Primitive;
}

/// <summary>
Expand Down Expand Up @@ -521,30 +547,46 @@ private void PopulateItemStorage(object toStore, ItemStorage storage, DynamoDBFl
if (keysOnly && !propertyStorage.IsHashKey && !propertyStorage.IsRangeKey &&
!propertyStorage.IsVersion && !propertyStorage.IsCounter) continue;

if (propertyStorage.IsFlattened) continue;

string propertyName = propertyStorage.PropertyName;
string attributeName = propertyStorage.AttributeName;

object value;
if (TryGetValue(toStore, propertyStorage.Member, out value))
{
DynamoDBEntry dbe = ToDynamoDBEntry(propertyStorage, value, flatConfig);
DynamoDBEntry dbe = ToDynamoDBEntry(propertyStorage, value, flatConfig, propertyStorage.ShouldFlattenChildProperties);

if (ShouldSave(dbe, ignoreNullValues))
{
Primitive dbePrimitive = dbe as Primitive;
if (propertyStorage.IsHashKey || propertyStorage.IsRangeKey ||
propertyStorage.IsVersion || propertyStorage.IsLSIRangeKey ||
propertyStorage.IsCounter)

if (propertyStorage.ShouldFlattenChildProperties)
{
if (dbe != null && dbePrimitive == null)
throw new InvalidOperationException("Property " + propertyName +
" is a hash key, range key, atomic counter or version property and must be Primitive");
}
if (dbe == null) continue;

document[attributeName] = dbe;
if (dbe is not Document innerDocument) continue;

if (propertyStorage.IsVersion)
storage.CurrentVersion = dbePrimitive;
foreach (var pair in innerDocument)
{
document[pair.Key] = pair.Value;
}
}
else
{
Primitive dbePrimitive = dbe as Primitive;
if (propertyStorage.IsHashKey || propertyStorage.IsRangeKey ||
propertyStorage.IsVersion || propertyStorage.IsLSIRangeKey)
{
if (dbe != null && dbePrimitive == null)
throw new InvalidOperationException("Property " + propertyName +
" is a hash key, range key or version property and must be Primitive");
}

document[attributeName] = dbe;

if (propertyStorage.IsVersion)
storage.CurrentVersion = dbePrimitive;
}
}
}
else
Expand Down Expand Up @@ -724,7 +766,8 @@ internal DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, ob

[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)
private DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, object value,
DynamoDBFlatConfig flatConfig, bool canReturnScalarInsteadOfList)
{
if (value == null)
return null;
Expand Down Expand Up @@ -803,7 +846,7 @@ private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstant
if (item == null)
entry = DynamoDBNull.Null;
else
entry = ToDynamoDBEntry(propertyStorage, item, flatConfig);
entry = ToDynamoDBEntry(propertyStorage, item, flatConfig, false);

output[key] = entry;
}
Expand Down Expand Up @@ -839,7 +882,7 @@ private bool TryToList(object value, [DynamicallyAccessedMembers(DynamicallyAcce
entry = DynamoDBNull.Null;
else
{
entry = ToDynamoDBEntry(propertyStorage, item, flatConfig);
entry = ToDynamoDBEntry(propertyStorage, item, flatConfig, false);
}

output.Add(entry);
Expand Down Expand Up @@ -1187,7 +1230,7 @@ private static List<QueryCondition> CreateQueryConditions(DynamoDBFlatConfig fla
// Key creation
private DynamoDBEntry ValueToDynamoDBEntry(PropertyStorage propertyStorage, object value, DynamoDBFlatConfig flatConfig)
{
var entry = ToDynamoDBEntry(propertyStorage, value, flatConfig);
var entry = ToDynamoDBEntry(propertyStorage, value, flatConfig, false);
return entry;
}
private static void ValidateKey(Key key, ItemStorageConfig storageConfig)
Expand Down
Loading