-
Notifications
You must be signed in to change notification settings - Fork 996
Support AutoGeneratedTimestamp annotations in nested objects #6546
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
Open
anasatirbasa
wants to merge
30
commits into
aws:master
Choose a base branch
from
anasatirbasa:feature/support-autoGeneratedTimestamp-and-updateBehavior-annotations-in-nested-objects
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
778d12b
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa d60f5dd
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
anasatirbasa 56d0d49
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa 471dc58
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
anasatirbasa 0f3b61a
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa af027ad
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa 480827d
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa 3412f52
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa cabe751
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa 63b6c82
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa 772e2f3
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa 55bcc06
Support AutoGeneratedTimestamp and UpdateBehavior annotations in nest…
anasatirbasa cf6c8f2
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
anasatirbasa e829dcb
Exclude nested update behavior functionality
anasatirbasa ab4c683
Added support for @DynamoDbAutoGeneratedTimestampAttribute on attribu…
anasatirbasa e1636a7
Added support for @DynamoDbAutoGeneratedTimestampAttribute on attribu…
anasatirbasa 1947ac6
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
anasatirbasa c9c0787
Added support for @DynamoDbAutoGeneratedTimestampAttribute on attribu…
anasatirbasa 7bdc3aa
Added support for @DynamoDbAutoGeneratedTimestampAttribute on attribu…
anasatirbasa 7cef75e
Added support for @DynamoDbAutoGeneratedTimestampAttribute on attribu…
anasatirbasa d8bc88b
Added support for @DynamoDbAutoGeneratedTimestampAttribute on attribu…
anasatirbasa ba342bf
Added support for @DynamoDbAutoGeneratedTimestampAttribute on attribu…
anasatirbasa 9b1ae1f
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
shetsa-amzn ca1b464
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
anasatirbasa 3141ee4
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
anasatirbasa 6348ef0
Fixed caching mechanism for nested schemas.
anasatirbasa f9eea6c
Merge remote-tracking branch 'origin/feature/support-autoGeneratedTim…
anasatirbasa 13c861a
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
anasatirbasa 7fc4441
fixed checkstyle
anasatirbasa 1c60743
Merge branch 'master' into feature/support-autoGeneratedTimestamp-and…
anasatirbasa 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
6 changes: 6 additions & 0 deletions
6
.changes/next-release/feature-AmazonDynamoDBEnhancedClient-96aff9e.json
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,6 @@ | ||
| { | ||
| "type": "feature", | ||
| "category": "Amazon DynamoDB Enhanced Client", | ||
| "contributor": "", | ||
| "description": "Added support for @DynamoDbAutoGeneratedTimestampAttribute on attributes within nested objects." | ||
| } |
244 changes: 229 additions & 15 deletions
244
...are/amazon/awssdk/enhanced/dynamodb/extensions/AutoGeneratedTimestampRecordExtension.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
263 changes: 263 additions & 0 deletions
263
...ftware/amazon/awssdk/enhanced/dynamodb/internal/extensions/utility/NestedRecordUtils.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,263 @@ | ||
| /* | ||
| * 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.extensions.utility; | ||
|
|
||
| import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.getNestedSchema; | ||
| import static software.amazon.awssdk.enhanced.dynamodb.internal.operations.UpdateItemOperation.NESTED_OBJECT_UPDATE; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.regex.Pattern; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter; | ||
| import software.amazon.awssdk.enhanced.dynamodb.EnhancedType; | ||
| import software.amazon.awssdk.enhanced.dynamodb.TableSchema; | ||
| import software.amazon.awssdk.services.dynamodb.model.AttributeValue; | ||
| import software.amazon.awssdk.utils.CollectionUtils; | ||
| import software.amazon.awssdk.utils.StringUtils; | ||
|
|
||
| @SdkInternalApi | ||
| public final class NestedRecordUtils { | ||
|
|
||
| private static final Pattern NESTED_OBJECT_PATTERN = Pattern.compile(NESTED_OBJECT_UPDATE); | ||
|
|
||
| private NestedRecordUtils() { | ||
| } | ||
|
|
||
| /** | ||
| * Resolves and returns the {@link TableSchema} for the element type of list attribute from the provided root schema. | ||
| * <p> | ||
| * This method is useful when dealing with lists of nested objects in a DynamoDB-enhanced table schema, particularly in | ||
| * scenarios where the list is part of a flattened nested structure. | ||
| * <p> | ||
| * If the provided key contains the nested object delimiter (e.g., {@code _NESTED_ATTR_UPDATE_}), the method traverses the | ||
| * nested hierarchy based on that path to locate the correct schema for the target attribute. Otherwise, it directly resolves | ||
| * the list element type from the root schema using reflection. | ||
| * | ||
| * @param rootSchema The root {@link TableSchema} representing the top-level entity. | ||
| * @param key The key representing the list attribute, either flat or nested (using a delimiter). | ||
| * @return The {@link TableSchema} representing the list element type of the specified attribute. | ||
| * @throws IllegalArgumentException If the list element class cannot be found via reflection. | ||
| */ | ||
| public static TableSchema<?> getTableSchemaForListElement(TableSchema<?> rootSchema, String key) { | ||
| return getTableSchemaForListElement(rootSchema, key, new HashMap<>()); | ||
| } | ||
|
|
||
| /** | ||
| * Same as {@link #getTableSchemaForListElement(TableSchema, String)} but allows callers to provide a shared per-operation | ||
| * cache for nested schema lookups. | ||
| */ | ||
| public static TableSchema<?> getTableSchemaForListElement( | ||
| TableSchema<?> rootSchema, | ||
| String key, | ||
| Map<SchemaLookupKey, Optional<? extends TableSchema<?>>> nestedSchemaCache) { | ||
| TableSchema<?> listElementSchema; | ||
|
|
||
| if (!key.contains(NESTED_OBJECT_UPDATE)) { | ||
| Optional<? extends TableSchema<?>> staticSchema = getNestedSchemaCached(nestedSchemaCache, rootSchema, key); | ||
| if (staticSchema.isPresent()) { | ||
| listElementSchema = staticSchema.get(); | ||
| } else { | ||
| AttributeConverter<?> converter = rootSchema.converterForAttribute(key); | ||
| if (converter == null) { | ||
| throw new IllegalArgumentException("No converter found for attribute: " + key); | ||
| } | ||
| List<EnhancedType<?>> rawClassParameters = converter.type().rawClassParameters(); | ||
| if (CollectionUtils.isNullOrEmpty(rawClassParameters)) { | ||
| throw new IllegalArgumentException("No type parameters found for list attribute: " + key); | ||
| } | ||
| listElementSchema = TableSchema.fromClass(rawClassParameters.get(0).rawClass()); | ||
| } | ||
| } else { | ||
| String[] parts = NESTED_OBJECT_PATTERN.split(key); | ||
| TableSchema<?> currentSchema = rootSchema; | ||
|
|
||
| for (int i = 0; i < parts.length - 1; i++) { | ||
| Optional<? extends TableSchema<?>> nestedSchema = | ||
| getNestedSchemaCached(nestedSchemaCache, currentSchema, parts[i]); | ||
| if (nestedSchema.isPresent()) { | ||
| currentSchema = nestedSchema.get(); | ||
| } | ||
| } | ||
|
|
||
| String attributeName = parts[parts.length - 1]; | ||
| Optional<? extends TableSchema<?>> nestedListSchema = | ||
| getNestedSchemaCached(nestedSchemaCache, currentSchema, attributeName); | ||
|
|
||
| listElementSchema = nestedListSchema.orElseThrow( | ||
| () -> new IllegalArgumentException("Unable to resolve schema for list element at: " + key)); | ||
| } | ||
|
|
||
| return listElementSchema; | ||
| } | ||
|
|
||
| /** | ||
| * Traverses the attribute keys representing flattened nested structures and resolves the corresponding {@link TableSchema} | ||
| * for each nested path. | ||
| * <p> | ||
| * The method constructs a mapping between each unique nested path (represented as dot-delimited strings) and the | ||
| * corresponding {@link TableSchema} object derived from the root schema. It supports resolving schemas for arbitrarily deep | ||
| * nesting, using the {@code _NESTED_ATTR_UPDATE_} pattern as a path delimiter. | ||
| * <p> | ||
| * This is typically used in update or transformation flows where fields from nested objects are represented as flattened keys | ||
| * in the attribute map (e.g., {@code parent_NESTED_ATTR_UPDATE_child}). | ||
| * | ||
| * @param attributesToSet A map of flattened attribute keys to values, where keys may represent paths to nested attributes. | ||
| * @param rootSchema The root {@link TableSchema} of the top-level entity. | ||
| * @return A map where the key is the nested path (e.g., {@code "parent.child"}) and the value is the {@link TableSchema} | ||
| * corresponding to that level in the object hierarchy. | ||
| */ | ||
| public static Map<String, TableSchema<?>> resolveSchemasPerPath(Map<String, AttributeValue> attributesToSet, | ||
| TableSchema<?> rootSchema) { | ||
| return resolveSchemasPerPath(attributesToSet, rootSchema, new HashMap<>()); | ||
| } | ||
|
|
||
| /** | ||
| * Same as {@link #resolveSchemasPerPath(Map, TableSchema)} but allows callers to provide a shared per-operation cache for | ||
| * nested schema lookups. | ||
| */ | ||
| public static Map<String, TableSchema<?>> resolveSchemasPerPath( | ||
| Map<String, AttributeValue> attributesToSet, | ||
| TableSchema<?> rootSchema, | ||
| Map<SchemaLookupKey, Optional<? extends TableSchema<?>>> nestedSchemaCache) { | ||
|
|
||
| Map<String, TableSchema<?>> schemaMap = new HashMap<>(); | ||
| schemaMap.put("", rootSchema); | ||
|
|
||
| for (String key : attributesToSet.keySet()) { | ||
| String[] parts = NESTED_OBJECT_PATTERN.split(key); | ||
|
|
||
| StringBuilder pathBuilder = new StringBuilder(); | ||
| TableSchema<?> currentSchema = rootSchema; | ||
|
|
||
| for (int i = 0; i < parts.length - 1; i++) { | ||
| if (pathBuilder.length() > 0) { | ||
| pathBuilder.append("."); | ||
| } | ||
| pathBuilder.append(parts[i]); | ||
|
|
||
| String path = pathBuilder.toString(); | ||
|
|
||
| if (!schemaMap.containsKey(path)) { | ||
| Optional<? extends TableSchema<?>> nestedSchema = | ||
| getNestedSchemaCached(nestedSchemaCache, currentSchema, parts[i]); | ||
|
|
||
| if (nestedSchema.isPresent()) { | ||
| TableSchema<?> resolved = nestedSchema.get(); | ||
| schemaMap.put(path, resolved); | ||
| currentSchema = resolved; | ||
| } | ||
| } else { | ||
| currentSchema = schemaMap.get(path); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return schemaMap; | ||
| } | ||
|
|
||
| /** | ||
| * Converts a dot-separated path to a composite key using nested object delimiters. Example: | ||
| * {@code reconstructCompositeKey("parent.child", "attr")} returns | ||
| * {@code "parent_NESTED_ATTR_UPDATE_child_NESTED_ATTR_UPDATE_attr"} | ||
| * | ||
| * @param path the dot-separated path; may be null or empty | ||
| * @param attributeName the attribute name to append; must not be null | ||
| * @return the composite key with nested object delimiters | ||
| */ | ||
| public static String reconstructCompositeKey(String path, String attributeName) { | ||
| if (attributeName == null) { | ||
| throw new IllegalArgumentException("Attribute name cannot be null"); | ||
| } | ||
|
|
||
| if (StringUtils.isEmpty(path)) { | ||
| return attributeName; | ||
| } | ||
|
|
||
| return String.join(NESTED_OBJECT_UPDATE, path.split("\\.")) | ||
| + NESTED_OBJECT_UPDATE + attributeName; | ||
| } | ||
|
|
||
| /** | ||
| * Cached wrapper around {@link software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils#getNestedSchema}. Cache | ||
| * key is based on (parent schema identity, attribute name). | ||
| */ | ||
| public static Optional<? extends TableSchema<?>> getNestedSchemaCached( | ||
| Map<SchemaLookupKey, Optional<? extends TableSchema<?>>> cache, | ||
| TableSchema<?> parentSchema, | ||
| String attributeName) { | ||
|
|
||
| SchemaLookupKey key = new SchemaLookupKey(parentSchema, attributeName); | ||
| return cache.computeIfAbsent(key, k -> getNestedSchema(parentSchema, attributeName)); | ||
| } | ||
|
|
||
| /** | ||
| * Cached wrapper for resolving list element schema, storing results (including null) in the provided cache. | ||
| * <p> | ||
| * Note: {@link #getTableSchemaForListElement(TableSchema, String, Map)} does not return null today, but this helper is used | ||
| * by callers that previously cached the list element schema separately, and it keeps the "cache null" behavior. | ||
| */ | ||
| public static TableSchema<?> getListElementSchemaCached( | ||
| Map<SchemaLookupKey, TableSchema<?>> cache, | ||
| TableSchema<?> parentSchema, | ||
| String attributeName) { | ||
|
|
||
| SchemaLookupKey key = new SchemaLookupKey(parentSchema, attributeName); | ||
|
|
||
| if (cache.containsKey(key)) { | ||
| return cache.get(key); | ||
| } | ||
|
|
||
| TableSchema<?> schema = getTableSchemaForListElement(parentSchema, attributeName, new HashMap<>()); | ||
|
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. This is probably a dumb question, but is there a reason not to use the cache here instead of creating a new HashMap (aside from type difference of Optional or not)? |
||
| cache.put(key, schema); | ||
| return schema; | ||
| } | ||
|
|
||
| /** | ||
| * Identity-based cache key for schema lookups: - compares TableSchema by identity (==) to avoid depending on its | ||
| * equals/hashCode semantics - compares attribute name by value | ||
| */ | ||
| public static final class SchemaLookupKey { | ||
| private final TableSchema<?> parentSchema; | ||
| private final String attributeName; | ||
| private final int hash; | ||
|
|
||
| public SchemaLookupKey(TableSchema<?> parentSchema, String attributeName) { | ||
| this.parentSchema = parentSchema; | ||
| this.attributeName = attributeName; | ||
| this.hash = 31 * System.identityHashCode(parentSchema) + attributeName.hashCode(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (!(o instanceof SchemaLookupKey)) { | ||
| return false; | ||
| } | ||
| SchemaLookupKey other = (SchemaLookupKey) o; | ||
| return this.parentSchema == other.parentSchema && this.attributeName.equals(other.attributeName); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return hash; | ||
| } | ||
| } | ||
| } | ||
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.
Why CharSequence instead of String here?