@@ -444,26 +444,52 @@ private void PopulateInstance(ItemStorage storage, object instance, DynamoDBFlat
444
444
{
445
445
foreach ( PropertyStorage propertyStorage in storageConfig . AllPropertyStorage )
446
446
{
447
+ if ( propertyStorage . IsFlattened ) continue ;
447
448
string attributeName = propertyStorage . AttributeName ;
448
-
449
- DynamoDBEntry entry ;
450
- if ( document . TryGetValue ( attributeName , out entry ) )
449
+ if ( propertyStorage . ShouldFlattenChildProperties )
451
450
{
452
- if ( ShouldSave ( entry , true ) )
451
+ //create instance of the flatten property
452
+ var targetType = propertyStorage . MemberType ;
453
+ object flattenedPropertyInstance = Utils . InstantiateConverter ( targetType , this ) ;
454
+
455
+ //populate the flatten properties
456
+ foreach ( var flattenPropertyStorage in propertyStorage . FlattenProperties )
453
457
{
454
- object value = FromDynamoDBEntry ( propertyStorage , entry , flatConfig ) ;
458
+ string flattenedAttributeName = flattenPropertyStorage . AttributeName ;
455
459
456
- if ( ! TrySetValue ( instance , propertyStorage . Member , value ) )
457
- {
458
- throw new InvalidOperationException ( "Unable to retrieve value from " + attributeName ) ;
459
- }
460
+ PopulateProperty ( storage , flatConfig , document , flattenedAttributeName , flattenPropertyStorage , flattenedPropertyInstance ) ;
461
+ }
462
+ if ( ! TrySetValue ( instance , propertyStorage . Member , flattenedPropertyInstance ) )
463
+ {
464
+ throw new InvalidOperationException ( "Unable to retrieve value from " + attributeName ) ;
460
465
}
461
-
462
- if ( propertyStorage . IsVersion )
463
- storage . CurrentVersion = entry as Primitive ;
464
466
}
467
+ else
468
+ {
469
+ PopulateProperty ( storage , flatConfig , document , attributeName , propertyStorage , instance ) ;
470
+ }
471
+ }
472
+ }
473
+ }
474
+
475
+ private void PopulateProperty ( ItemStorage storage , DynamoDBFlatConfig flatConfig , Document document ,
476
+ string attributeName , PropertyStorage propertyStorage , object instance )
477
+ {
478
+ DynamoDBEntry entry ;
479
+ if ( ! document . TryGetValue ( attributeName , out entry ) ) return ;
480
+
481
+ if ( ShouldSave ( entry , true ) )
482
+ {
483
+ object value = FromDynamoDBEntry ( propertyStorage , entry , flatConfig ) ;
484
+
485
+ if ( ! TrySetValue ( instance , propertyStorage . Member , value ) )
486
+ {
487
+ throw new InvalidOperationException ( "Unable to retrieve value from " + attributeName ) ;
465
488
}
466
489
}
490
+
491
+ if ( propertyStorage . IsVersion )
492
+ storage . CurrentVersion = entry as Primitive ;
467
493
}
468
494
469
495
/// <summary>
@@ -521,30 +547,46 @@ private void PopulateItemStorage(object toStore, ItemStorage storage, DynamoDBFl
521
547
if ( keysOnly && ! propertyStorage . IsHashKey && ! propertyStorage . IsRangeKey &&
522
548
! propertyStorage . IsVersion && ! propertyStorage . IsCounter ) continue ;
523
549
550
+ if ( propertyStorage . IsFlattened ) continue ;
551
+
524
552
string propertyName = propertyStorage . PropertyName ;
525
553
string attributeName = propertyStorage . AttributeName ;
526
554
527
555
object value ;
528
556
if ( TryGetValue ( toStore , propertyStorage . Member , out value ) )
529
557
{
530
- DynamoDBEntry dbe = ToDynamoDBEntry ( propertyStorage , value , flatConfig ) ;
558
+ DynamoDBEntry dbe = ToDynamoDBEntry ( propertyStorage , value , flatConfig , propertyStorage . ShouldFlattenChildProperties ) ;
531
559
532
560
if ( ShouldSave ( dbe , ignoreNullValues ) )
533
561
{
534
- Primitive dbePrimitive = dbe as Primitive ;
535
- if ( propertyStorage . IsHashKey || propertyStorage . IsRangeKey ||
536
- propertyStorage . IsVersion || propertyStorage . IsLSIRangeKey ||
537
- propertyStorage . IsCounter )
562
+
563
+ if ( propertyStorage . ShouldFlattenChildProperties )
538
564
{
539
- if ( dbe != null && dbePrimitive == null )
540
- throw new InvalidOperationException ( "Property " + propertyName +
541
- " is a hash key, range key, atomic counter or version property and must be Primitive" ) ;
542
- }
565
+ if ( dbe == null ) continue ;
543
566
544
- document [ attributeName ] = dbe ;
567
+ if ( dbe is not Document innerDocument ) continue ;
545
568
546
- if ( propertyStorage . IsVersion )
547
- storage . CurrentVersion = dbePrimitive ;
569
+ foreach ( var pair in innerDocument )
570
+ {
571
+ document [ pair . Key ] = pair . Value ;
572
+ }
573
+ }
574
+ else
575
+ {
576
+ Primitive dbePrimitive = dbe as Primitive ;
577
+ if ( propertyStorage . IsHashKey || propertyStorage . IsRangeKey ||
578
+ propertyStorage . IsVersion || propertyStorage . IsLSIRangeKey )
579
+ {
580
+ if ( dbe != null && dbePrimitive == null )
581
+ throw new InvalidOperationException ( "Property " + propertyName +
582
+ " is a hash key, range key or version property and must be Primitive" ) ;
583
+ }
584
+
585
+ document [ attributeName ] = dbe ;
586
+
587
+ if ( propertyStorage . IsVersion )
588
+ storage . CurrentVersion = dbePrimitive ;
589
+ }
548
590
}
549
591
}
550
592
else
@@ -724,7 +766,8 @@ internal DynamoDBEntry ToDynamoDBEntry(SimplePropertyStorage propertyStorage, ob
724
766
725
767
[ UnconditionalSuppressMessage ( "ReflectionAnalysis" , "IL2072" ,
726
768
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." ) ]
727
- private DynamoDBEntry ToDynamoDBEntry ( SimplePropertyStorage propertyStorage , object value , DynamoDBFlatConfig flatConfig , bool canReturnScalarInsteadOfList )
769
+ private DynamoDBEntry ToDynamoDBEntry ( SimplePropertyStorage propertyStorage , object value ,
770
+ DynamoDBFlatConfig flatConfig , bool canReturnScalarInsteadOfList )
728
771
{
729
772
if ( value == null )
730
773
return null ;
@@ -803,7 +846,7 @@ private bool TryToMap(object value, [DynamicallyAccessedMembers(InternalConstant
803
846
if ( item == null )
804
847
entry = DynamoDBNull . Null ;
805
848
else
806
- entry = ToDynamoDBEntry ( propertyStorage , item , flatConfig ) ;
849
+ entry = ToDynamoDBEntry ( propertyStorage , item , flatConfig , false ) ;
807
850
808
851
output [ key ] = entry ;
809
852
}
@@ -839,7 +882,7 @@ private bool TryToList(object value, [DynamicallyAccessedMembers(DynamicallyAcce
839
882
entry = DynamoDBNull . Null ;
840
883
else
841
884
{
842
- entry = ToDynamoDBEntry ( propertyStorage , item , flatConfig ) ;
885
+ entry = ToDynamoDBEntry ( propertyStorage , item , flatConfig , false ) ;
843
886
}
844
887
845
888
output . Add ( entry ) ;
@@ -1187,7 +1230,7 @@ private static List<QueryCondition> CreateQueryConditions(DynamoDBFlatConfig fla
1187
1230
// Key creation
1188
1231
private DynamoDBEntry ValueToDynamoDBEntry ( PropertyStorage propertyStorage , object value , DynamoDBFlatConfig flatConfig )
1189
1232
{
1190
- var entry = ToDynamoDBEntry ( propertyStorage , value , flatConfig ) ;
1233
+ var entry = ToDynamoDBEntry ( propertyStorage , value , flatConfig , false ) ;
1191
1234
return entry ;
1192
1235
}
1193
1236
private static void ValidateKey ( Key key , ItemStorageConfig storageConfig )
0 commit comments