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

Emit warnings for deprecated traits #279

Merged
merged 5 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Emit on shapes with deprecated traits
  • Loading branch information
Chase Coalwell committed Feb 19, 2020
commit ea47f366e419ad8f3f9a8821923e8ac47a866ea4
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) {
srchase marked this conversation as resolved.
Show resolved Hide resolved

return model.shapes()
Set<ShapeId> deprecatedTraits = model.shapes()
srchase marked this conversation as resolved.
Show resolved Hide resolved
.filter(shape -> shape.hasTrait(TraitDefinition.class))
.filter(trait -> trait.getTrait(DeprecatedTrait.class).isPresent())
srchase marked this conversation as resolved.
Show resolved Hide resolved
.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))
srchase marked this conversation as resolved.
Show resolved Hide resolved
.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()))
srchase marked this conversation as resolved.
Show resolved Hide resolved
.map(boundDeprecatedTrait -> warning(shape, format("Shape `%s` uses deprecated trait: %s",
shape.getId().getName(), boundDeprecatedTrait.getName())))
srchase marked this conversation as resolved.
Show resolved Hide resolved
.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": [
srchase marked this conversation as resolved.
Show resolved Hide resolved
{
"id": "Deprecated Traits",
"name": "DeprecatedTraits"
srchase marked this conversation as resolved.
Show resolved Hide resolved
}
]
Expand Down