All Downloads are FREE. Search and download functionalities are using the official Maven repository.

software.amazon.awssdk.enhanced.dynamodb.internal.mapper.ResolvedImmutableAttribute Maven / Gradle / Ivy

/*
 * 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.mapper;

import static software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.nullAttributeValue;
import static software.amazon.awssdk.enhanced.dynamodb.internal.EnhancedClientUtils.isNullAttributeValue;

import java.util.function.BiConsumer;
import java.util.function.Function;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.enhanced.dynamodb.AttributeConverter;
import software.amazon.awssdk.enhanced.dynamodb.mapper.ImmutableAttribute;
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableMetadata;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;

@SdkInternalApi
public final class ResolvedImmutableAttribute {
    private final String attributeName;
    private final Function getAttributeMethod;
    private final BiConsumer updateBuilderMethod;
    private final StaticTableMetadata tableMetadata;
    private final AttributeConverter attributeConverter;

    private ResolvedImmutableAttribute(String attributeName,
                                       Function getAttributeMethod,
                                       BiConsumer updateBuilderMethod,
                                       StaticTableMetadata tableMetadata,
                                       AttributeConverter attributeConverter) {
        this.attributeName = attributeName;
        this.getAttributeMethod = getAttributeMethod;
        this.updateBuilderMethod =  updateBuilderMethod;
        this.tableMetadata = tableMetadata;
        this.attributeConverter = attributeConverter;
    }

    public static  ResolvedImmutableAttribute create(ImmutableAttribute immutableAttribute,
                                                                    AttributeConverter attributeConverter) {

        AttributeType attributeType = StaticAttributeType.create(attributeConverter);
        Function getAttributeValueWithTransform = item -> {
            R value = immutableAttribute.getter().apply(item);
            return value == null ? nullAttributeValue() : attributeType.objectToAttributeValue(value);
        };

        // When setting a value on the java object, do not explicitly set nulls as this can cause an NPE to be thrown
        // if the target attribute type is a primitive.
        BiConsumer updateBuilderWithTransform =
            (builder, attributeValue) -> {
                // If the attributeValue is null, do not attempt to marshal
                if (isNullAttributeValue(attributeValue)) {
                    return;
                }

                R value = attributeType.attributeValueToObject(attributeValue);

                if (value != null) {
                    immutableAttribute.setter().accept(builder, value);
                }
            };

        StaticTableMetadata.Builder tableMetadataBuilder = StaticTableMetadata.builder();
        immutableAttribute.tags().forEach(tag -> {
            tag.validateType(immutableAttribute.name(), immutableAttribute.type(),
                             attributeType.attributeValueType());
            tag.modifyMetadata(immutableAttribute.name(), attributeType.attributeValueType()).accept(tableMetadataBuilder);
        });
        return new ResolvedImmutableAttribute<>(immutableAttribute.name(),
                                                getAttributeValueWithTransform,
                                                updateBuilderWithTransform,
                                                tableMetadataBuilder.build(),
                                                attributeConverter);
    }

    public  ResolvedImmutableAttribute transform(
        Function transformItem,
        Function transformBuilder) {

        return new ResolvedImmutableAttribute<>(
            attributeName,
            item -> {
                T otherItem = transformItem.apply(item);

                // If the containing object is null don't attempt to read attributes from it
                return otherItem == null ?
                    nullAttributeValue() : getAttributeMethod.apply(otherItem);
            },
            (item, value) -> updateBuilderMethod.accept(transformBuilder.apply(item), value),
            tableMetadata, attributeConverter);
    }

    public String attributeName() {
        return attributeName;
    }

    public Function attributeGetterMethod() {
        return getAttributeMethod;
    }

    public BiConsumer updateItemMethod() {
        return updateBuilderMethod;
    }

    public StaticTableMetadata tableMetadata() {
        return tableMetadata;
    }

    public AttributeConverter attributeConverter() {
        return attributeConverter;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy