47
47
import software .amazon .awssdk .protocols .jsoncore .JsonNode ;
48
48
import software .amazon .awssdk .protocols .jsoncore .JsonNodeParser ;
49
49
import software .amazon .awssdk .services .dynamodb .model .AttributeValue ;
50
+ import software .amazon .awssdk .utils .StringUtils ;
51
+ import software .amazon .awssdk .utils .Validate ;
50
52
51
53
/**
52
54
* Default implementation of {@link EnhancedDocument}. This class is used by SDK to create Enhanced Documents.
57
59
@ SdkInternalApi
58
60
public class DefaultEnhancedDocument implements EnhancedDocument {
59
61
60
- private static final DefaultAttributeConverterProvider DEFAULT_PROVIDER = DefaultAttributeConverterProvider .create ();
61
-
62
62
private static final JsonItemAttributeConverter JSON_ITEM_ATTRIBUTE_CONVERTER = JsonItemAttributeConverter .create ();
63
63
64
64
private final Map <String , AttributeValue > attributeValueMap ;
@@ -67,7 +67,7 @@ public class DefaultEnhancedDocument implements EnhancedDocument {
67
67
68
68
private DefaultEnhancedDocument (Map <String , AttributeValue > attributeValueMap ) {
69
69
this .attributeValueMap = attributeValueMap ;
70
- this .attributeConverterProviders = ChainConverterProvider .create (DEFAULT_PROVIDER );
70
+ this .attributeConverterProviders = ChainConverterProvider .create (DefaultAttributeConverterProvider . create () );
71
71
}
72
72
73
73
public DefaultEnhancedDocument (DefaultBuilder builder ) {
@@ -89,7 +89,8 @@ public Builder toBuilder() {
89
89
90
90
}
91
91
92
- public Map <String , AttributeValue > getAttributeValueMap () {
92
+ @ Override
93
+ public Map <String , AttributeValue > toAttributeValueMap () {
93
94
return attributeValueMap ;
94
95
}
95
96
@@ -358,93 +359,120 @@ public Builder add(String attributeName, Object value) {
358
359
359
360
private ChainConverterProvider providerFromBuildAndAppendDefault () {
360
361
List <AttributeConverterProvider > converterProviders = new ArrayList <>(attributeConverterProviders );
361
- converterProviders .add (DEFAULT_PROVIDER );
362
+ converterProviders .add (DefaultAttributeConverterProvider . create () );
362
363
ChainConverterProvider attributeConverterProvider = ChainConverterProvider .create (converterProviders );
363
364
return attributeConverterProvider ;
364
365
}
365
366
366
367
@ Override
367
368
public Builder addString (String attributeName , String value ) {
368
- attributeValueMap .put (attributeName , AttributeValue .fromS (value ));
369
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
370
+ if (!isNullValueAdded (attributeName , value )) {
371
+ attributeValueMap .put (attributeName , AttributeValue .fromS (value ));
372
+ }
369
373
return this ;
370
374
}
371
375
372
376
@ Override
373
377
public Builder addNumber (String attributeName , Number value ) {
374
- attributeValueMap .put (attributeName , AttributeValue .fromN (value != null ? String .valueOf (value ) : null ));
378
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
379
+ if (!isNullValueAdded (attributeName , value )) {
380
+ attributeValueMap .put (attributeName , AttributeValue .fromN (String .valueOf (value )));
381
+ }
375
382
return this ;
376
383
}
377
384
378
385
@ Override
379
386
public Builder addSdkBytes (String attributeName , SdkBytes value ) {
380
- attributeValueMap .put (attributeName , AttributeValue .fromB (value ));
387
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
388
+ if (!isNullValueAdded (attributeName , value )) {
389
+ attributeValueMap .put (attributeName , AttributeValue .fromB (value ));
390
+ }
381
391
return this ;
382
392
}
383
393
384
394
@ Override
385
395
public Builder addBoolean (String attributeName , boolean value ) {
386
- attributeValueMap .put (attributeName , AttributeValue .fromBool (value ));
396
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
397
+ if (!isNullValueAdded (attributeName , value )) {
398
+ attributeValueMap .put (attributeName , AttributeValue .fromBool (value ));
399
+ }
387
400
return this ;
388
401
}
389
402
390
403
@ Override
391
404
public Builder addNull (String attributeName ) {
405
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
392
406
attributeValueMap .put (attributeName , NULL_ATTRIBUTE_VALUE );
393
407
return this ;
394
408
}
395
409
396
410
@ Override
397
411
public Builder addStringSet (String attributeName , Set <String > values ) {
398
- attributeValueMap .put (attributeName , AttributeValue .fromSs (values .stream ().collect (Collectors .toList ())));
412
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
413
+ if (!isNullValueAdded (attributeName , values )) {
414
+ attributeValueMap .put (attributeName , AttributeValue .fromSs (values .stream ().collect (Collectors .toList ())));
415
+ }
399
416
return this ;
400
417
}
401
418
402
419
@ Override
403
420
public Builder addNumberSet (String attributeName , Set <Number > values ) {
404
- List <String > collect = values .stream ().map (value -> value .toString ()).collect (Collectors .toList ());
405
- attributeValueMap .put (attributeName , AttributeValue .fromNs (collect ));
421
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
422
+ if (!isNullValueAdded (attributeName , values )) {
423
+ List <String > collect = values .stream ().map (value -> value .toString ()).collect (Collectors .toList ());
424
+ attributeValueMap .put (attributeName , AttributeValue .fromNs (collect ));
425
+
426
+ }
406
427
return this ;
407
428
}
408
429
409
430
@ Override
410
431
public Builder addSdkBytesSet (String attributeName , Set <SdkBytes > values ) {
411
- attributeValueMap .put (attributeName , AttributeValue .fromBs (values .stream ().collect (Collectors .toList ())));
432
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
433
+ if (!isNullValueAdded (attributeName , values )) {
434
+ attributeValueMap .put (attributeName , AttributeValue .fromBs (values .stream ().collect (Collectors .toList ())));
435
+ }
412
436
return this ;
413
437
}
414
438
415
439
@ Override
416
440
public Builder addList (String attributeName , List <?> value ) {
417
- attributeValueMap .put (attributeName , convert (value , providerFromBuildAndAppendDefault ()));
441
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
442
+ if (!isNullValueAdded (attributeName , value )) {
443
+ attributeValueMap .put (attributeName , convert (value , providerFromBuildAndAppendDefault ()));
444
+ }
418
445
return this ;
419
446
}
420
447
421
448
@ Override
422
449
public Builder addMap (String attributeName , Map <String , ?> value ) {
423
- attributeValueMap .put (attributeName , convert (value , providerFromBuildAndAppendDefault ()));
450
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
451
+ if (!isNullValueAdded (attributeName , value )) {
452
+ attributeValueMap .put (attributeName , convert (value , providerFromBuildAndAppendDefault ()));
453
+ }
424
454
return this ;
425
455
}
426
456
427
457
@ Override
428
458
public Builder addJson (String attributeName , String json ) {
429
- JsonItemAttributeConverter jsonItemAttributeConverter = JsonItemAttributeConverter .create ();
430
- JsonNodeParser build = JsonNodeParser .builder ().build ();
431
- JsonNode jsonNode = build .parse (json );
432
- AttributeValue attributeValue = jsonItemAttributeConverter .transformFrom (jsonNode );
433
- attributeValueMap .put (attributeName , attributeValue );
459
+ Validate .isTrue (!StringUtils .isEmpty (attributeName ), "attributeName cannot empty or null" );
460
+ if (!isNullValueAdded (attributeName , json )) {
461
+ JsonItemAttributeConverter jsonItemAttributeConverter = JsonItemAttributeConverter .create ();
462
+ JsonNodeParser build = JsonNodeParser .builder ().build ();
463
+ JsonNode jsonNode = build .parse (json );
464
+ AttributeValue attributeValue = jsonItemAttributeConverter .transformFrom (jsonNode );
465
+ attributeValueMap .put (attributeName , attributeValue );
466
+ }
434
467
return this ;
435
468
}
436
469
437
470
@ Override
438
471
public Builder addEnhancedDocument (String attributeName , EnhancedDocument enhancedDocument ) {
439
- if ( enhancedDocument == null ) {
440
- attributeValueMap . put ( attributeName , NULL_ATTRIBUTE_VALUE );
441
- return this ;
472
+ Validate . isTrue (! StringUtils . isEmpty ( attributeName ), "attributeName cannot empty or null" );
473
+ if (! isNullValueAdded ( attributeName , enhancedDocument )) {
474
+ attributeValueMap . put ( attributeName , AttributeValue . fromM ( enhancedDocument . toAttributeValueMap ())) ;
442
475
}
443
- DefaultEnhancedDocument defaultEnhancedDocument =
444
- enhancedDocument instanceof DefaultEnhancedDocument
445
- ? (DefaultEnhancedDocument ) enhancedDocument
446
- : (DefaultEnhancedDocument ) enhancedDocument .toBuilder ().json (enhancedDocument .toJson ()).build ();
447
- attributeValueMap .put (attributeName , AttributeValue .fromM (defaultEnhancedDocument .attributeValueMap ));
448
476
return this ;
449
477
}
450
478
@@ -492,6 +520,14 @@ public DefaultBuilder attributeValueMap(Map<String, AttributeValue> attributeVal
492
520
this .attributeValueMap = attributeValueMap != null ? new LinkedHashMap <>(attributeValueMap ) : null ;
493
521
return this ;
494
522
}
523
+
524
+ private boolean isNullValueAdded (String attributeName , Object value ) {
525
+ if (value == null ) {
526
+ addNull (attributeName );
527
+ return true ;
528
+ }
529
+ return false ;
530
+ }
495
531
}
496
532
497
533
@ Override
0 commit comments