-
Notifications
You must be signed in to change notification settings - Fork 244
Make internal a trait and not just a tag #531
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
66 changes: 66 additions & 0 deletions
66
smithy-build/src/main/java/software/amazon/smithy/build/transforms/ExcludeShapesByTrait.java
This file contains hidden or 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,66 @@ | ||
| /* | ||
| * 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.build.transforms; | ||
|
|
||
| import software.amazon.smithy.build.TransformContext; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.loader.Prelude; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
|
|
||
| /** | ||
| * Removes shapes from the model if they are marked with a specific trait. | ||
| */ | ||
| public final class ExcludeShapesByTrait extends ConfigurableProjectionTransformer<ExcludeShapesByTrait.Config> { | ||
|
|
||
| public static final class Config { | ||
| private String trait; | ||
|
|
||
| /** | ||
| * Gets the shape ID of the trait to filter shapes by. | ||
| * | ||
| * @return Returns the trait shape ID. | ||
| */ | ||
| public String getTrait() { | ||
| return trait; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the shape ID of the trait to filter shapes by. | ||
| * | ||
| * @param trait The shape ID of the trait that if present causes a shape to be removed. | ||
| */ | ||
| public void setTrait(String trait) { | ||
| this.trait = trait; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "excludeShapesByTrait"; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<Config> getConfigType() { | ||
| return Config.class; | ||
| } | ||
|
|
||
| protected Model transformWithConfig(TransformContext context, Config config) { | ||
| // Default to smithy.api# if the given trait ID is relative. | ||
| ShapeId traitId = ShapeId.fromOptionalNamespace(Prelude.NAMESPACE, config.getTrait()); | ||
|
|
||
| return context.getTransformer().removeShapesIf(context.getModel(), shape -> shape.hasTrait(traitId)); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...d/src/main/resources/META-INF/services/software.amazon.smithy.build.ProjectionTransformer
This file contains hidden or 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
48 changes: 48 additions & 0 deletions
48
...build/src/test/java/software/amazon/smithy/build/transforms/ExcludeShapesByTraitTest.java
This file contains hidden or 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,48 @@ | ||
| package software.amazon.smithy.build.transforms; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.hasItem; | ||
| import static org.hamcrest.Matchers.not; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.MethodSource; | ||
| import software.amazon.smithy.build.TransformContext; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.node.Node; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
|
|
||
| public class ExcludeShapesByTraitTest { | ||
| @ParameterizedTest | ||
| @MethodSource("traitValues") | ||
| public void removesShapesByTrait(String trait) { | ||
| Model model = Model.assembler() | ||
| .addImport(getClass().getResource("internal-shapes.smithy")) | ||
| .assemble() | ||
| .unwrap(); | ||
| TransformContext context = TransformContext.builder() | ||
| .model(model) | ||
| .settings(Node.objectNode().withMember("trait", Node.from(trait))) | ||
| .build(); | ||
| Model result = new ExcludeShapesByTrait().transform(context); | ||
|
|
||
| // Structure removal also removes members. | ||
| assertThat(result.getShapeIds(), not(hasItem(ShapeId.from("smithy.example#InternalStructure")))); | ||
| assertThat(result.getShapeIds(), not(hasItem(ShapeId.from("smithy.example#InternalStructure$foo")))); | ||
|
|
||
| // Can remove specific members from structures. | ||
| assertThat(result.getShapeIds(), hasItem(ShapeId.from("smithy.example#ExternalStructure"))); | ||
| assertThat(result.getShapeIds(), hasItem(ShapeId.from("smithy.example#ExternalStructure$external"))); | ||
| assertThat(result.getShapeIds(), not(hasItem(ShapeId.from("smithy.example#ExternalStructure$internal")))); | ||
| } | ||
|
|
||
| public static List<String> traitValues() { | ||
| return Arrays.asList( | ||
| // Relative IDs are assumed to be in "smithy.api". | ||
| "internal", | ||
| // Absolute IDs are used as-is. | ||
| "smithy.api#internal" | ||
| ); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
...y-build/src/test/resources/software/amazon/smithy/build/transforms/internal-shapes.smithy
This file contains hidden or 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,13 @@ | ||
| namespace smithy.example | ||
|
|
||
| @internal | ||
| structure InternalStructure { | ||
| foo: String, | ||
| } | ||
|
|
||
| structure ExternalStructure { | ||
| @internal | ||
| internal: String, | ||
|
|
||
| external: String, | ||
| } |
42 changes: 42 additions & 0 deletions
42
smithy-model/src/main/java/software/amazon/smithy/model/traits/InternalTrait.java
This file contains hidden or 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,42 @@ | ||
| /* | ||
| * 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.node.Node; | ||
| import software.amazon.smithy.model.node.ObjectNode; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
|
|
||
| /** | ||
| * Shapes marked with the internal trait are meant only for internal use and | ||
| * must not be exposed to customers. | ||
| */ | ||
| public final class InternalTrait extends AnnotationTrait { | ||
| public static final ShapeId ID = ShapeId.from("smithy.api#internal"); | ||
|
|
||
| public InternalTrait(ObjectNode node) { | ||
| super(ID, node); | ||
| } | ||
|
|
||
| public InternalTrait() { | ||
| this(Node.objectNode()); | ||
| } | ||
|
|
||
| public static final class Provider extends AnnotationTrait.Provider<InternalTrait> { | ||
| public Provider() { | ||
| super(ID, InternalTrait::new); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.