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

software.amazon.smithy.model.transform.plugins.CleanBindings Maven / Gradle / Ivy

Go to download

This module provides the core implementation of loading, validating, traversing, mutating, and serializing a Smithy model.

There is a newer version: 1.54.0
Show newest version
/*
 * Copyright 2019 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.smithy.model.transform.plugins;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ResourceShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.model.transform.ModelTransformerPlugin;

/**
 * Removes operation bindings from resources and services when operations
 * are removed, and removes resource bindings from services and resources when
 * resources are removed.
 */
public final class CleanBindings implements ModelTransformerPlugin {
    @Override
    public Model onRemove(ModelTransformer transformer, Collection shapes, Model model) {
        Set removedResources = shapes.stream()
                .filter(Shape::isResourceShape)
                .map(Shape::getId)
                .collect(Collectors.toSet());
        Set removedOperations = shapes.stream()
                .filter(Shape::isOperationShape)
                .map(Shape::getId)
                .collect(Collectors.toSet());

        Set toReplace = getServicesToUpdate(model, removedResources, removedOperations);
        toReplace.addAll(getResourcesToUpdate(model, removedResources, removedOperations));
        return transformer.replaceShapes(model, toReplace);
    }

    private Set getServicesToUpdate(Model model, Set resources, Set operations) {
        return model.shapes(ServiceShape.class)
                .filter(service -> containsAny(service.getResources(), resources)
                                   || containsAny(service.getOperations(), operations))
                .map(service -> {
                    ServiceShape.Builder builder = service.toBuilder();
                    resources.forEach(builder::removeResource);
                    operations.forEach(builder::removeOperation);
                    return builder.build();
                })
                .collect(Collectors.toSet());
    }

    private Set getResourcesToUpdate(Model model, Set resources, Set operations) {
        return model.shapes(ResourceShape.class)
                .filter(resource -> containsAny(resource.getAllOperations(), operations)
                                    || containsAny(resource.getResources(), resources))
                .map(resource -> {
                    ResourceShape.Builder builder = resource.toBuilder();
                    resources.forEach(builder::removeResource);
                    operations.forEach(builder::removeFromAllOperationBindings);
                    return builder.build();
                })
                .collect(Collectors.toSet());
    }

    private boolean containsAny(Set haystack, Set needles) {
        Set container = new HashSet<>(haystack);
        container.retainAll(needles);
        return !container.isEmpty();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy