|
| 1 | +/* |
| 2 | + * Copyright 2020 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.smithy.model.transform.plugins; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import java.util.stream.Stream; |
| 23 | +import software.amazon.smithy.model.Model; |
| 24 | +import software.amazon.smithy.model.shapes.Shape; |
| 25 | +import software.amazon.smithy.model.shapes.ShapeId; |
| 26 | +import software.amazon.smithy.model.shapes.StructureShape; |
| 27 | +import software.amazon.smithy.model.traits.AuthDefinitionTrait; |
| 28 | +import software.amazon.smithy.model.traits.ProtocolDefinitionTrait; |
| 29 | +import software.amazon.smithy.model.traits.Trait; |
| 30 | +import software.amazon.smithy.model.transform.ModelTransformer; |
| 31 | +import software.amazon.smithy.model.transform.ModelTransformerPlugin; |
| 32 | + |
| 33 | +/** |
| 34 | + * Removes traits from {@link AuthDefinitionTrait} and |
| 35 | + * {@link ProtocolDefinitionTrait} traits that refer to removed shapes. |
| 36 | + */ |
| 37 | +public final class CleanTraitDefinitions implements ModelTransformerPlugin { |
| 38 | + @Override |
| 39 | + public Model onRemove(ModelTransformer transformer, Collection<Shape> removed, Model model) { |
| 40 | + Set<ShapeId> removedShapeIds = removed.stream().map(Shape::getId).collect(Collectors.toSet()); |
| 41 | + model = transformer.replaceShapes(model, getAuthDefShapesToReplace(model, removedShapeIds)); |
| 42 | + |
| 43 | + return transformer.replaceShapes(model, getProtocolDefShapesToReplace(model, removedShapeIds)); |
| 44 | + } |
| 45 | + |
| 46 | + private Set<Shape> getAuthDefShapesToReplace(Model model, Set<ShapeId> removedShapeIds) { |
| 47 | + return model.shapes(StructureShape.class) |
| 48 | + .flatMap(s -> Trait.flatMapStream(s, AuthDefinitionTrait.class)) |
| 49 | + .flatMap(pair -> { |
| 50 | + AuthDefinitionTrait authDefTrait = pair.getRight(); |
| 51 | + List<ShapeId> traits = authDefTrait.getTraits(); |
| 52 | + List<ShapeId> newTraits = excludeTraitsInSet(traits, removedShapeIds); |
| 53 | + |
| 54 | + // Return early if re-built list of traits is the same as existing list. |
| 55 | + if (traits.equals(newTraits)) { |
| 56 | + return Stream.empty(); |
| 57 | + } |
| 58 | + |
| 59 | + // If the list of traits on the AuthDefinitionTrait has changed due to a trait shape being |
| 60 | + // removed from the model, return a new version of the shape with a new version of the trait. |
| 61 | + return Stream.of(pair.getLeft().toBuilder().addTrait(authDefTrait.toBuilder() |
| 62 | + .traits(newTraits).build()).build()); |
| 63 | + }) |
| 64 | + .collect(Collectors.toSet()); |
| 65 | + } |
| 66 | + |
| 67 | + private Set<Shape> getProtocolDefShapesToReplace(Model model, Set<ShapeId> removedShapeIds) { |
| 68 | + return model.shapes(StructureShape.class) |
| 69 | + .flatMap(s -> Trait.flatMapStream(s, ProtocolDefinitionTrait.class)) |
| 70 | + .flatMap(pair -> { |
| 71 | + ProtocolDefinitionTrait protocolDefinitionTrait = pair.getRight(); |
| 72 | + List<ShapeId> traits = protocolDefinitionTrait.getTraits(); |
| 73 | + List<ShapeId> newTraits = excludeTraitsInSet(traits, removedShapeIds); |
| 74 | + |
| 75 | + // Return early if re-built list of traits is the same as existing list. |
| 76 | + if (traits.equals(newTraits)) { |
| 77 | + return Stream.empty(); |
| 78 | + } |
| 79 | + |
| 80 | + // If the list of traits on the ProtocolDefinitionTrait has changed due to a trait shape |
| 81 | + // being removed from the model, return a new version of the shape with a new version of |
| 82 | + // the trait. |
| 83 | + return Stream.of(pair.getLeft().toBuilder().addTrait(protocolDefinitionTrait.toBuilder() |
| 84 | + .traits(newTraits).build()).build()); |
| 85 | + }) |
| 86 | + .collect(Collectors.toSet()); |
| 87 | + } |
| 88 | + |
| 89 | + private List<ShapeId> excludeTraitsInSet(List<ShapeId> traits, Set<ShapeId> shapeIds) { |
| 90 | + return traits.stream() |
| 91 | + .filter(trait -> !shapeIds.contains(trait)) |
| 92 | + .collect(Collectors.toList()); |
| 93 | + } |
| 94 | +} |
0 commit comments