
software.amazon.smithy.model.transform.plugins.CleanResourceReferences Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of smithy-model Show documentation
Show all versions of smithy-model Show documentation
This module provides the core implementation of loading, validating, traversing, mutating, and serializing a Smithy model.
/*
* 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.List;
import java.util.Set;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.ReferencesTrait;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.model.transform.ModelTransformerPlugin;
/**
* Removes references to resources that are removed from
* {@link ReferencesTrait}s.
*/
public final class CleanResourceReferences implements ModelTransformerPlugin {
@Override
public Model onRemove(ModelTransformer transformer, Collection shapes, Model model) {
Set toReplace = new HashSet<>();
shapes.forEach(shape -> toReplace.addAll(getAffectedStructures(model, shape)));
return transformer.replaceShapes(model, toReplace);
}
private Set getAffectedStructures(Model model, Shape resource) {
Set result = new HashSet<>();
for (StructureShape struct : model.getStructureShapesWithTrait(ReferencesTrait.class)) {
// References can also be on strings, but we only care about structs.
ReferencesTrait trait = struct.expectTrait(ReferencesTrait.class);
// Get the reference to a particular shape.
List references = trait.getResourceReferences(resource.getId());
if (!references.isEmpty()) {
// If the trait contains a reference to the resource, then create a new version of the
// trait and shape that no longer reference the resource.
ReferencesTrait.Builder traitBuilder = trait.toBuilder();
traitBuilder.clearReferences();
for (ReferencesTrait.Reference ref : trait.getReferences()) {
if (!references.contains(ref)) {
traitBuilder.addReference(ref);
}
}
result.add(struct.toBuilder().addTrait(traitBuilder.build()).build());
}
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy