Skip to content

Commit 6a99041

Browse files
committed
Attribute converters for Java types
1 parent 5971748 commit 6a99041

File tree

190 files changed

+11076
-1061
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+11076
-1061
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.enhanced.dynamodb;
17+
18+
import java.time.Instant;
19+
20+
import software.amazon.awssdk.annotations.SdkPublicApi;
21+
import software.amazon.awssdk.annotations.ThreadSafe;
22+
import software.amazon.awssdk.enhanced.dynamodb.TypeToken;
23+
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.InstantAsIntegerAttributeConverter;
24+
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.InstantAsStringAttributeConverter;
25+
import software.amazon.awssdk.enhanced.dynamodb.internal.converter.attribute.StringAttributeConverter;
26+
import software.amazon.awssdk.enhanced.dynamodb.mapper.AttributeValueType;
27+
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
28+
29+
/**
30+
* Converts between a specific Java type and an {@link AttributeValue}.
31+
*
32+
* <p>
33+
* Examples:
34+
* <ul>
35+
* <li>The {@link StringAttributeConverter} converts a {@link String} into a DynamoDB string
36+
* ({@link software.amazon.awssdk.services.dynamodb.model.AttributeValue#s()}).</li>
37+
* <li>The {@link InstantAsIntegerAttributeConverter} converts an {@link Instant} into a DynamoDB number
38+
* ({@link software.amazon.awssdk.services.dynamodb.model.AttributeValue#n()}).</li>
39+
* <li>The {@link InstantAsStringAttributeConverter} converts an {@link Instant} into a DynamoDB string
40+
* ({@link software.amazon.awssdk.services.dynamodb.model.AttributeValue#s()}).</li>
41+
* </ul>
42+
*/
43+
@SdkPublicApi
44+
@ThreadSafe
45+
public interface AttributeConverter<T> {
46+
/**
47+
* Convert the provided Java object into an {@link AttributeValue}. This will raise a {@link RuntimeException} if the
48+
* conversion fails, or the input is null.
49+
*
50+
* <p>
51+
* Example:
52+
* {@code
53+
* InstantAsStringAttributeConverter converter = InstantAsStringAttributeConverter.create();
54+
* assertEquals(converter.toAttributeValue(Instant.EPOCH),
55+
* ItemAttributeValue.fromString("1970-01-01T00:00:00Z"));
56+
* }
57+
*/
58+
AttributeValue transformFrom(T input);
59+
60+
/**
61+
* Convert the provided {@link AttributeValue} into a Java object. This will raise a {@link RuntimeException} if the
62+
* conversion fails, or the input is null.
63+
*
64+
* <p>
65+
* Example:
66+
* {@code
67+
* InstantAsStringAttributeConverter converter = InstantAsStringAttributeConverter.create();
68+
* assertEquals(converter.fromAttributeValue(ItemAttributeValue.fromString("1970-01-01T00:00:00Z")),
69+
* Instant.EPOCH);
70+
* }
71+
*/
72+
T transformTo(AttributeValue input);
73+
74+
/**
75+
* The type supported by this converter.
76+
*/
77+
TypeToken<T> type();
78+
79+
/**
80+
* The {@link AttributeValueType} that a converter stores and reads values
81+
* from DynamoDB via the {@link AttributeValue} class.
82+
*/
83+
AttributeValueType attributeValueType();
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.enhanced.dynamodb;
17+
18+
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.enhanced.dynamodb.internal.DefaultAttributeConverterProvider;
20+
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
21+
22+
/**
23+
* Interface for determining the {@link AttributeConverter} to use for
24+
* converting a given {@link TypeToken}.
25+
*/
26+
@SdkPublicApi
27+
public interface AttributeConverterProvider {
28+
29+
/**
30+
* Finds a {@link AttributeConverter} for converting an object with a type
31+
* specified by a {@link TypeToken} to a {@link AttributeValue} and back.
32+
*
33+
* @param typeToken The type of the object to be converted
34+
* @return {@link AttributeConverter} for converting the given type.
35+
*/
36+
<T> AttributeConverter<T> converterFor(TypeToken<T> typeToken);
37+
38+
/**
39+
* Returns a default implementation of AttributeConverterProvider with all
40+
* standard Java type converters included.
41+
*/
42+
static AttributeConverterProvider defaultProvider() {
43+
return DefaultAttributeConverterProvider.create();
44+
}
45+
}

0 commit comments

Comments
 (0)