-
Notifications
You must be signed in to change notification settings - Fork 1k
Add support for strategy-based UUID auto-generation (@DynamoDbAutoGeneratedUuid) #6373
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
base: master
Are you sure you want to change the base?
Changes from 33 commits
7cec4a2
8b80b18
d615605
da14ad5
031ef44
a47b1d7
0a29161
1bf7a3a
8c4826d
8a3048f
79ba90f
459fae8
8ae8405
08012f9
67cd161
a4bfb1e
fd877c7
bbd03ac
e654ef5
8c3d7d1
6d66b77
5bad65a
b935acf
45c4000
407703e
9a44ed2
dea26e0
defb044
8b057b5
68ca368
ae9e8ad
b72323f
0cdc2c2
9fe582f
9e890bb
65a14d0
3a95cab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "Amazon DynamoDB Enhanced Client", | ||
| "contributor": "", | ||
| "description": "Added strategy-based UUID generation to DynamoDbAutoGeneratedUuid with ALWAYS (default) and CREATE modes." | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * 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.extensions.annotations; | ||
|
|
||
| import software.amazon.awssdk.annotations.SdkPublicApi; | ||
|
|
||
| /** | ||
| * Strategy used to decide when auto-generated attribute values are produced. | ||
| * <p> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "attribute values" here is misleading, because its colliding with The rest is an embellishment with implementation details that we don't need. proposed: /**
* Strategy used to decide when a new value is generated for an annotated attribute
*/
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the Javadoc (same commit as above: #9fe582f) |
||
| * Currently used by {@link DynamoDbAutoGeneratedUuid}. This is the V2 analogue of the v1 SDK's | ||
| * {@code DynamoDBAutoGenerateStrategy}, which is shared by other annotations such as auto-generated timestamps. | ||
| */ | ||
| @SdkPublicApi | ||
| public enum DynamoDbAutoGenerateStrategy { | ||
| /** | ||
| * Generate a new value on every write operation. | ||
| */ | ||
| ALWAYS, | ||
|
|
||
| /** | ||
| * Generate a value only when the current value is missing. | ||
| * Missing means the value is absent from the write item map or is DynamoDB {@code NULL}. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should remove line 35 This explanation couples the enum to how the autogenerated UUID extension works. We might want to redefine what "missing" means when we implement other extensions that use this strategy enum.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @RanVaknin, updated this Javadoc as well (same commit as above: #9fe582f) |
||
| */ | ||
| CREATE | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,18 +19,32 @@ | |
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
| import java.util.UUID; | ||
| import software.amazon.awssdk.annotations.SdkPublicApi; | ||
| import software.amazon.awssdk.enhanced.dynamodb.internal.extensions.AutoGeneratedUuidTag; | ||
| import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.BeanTableSchemaAttributeTag; | ||
|
|
||
| /** | ||
| * Denotes this attribute as recording the auto generated UUID string for the record. Every time a record with this | ||
| * attribute is written to the database it will update the attribute with a {@link UUID#randomUUID} string. | ||
| * Denotes this attribute as recording the auto generated UUID string for the record. | ||
| * <p> | ||
| * The {@link #strategy()} controls whether UUID is generated on every write or only when missing. | ||
| * The default is {@link DynamoDbAutoGenerateStrategy#ALWAYS} for backward compatibility with existing | ||
| * {@code @DynamoDbAutoGeneratedUuid} usage. | ||
| * Use {@link DynamoDbAutoGenerateStrategy#CREATE} when you want to generate only if the value is missing | ||
| * (absent from the write item map or DynamoDB {@code NULL}). An empty string is treated as present and is preserved. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This adds unnecassary info about the implementation. The following is sufficient: * Use {@link DynamoDbAutoGenerateStrategy#CREATE} when you want to generate only if the value is missing
* (absent from the write item map or DynamoDB {@code NULL}).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I updated the Javadoc as you proposed (commit #9fe582f) |
||
| * <p> | ||
| * {@code CREATE} inspects the write item map after mapping, not the value stored in DynamoDB. | ||
| * With {@code updateItem} and {@code ignoreNulls(true)}, a null CREATE field is omitted from the map, so a new UUID | ||
| * is generated and silently overwrites any existing stored value. | ||
| */ | ||
| @SdkPublicApi | ||
| @Target(ElementType.METHOD) | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @BeanTableSchemaAttributeTag(AutoGeneratedUuidTag.class) | ||
| public @interface DynamoDbAutoGeneratedUuid { | ||
| } | ||
| /** | ||
| * Defines when a new UUID should be generated. | ||
| * <p> | ||
| * Defaults to {@link DynamoDbAutoGenerateStrategy#ALWAYS} to preserve backward compatibility. | ||
| */ | ||
| DynamoDbAutoGenerateStrategy strategy() default DynamoDbAutoGenerateStrategy.ALWAYS; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public final class UuidTestUtils { | ||
|
|
||
| private UuidTestUtils() { | ||
| } | ||
|
|
||
| public static boolean isValidUuid(String uuid) { | ||
| try { | ||
| UUID.fromString(uuid); | ||
| return true; | ||
| } catch (Exception e) { | ||
| return false; | ||
| } | ||
| } | ||
| } |
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.
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.
please dont apply suggestion through the github UI. This is meant to replace 124-128