Skip to content

Commit

Permalink
Emit on shapes with deprecated traits
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell committed Feb 19, 2020
1 parent 9a23747 commit ea47f36
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,21 @@

import static java.lang.String.format;

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

/**
* Emits a validation event if a model contains a trait that has been marked as deprecated.
* Emits a validation event if a model contains shapes that are bound to deprecated traits.
*/
public final class DeprecatedTraitsValidator extends AbstractValidator {

Expand All @@ -39,12 +43,24 @@ public Provider() {

@Override
public List<ValidationEvent> validate(Model model) {

return model.shapes()
Set<ShapeId> deprecatedTraits = model.shapes()
.filter(shape -> shape.hasTrait(TraitDefinition.class))
.filter(trait -> trait.getTrait(DeprecatedTrait.class).isPresent())
.map(deprecatedTrait -> warning(deprecatedTrait, format("The %s trait is deprecated.",
deprecatedTrait.getId().getName())))
.map(Shape::getId)
.collect(Collectors.toSet());

return model.shapes()
.filter(shape -> !shape.hasTrait(TraitDefinition.class))
.flatMap(nonTraitShape -> validateShape(nonTraitShape, deprecatedTraits).stream())
.collect(Collectors.toList());
}

private List<ValidationEvent> validateShape(Shape shape, Set<ShapeId> deprecatedTraits) {
List<ValidationEvent> events = new ArrayList<>();
shape.getAllTraits().keySet().stream().filter(trait -> deprecatedTraits.contains(trait.toShapeId()))
.map(boundDeprecatedTrait -> warning(shape, format("Shape `%s` uses deprecated trait: %s",
shape.getId().getName(), boundDeprecatedTrait.getName())))
.forEach(events::add);
return events;
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
[WARNING] ns.foo#myTraitName: The myTraitName trait is deprecated. | Deprecated Traits
[WARNING] ns.foo#MyString: Shape `MyString` uses deprecated trait: fooTrait | DeprecatedTraits
[WARNING] ns.foo#MyString: Shape `MyString` uses deprecated trait: barTrait | DeprecatedTraits
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"ns.foo#MyString": {
"type": "string",
"traits": {
"ns.foo#myTraitName": { }
"ns.foo#fooTrait": { },
"ns.foo#barTrait": { },
"ns.foo#bazTrait": { }
}
},
"ns.foo#myTraitName": {
"ns.foo#fooTrait": {
"type": "structure",
"members": { },
"traits": {
Expand All @@ -16,12 +18,30 @@
"selector": "*"
}
}
},
"ns.foo#barTrait": {
"type": "structure",
"members": { },
"traits": {
"smithy.api#deprecated": { },
"smithy.api#trait": {
"selector": "*"
}
}
},
"ns.foo#bazTrait": {
"type": "structure",
"members": { },
"traits": {
"smithy.api#trait": {
"selector": "*"
}
}
}
},
"metadata": {
"validators": [
{
"id": "Deprecated Traits",
"name": "DeprecatedTraits"
}
]
Expand Down

0 comments on commit ea47f36

Please sign in to comment.