forked from smithy-lang/smithy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.10] Adds UnstableTrait and UnstableTraitValidator (smithy-lang#290)
- Loading branch information
1 parent
f6741ff
commit 19cb902
Showing
10 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
smithy-model/src/main/java/software/amazon/smithy/model/traits/UnstableTrait.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2020 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.traits; | ||
|
||
import software.amazon.smithy.model.SourceLocation; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
|
||
/** | ||
* Marks a shape as unstable. | ||
*/ | ||
public final class UnstableTrait extends BooleanTrait { | ||
public static final ShapeId ID = ShapeId.from("smithy.api#unstable"); | ||
|
||
public UnstableTrait(SourceLocation sourceLocation) { | ||
super(ID, sourceLocation); | ||
} | ||
|
||
public UnstableTrait() { | ||
this(SourceLocation.NONE); | ||
} | ||
|
||
public static final class Provider extends BooleanTrait.Provider<UnstableTrait> { | ||
public Provider() { | ||
super(ID, UnstableTrait::new); | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
.../main/java/software/amazon/smithy/model/validation/validators/UnstableTraitValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright 2020 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.validation.validators; | ||
|
||
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.UnstableTrait; | ||
import software.amazon.smithy.model.validation.AbstractValidator; | ||
import software.amazon.smithy.model.validation.ValidationEvent; | ||
|
||
/** | ||
* Emits a validation event if a model contains shapes that are bound to unstable traits. | ||
*/ | ||
public final class UnstableTraitValidator extends AbstractValidator { | ||
@Override | ||
public List<ValidationEvent> validate(Model model) { | ||
Set<ShapeId> unstableTraits = model.getTraitShapes().stream() | ||
.filter(trait -> trait.hasTrait(UnstableTrait.class)) | ||
.map(Shape::getId) | ||
.collect(Collectors.toSet()); | ||
|
||
return model.shapes() | ||
.flatMap(shape -> validateShape(shape, unstableTraits).stream()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private List<ValidationEvent> validateShape(Shape shape, Set<ShapeId> unstableTraits) { | ||
List<ValidationEvent> events = new ArrayList<>(); | ||
shape.getAllTraits().forEach((shapeId, trait) -> { | ||
if (!unstableTraits.contains(trait.toShapeId())) { | ||
return; | ||
} | ||
events.add(warning(shape, trait, format("This shape applies a trait that is unstable: %s", | ||
trait.toShapeId()))); | ||
}); | ||
return events; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
smithy-model/src/test/java/software/amazon/smithy/model/traits/UnstableTraitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2020 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.traits; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.Optional; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.smithy.model.node.Node; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
|
||
public class UnstableTraitTest { | ||
@Test | ||
public void loadsTrait() { | ||
TraitFactory provider = TraitFactory.createServiceFactory(); | ||
Optional<Trait> trait = provider.createTrait( | ||
ShapeId.from("smithy.api#unstable"), ShapeId.from("ns.qux#foo"), Node.from(true)); | ||
|
||
assertTrue(trait.isPresent()); | ||
assertThat(trait.get(), instanceOf(UnstableTrait.class)); | ||
assertThat(trait.get().toNode(), equalTo(Node.from(true))); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ources/software/amazon/smithy/model/errorfiles/validators/unstable-trait-validator.errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[WARNING] ns.foo#MyString: This shape applies a trait that is unstable: ns.foo#fooTrait | UnstableTrait |
31 changes: 31 additions & 0 deletions
31
...esources/software/amazon/smithy/model/errorfiles/validators/unstable-trait-validator.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"smithy": "0.5.0", | ||
"shapes": { | ||
"ns.foo#MyString": { | ||
"type": "string", | ||
"traits": { | ||
"ns.foo#fooTrait": { }, | ||
"ns.foo#barTrait": { } | ||
} | ||
}, | ||
"ns.foo#fooTrait": { | ||
"type": "structure", | ||
"members": { }, | ||
"traits": { | ||
"smithy.api#unstable": { }, | ||
"smithy.api#trait": { | ||
"selector": "*" | ||
} | ||
} | ||
}, | ||
"ns.foo#barTrait": { | ||
"type": "structure", | ||
"members": { }, | ||
"traits": { | ||
"smithy.api#trait": { | ||
"selector": "*" | ||
} | ||
} | ||
} | ||
} | ||
} |