
software.amazon.smithy.build.transforms.ExcludeTraits Maven / Gradle / Ivy
Show all versions of smithy-build Show documentation
/*
* 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.build.transforms;
import java.util.Collections;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import software.amazon.smithy.build.TransformContext;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.TraitDefinition;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.utils.Pair;
/**
* {@code excludeTraits} removes trait definitions and traits from
* shapes when a trait name matches any of the values given in
* {@code traits}.
*
* Arguments that end with "#" exclude the traits of an entire
* namespace. Trait shape IDs that are relative are assumed to be
* part of the {@code smithy.api} prelude namespace.
*/
public final class ExcludeTraits extends BackwardCompatHelper {
private static final Logger LOGGER = Logger.getLogger(ExcludeTraits.class.getName());
/**
* {@code excludeTraits} configuration settings.
*/
public static final class Config {
private Set traits = Collections.emptySet();
/**
* Gets the list of trait shape IDs/namespaces to exclude.
*
* @return shape IDs to exclude.
*/
public Set getTraits() {
return traits;
}
/**
* Sets the list of trait shape IDs/namespaces to exclude.
*
* Relative shape IDs are considered traits in the prelude
* namespace, {@code smithy.api}. Strings ending in "#" are
* used to exclude traits from an entire namespace.
*
* @param traits Traits to exclude.
*/
public void setTraits(Set traits) {
this.traits = traits;
}
}
@Override
public Class getConfigType() {
return Config.class;
}
@Override
public String getName() {
return "excludeTraits";
}
@Override
String getBackwardCompatibleNameMapping() {
return "traits";
}
@Override
public Model transformWithConfig(TransformContext context, Config config) {
Pair, Set> namesAndNamespaces = TraitRemovalUtils.parseTraits(config.getTraits());
Set names = namesAndNamespaces.getLeft();
Set namespaces = namesAndNamespaces.getRight();
LOGGER.info(() -> "Excluding traits by ID " + names + " and namespaces " + namespaces);
Model model = context.getModel();
ModelTransformer transformer = context.getTransformer();
Set removeTraits = model.getShapesWithTrait(TraitDefinition.class).stream()
.filter(trait -> TraitRemovalUtils.matchesTraitDefinition(trait, names, namespaces))
.collect(Collectors.toSet());
if (!removeTraits.isEmpty()) {
LOGGER.info(() -> "Excluding traits: " + removeTraits);
}
return transformer.removeShapes(model, removeTraits);
}
}