|
| 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.extensions.utility; |
| 17 | + |
| 18 | +import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.getNestedSchema; |
| 19 | +import static software.amazon.awssdk.enhanced.dynamodb.internal.operations.UpdateItemOperation.NESTED_OBJECT_UPDATE; |
| 20 | + |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import java.util.regex.Pattern; |
| 25 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 26 | +import software.amazon.awssdk.enhanced.dynamodb.TableSchema; |
| 27 | +import software.amazon.awssdk.services.dynamodb.model.AttributeValue; |
| 28 | + |
| 29 | +@SdkInternalApi |
| 30 | +public final class NestedRecordUtils { |
| 31 | + |
| 32 | + private static final Pattern NESTED_OBJECT_PATTERN = Pattern.compile(NESTED_OBJECT_UPDATE); |
| 33 | + |
| 34 | + private NestedRecordUtils() { |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Resolves and returns the {@link TableSchema} for the element type of a list attribute from the provided root schema. |
| 39 | + * <p> |
| 40 | + * This method is useful when dealing with lists of nested objects in a DynamoDB-enhanced table schema, |
| 41 | + * particularly in scenarios where the list is part of a flattened nested structure. |
| 42 | + * <p> |
| 43 | + * If the provided key contains the nested object delimiter (e.g., {@code _NESTED_ATTR_UPDATE_}), the method traverses |
| 44 | + * the nested hierarchy based on that path to locate the correct schema for the target attribute. |
| 45 | + * Otherwise, it directly resolves the list element type from the root schema using reflection. |
| 46 | + * |
| 47 | + * @param rootSchema The root {@link TableSchema} representing the top-level entity. |
| 48 | + * @param key The key representing the list attribute, either flat or nested (using a delimiter). |
| 49 | + * @return The {@link TableSchema} representing the list element type of the specified attribute. |
| 50 | + * @throws IllegalArgumentException If the list element class cannot be found via reflection. |
| 51 | + */ |
| 52 | + public static TableSchema<?> getTableSchemaForListElement(TableSchema<?> rootSchema, String key) { |
| 53 | + TableSchema<?> listElementSchema; |
| 54 | + try { |
| 55 | + if (!key.contains(NESTED_OBJECT_UPDATE)) { |
| 56 | + listElementSchema = TableSchema.fromClass( |
| 57 | + Class.forName(rootSchema.converterForAttribute(key).type().rawClassParameters().get(0).rawClass().getName())); |
| 58 | + } else { |
| 59 | + String[] parts = NESTED_OBJECT_PATTERN.split(key); |
| 60 | + TableSchema<?> currentSchema = rootSchema; |
| 61 | + |
| 62 | + for (int i = 0; i < parts.length - 1; i++) { |
| 63 | + Optional<? extends TableSchema<?>> nestedSchema = getNestedSchema(currentSchema, parts[i]); |
| 64 | + if (nestedSchema.isPresent()) { |
| 65 | + currentSchema = nestedSchema.get(); |
| 66 | + } |
| 67 | + } |
| 68 | + String attributeName = parts[parts.length - 1]; |
| 69 | + listElementSchema = TableSchema.fromClass( |
| 70 | + Class.forName(currentSchema.converterForAttribute(attributeName) |
| 71 | + .type().rawClassParameters().get(0).rawClass().getName())); |
| 72 | + } |
| 73 | + } catch (ClassNotFoundException e) { |
| 74 | + throw new IllegalArgumentException("Class not found for field name: " + key, e); |
| 75 | + } |
| 76 | + return listElementSchema; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Traverses the attribute keys representing flattened nested structures and resolves the corresponding |
| 81 | + * {@link TableSchema} for each nested path. |
| 82 | + * <p> |
| 83 | + * The method constructs a mapping between each unique nested path (represented as dot-delimited strings) |
| 84 | + * and the corresponding {@link TableSchema} object derived from the root schema. It supports resolving schemas |
| 85 | + * for arbitrarily deep nesting, using the {@code _NESTED_ATTR_UPDATE_} pattern as a path delimiter. |
| 86 | + * <p> |
| 87 | + * This is typically used in update or transformation flows where fields from nested objects are represented |
| 88 | + * as flattened keys in the attribute map (e.g., {@code parent_NESTED_ATTR_UPDATE_child}). |
| 89 | + * |
| 90 | + * @param attributesToSet A map of flattened attribute keys to values, where keys may represent paths to nested attributes. |
| 91 | + * @param rootSchema The root {@link TableSchema} of the top-level entity. |
| 92 | + * @return A map where the key is the nested path (e.g., {@code "parent.child"}) and the value is the {@link TableSchema} |
| 93 | + * corresponding to that level in the object hierarchy. |
| 94 | + */ |
| 95 | + public static Map<String, TableSchema<?>> resolveSchemasPerPath(Map<String, AttributeValue> attributesToSet, |
| 96 | + TableSchema<?> rootSchema) { |
| 97 | + Map<String, TableSchema<?>> schemaMap = new HashMap<>(); |
| 98 | + schemaMap.put("", rootSchema); |
| 99 | + |
| 100 | + for (String key : attributesToSet.keySet()) { |
| 101 | + String[] parts = NESTED_OBJECT_PATTERN.split(key); |
| 102 | + |
| 103 | + StringBuilder pathBuilder = new StringBuilder(); |
| 104 | + TableSchema<?> currentSchema = rootSchema; |
| 105 | + |
| 106 | + for (int i = 0; i < parts.length - 1; i++) { |
| 107 | + if (pathBuilder.length() > 0) { |
| 108 | + pathBuilder.append("."); |
| 109 | + } |
| 110 | + pathBuilder.append(parts[i]); |
| 111 | + |
| 112 | + String path = pathBuilder.toString(); |
| 113 | + |
| 114 | + if (!schemaMap.containsKey(path)) { |
| 115 | + Optional<? extends TableSchema<?>> nestedSchema = getNestedSchema(currentSchema, parts[i]); |
| 116 | + if (nestedSchema.isPresent()) { |
| 117 | + schemaMap.put(path, nestedSchema.get()); |
| 118 | + currentSchema = nestedSchema.get(); |
| 119 | + } |
| 120 | + } else { |
| 121 | + currentSchema = schemaMap.get(path); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + return schemaMap; |
| 126 | + } |
| 127 | + |
| 128 | + public static String reconstructCompositeKey(String path, String attributeName) { |
| 129 | + if (path == null || path.isEmpty()) { |
| 130 | + return attributeName; |
| 131 | + } |
| 132 | + return String.join(NESTED_OBJECT_UPDATE, path.split("\\.")) |
| 133 | + + NESTED_OBJECT_UPDATE + attributeName; |
| 134 | + } |
| 135 | +} |
0 commit comments