-
Notifications
You must be signed in to change notification settings - Fork 870
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The builder for EnhancedDocument should not rely on the order in whic…
…h attribute converters are added to it
- Loading branch information
Showing
8 changed files
with
671 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...dk/enhanced/dynamodb/converters/document/CustomAttributeForDocumentConverterProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.enhanced.dynamodb.converters.document; | ||
|
||
import java.util.Map; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeValueType; | ||
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; | ||
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.EnhancedAttributeValue; | ||
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.string.IntegerStringConverter; | ||
import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
import software.amazon.awssdk.utils.ImmutableMap; | ||
|
||
public class CustomAttributeForDocumentConverterProvider implements AttributeConverterProvider { | ||
private final Map<EnhancedType<?>, AttributeConverter<?>> converterCache = ImmutableMap.of( | ||
EnhancedType.of(CustomClassForDocumentAPI.class), new CustomClassForDocumentAttributeConverter(), | ||
EnhancedType.of(Integer.class), new CustomIntegerAttributeConverter() | ||
); | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public <T> AttributeConverter<T> converterFor(EnhancedType<T> enhancedType) { | ||
return (AttributeConverter<T>) converterCache.get(enhancedType); | ||
} | ||
|
||
public static CustomAttributeForDocumentConverterProvider create(){ | ||
return new CustomAttributeForDocumentConverterProvider(); | ||
} | ||
|
||
|
||
private static class CustomStringAttributeConverter implements AttributeConverter<String> { | ||
|
||
final static String DEFAULT_SUFFIX = "-custom"; | ||
|
||
@Override | ||
public AttributeValue transformFrom(String input) { | ||
return EnhancedAttributeValue.fromString(input + DEFAULT_SUFFIX).toAttributeValue(); | ||
} | ||
|
||
@Override | ||
public String transformTo(AttributeValue input) { | ||
return input.s(); | ||
} | ||
|
||
@Override | ||
public EnhancedType<String> type() { | ||
return EnhancedType.of(String.class); | ||
} | ||
|
||
@Override | ||
public AttributeValueType attributeValueType() { | ||
return AttributeValueType.S; | ||
} | ||
} | ||
|
||
private static class CustomIntegerAttributeCo implements AttributeConverter<Integer> { | ||
|
||
final static Integer DEFAULT_INCREMENT = 10; | ||
|
||
@Override | ||
public AttributeValue transformFrom(Integer input) { | ||
return EnhancedAttributeValue.fromNumber(IntegerStringConverter.create().toString(input + DEFAULT_INCREMENT)) | ||
.toAttributeValue(); | ||
} | ||
|
||
@Override | ||
public Integer transformTo(AttributeValue input) { | ||
return Integer.valueOf(input.n()); | ||
} | ||
|
||
@Override | ||
public EnhancedType<Integer> type() { | ||
return EnhancedType.of(Integer.class); | ||
} | ||
|
||
@Override | ||
public AttributeValueType attributeValueType() { | ||
return AttributeValueType.N; | ||
} | ||
} | ||
} |
Oops, something went wrong.