-
Notifications
You must be signed in to change notification settings - Fork 911
DDB Enhanced adding ChainConverterProvider #1746
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
cenedhryn
merged 1 commit into
master
from
salande/enhanced-ddb-chained-converter-providers
Apr 1, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
70 changes: 70 additions & 0 deletions
70
...a/software/amazon/awssdk/enhanced/dynamodb/internal/converter/ChainConverterProvider.java
This file contains hidden or 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,70 @@ | ||
/* | ||
* 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.internal.converter; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider; | ||
import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; | ||
|
||
/** | ||
* A {@link AttributeConverterProvider} that allows multiple providers to be chained in a specified order | ||
* to act as a single composite provider. When searching for an attribute converter for a type, | ||
* the providers will be called in forward/ascending order, attempting to find a converter from the | ||
* first provider, then the second, and so on, until a match is found or the operation fails. | ||
*/ | ||
@SdkInternalApi | ||
public final class ChainConverterProvider implements AttributeConverterProvider { | ||
private final List<AttributeConverterProvider> providerChain; | ||
|
||
private ChainConverterProvider(List<AttributeConverterProvider> providers) { | ||
this.providerChain = new ArrayList<>(providers); | ||
} | ||
|
||
/** | ||
* Construct a new instance of {@link ChainConverterProvider}. | ||
* @param providers A list of {@link AttributeConverterProvider} to chain together. | ||
* @return A constructed {@link ChainConverterProvider} object. | ||
*/ | ||
public static ChainConverterProvider create(AttributeConverterProvider... providers) { | ||
return new ChainConverterProvider(Arrays.asList(providers)); | ||
} | ||
|
||
/** | ||
* Construct a new instance of {@link ChainConverterProvider}. | ||
* @param providers A list of {@link AttributeConverterProvider} to chain together. | ||
* @return A constructed {@link ChainConverterProvider} object. | ||
*/ | ||
public static ChainConverterProvider create(List<AttributeConverterProvider> providers) { | ||
return new ChainConverterProvider(providers); | ||
} | ||
|
||
public List<AttributeConverterProvider> chainedProviders() { | ||
return Collections.unmodifiableList(this.providerChain); | ||
} | ||
|
||
@Override | ||
public <T> AttributeConverter<T> converterFor(EnhancedType<T> enhancedType) { | ||
return this.providerChain.stream() | ||
.filter(provider -> provider.converterFor(enhancedType) != null) | ||
.map(p -> p.converterFor(enhancedType)) | ||
.findFirst().orElse(null); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...oftware/amazon/awssdk/enhanced/dynamodb/internal/converter/ConverterProviderResolver.java
This file contains hidden or 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,63 @@ | ||
/* | ||
* 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.internal.converter; | ||
|
||
import java.util.List; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverterProvider; | ||
import software.amazon.awssdk.enhanced.dynamodb.DefaultAttributeConverterProvider; | ||
|
||
/** | ||
* Static module to assist with the initialization of attribute converter providers for a StaticTableSchema. | ||
*/ | ||
@SdkInternalApi | ||
public final class ConverterProviderResolver { | ||
|
||
private static final AttributeConverterProvider DEFAULT_ATTRIBUTE_CONVERTER = | ||
DefaultAttributeConverterProvider.create(); | ||
|
||
private ConverterProviderResolver() { | ||
} | ||
|
||
/** | ||
* Static provider for the default attribute converters that are bundled with the DynamoDB Enhanced Client. | ||
* This provider will be used by default unless overridden in the static table schema builder or using bean | ||
* annotations. | ||
*/ | ||
public static AttributeConverterProvider defaultConverterProvider() { | ||
return DEFAULT_ATTRIBUTE_CONVERTER; | ||
} | ||
|
||
/** | ||
* Resolves a list of attribute converter providers into a single provider. If the list is a singleton, | ||
* it will just return that provider, otherwise it will combine them into a | ||
* {@link ChainConverterProvider} using the order provided in the list. | ||
* | ||
* @param providers A list of providers to be combined in strict order | ||
* @return A single provider that combines all the supplied providers or null if no providers were supplied | ||
*/ | ||
public static AttributeConverterProvider resolveProviders(List<AttributeConverterProvider> providers) { | ||
if (providers == null || providers.isEmpty()) { | ||
return null; | ||
} | ||
|
||
if (providers.size() == 1) { | ||
return providers.get(0); | ||
} | ||
|
||
return ChainConverterProvider.create(providers); | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should spruce up the javadoc a bit to celebrate this class' new status as 'public'. At a minimum mention (preferably with examples) how to use it in a StaticTableSchema builder and a BeanTableSchema annotation.