Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize deprecated trait validation #1162

Merged
merged 1 commit into from
Mar 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

import java.util.ArrayList;
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.traits.DeprecatedTrait;
import software.amazon.smithy.model.traits.TraitDefinition;
import software.amazon.smithy.model.validation.AbstractValidator;
import software.amazon.smithy.model.validation.ValidationEvent;

Expand All @@ -33,19 +35,20 @@ public final class DeprecatedTraitValidator extends AbstractValidator {
public List<ValidationEvent> validate(Model model) {
List<ValidationEvent> events = new ArrayList<>();

// Get all shapes marked as deprecated.
for (Shape trait : model.getShapesWithTrait(DeprecatedTrait.class)) {
// Get all shapes that use the shapes marked as deprecated as traits.
for (Shape shape : model.getShapesWithTrait(trait)) {
// Any match here must use the trait shape as an actual trait.
DeprecatedTrait deprecatedTrait = model.expectShape(trait.toShapeId())
.expectTrait(DeprecatedTrait.class);
String traitMessage = trait.toShapeId().toString();
if (deprecatedTrait.getMessage().isPresent()) {
traitMessage = traitMessage + ", " + deprecatedTrait.getMessage().get();
for (Shape trait : model.getShapesWithTrait(TraitDefinition.class)) {
if (trait.hasTrait(DeprecatedTrait.class)) {
Set<Shape> shapesWithTrait = model.getShapesWithTrait(trait);
if (!shapesWithTrait.isEmpty()) {
DeprecatedTrait deprecatedTrait = trait.expectTrait(DeprecatedTrait.class);
String traitMessage = trait.toShapeId().toString();
if (deprecatedTrait.getMessage().isPresent()) {
traitMessage = traitMessage + ", " + deprecatedTrait.getMessage().get();
}
for (Shape shape : shapesWithTrait) {
events.add(warning(shape, trait, format(
"This shape applies a trait that is deprecated: %s", traitMessage)));
}
}
events.add(warning(shape, trait, format(
"This shape applies a trait that is deprecated: %s", traitMessage)));
}
}

Expand Down