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

software.amazon.smithy.diff.evaluators.ServiceRename Maven / Gradle / Ivy

Go to download

This module detects differences between two Smithy models, identifying changes that are safe and changes that are backward incompatible.

There is a newer version: 1.53.0
Show newest version
/*
 * Copyright 2021 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.diff.evaluators;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import software.amazon.smithy.diff.Differences;
import software.amazon.smithy.model.neighbor.Walker;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.validation.ValidationEvent;

/**
 * Creates an ERROR event when a shape is renamed that was already
 * part of the service, when a rename changes for a shape, or when
 * a rename is removed for a shape.
 */
public final class ServiceRename extends AbstractDiffEvaluator {
    @Override
    public List evaluate(Differences differences) {
        Walker oldWalker = new Walker(differences.getOldModel());

        return differences.changedShapes(ServiceShape.class)
                .flatMap(diff -> {
                    ServiceShape oldShape = diff.getOldShape();
                    ServiceShape newShape = diff.getNewShape();
                    if (oldShape.getRename().equals(newShape.getRename())) {
                        return Stream.empty();
                    }

                    // Look for the removal or changing of old renames.
                    List events = new ArrayList<>();
                    for (Map.Entry old : oldShape.getRename().entrySet()) {
                        String newValue = newShape.getRename().get(old.getKey());
                        if (newValue == null) {
                            events.add(error(newShape, String.format(
                                    "Service rename of `%s` to `%s` was removed",
                                    old.getKey(), old.getValue())));
                        } else if (!old.getValue().equals(newValue)) {
                            events.add(error(newShape, String.format(
                                    "Service rename of `%s` was changed from `%s` to `%s`",
                                    old.getKey(), old.getValue(), newValue)));
                        }
                    }

                    // Look for the addition of new renames to shapes already in the closure.
                    Set oldClosure = oldWalker.walkShapeIds(oldShape);
                    for (Map.Entry newEntry : newShape.getRename().entrySet()) {
                        if (!oldShape.getRename().containsKey(newEntry.getKey())) {
                            if (oldClosure.contains(newEntry.getKey())) {
                                events.add(error(newShape, String.format(
                                        "Service rename of `%s` to `%s` was added to an old shape",
                                        newEntry.getKey(), newEntry.getValue())));
                            }
                        }
                    }

                    return events.stream();
                })
                .collect(Collectors.toList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy