-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add smithy-build transform for deprecated shapes
- Loading branch information
Showing
5 changed files
with
217 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
79 changes: 79 additions & 0 deletions
79
...y-build/src/main/java/software/amazon/smithy/build/transforms/RemoveDeprecatedShapes.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,79 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.build.transforms; | ||
|
||
import software.amazon.smithy.build.TransformContext; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.transform.ModelTransformer; | ||
|
||
/** | ||
* {@code removeDeprecatedShapes} removes shapes from a model if they have been deprecated before a version or date. | ||
*/ | ||
public final class RemoveDeprecatedShapes extends ConfigurableProjectionTransformer<RemoveDeprecatedShapes.Config> { | ||
|
||
/** | ||
* {@code RemoveDeprecatedShapes} configuration settings. | ||
*/ | ||
public static final class Config { | ||
private String relativeDate; | ||
private String relativeVersion; | ||
|
||
/** | ||
* Gets the date used to filter deprecated shapes. | ||
* | ||
* @return The date used to filter deprecated shapes. | ||
*/ | ||
public String getRelativeDate() { | ||
return relativeDate; | ||
} | ||
|
||
/** | ||
* Sets the date used to filter deprecated shapes. | ||
* | ||
* @param relativeDate The date used to filter deprecated shapes. | ||
*/ | ||
public void setRelativeDate(String relativeDate) { | ||
this.relativeDate = relativeDate; | ||
} | ||
|
||
/** | ||
* Gets the version used to filter deprecated shapes. | ||
* | ||
* @return The version used to filter deprecated shapes. | ||
*/ | ||
public String getRelativeVersion() { | ||
return relativeVersion; | ||
} | ||
|
||
/** | ||
* Sets the version used to filter deprecated shapes. | ||
* | ||
* @param relativeVersion The version used to filter deprecated shapes. | ||
*/ | ||
public void setRelativeVersion(String relativeVersion) { | ||
this.relativeVersion = relativeVersion; | ||
} | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "removeDeprecatedShapes"; | ||
} | ||
|
||
@Override | ||
public Class<Config> getConfigType() { | ||
return Config.class; | ||
} | ||
|
||
@Override | ||
protected Model transformWithConfig(TransformContext context, Config config) { | ||
Model model = context.getModel(); | ||
ModelTransformer transformer = context.getTransformer(); | ||
model = transformer.filterDeprecatedRelativeDate(model, config.getRelativeDate()); | ||
model = transformer.filterDeprecatedRelativeVersion(model, config.getRelativeVersion()); | ||
return model; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ild/src/test/java/software/amazon/smithy/build/transforms/RemoveDeprecatedShapesTest.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,45 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.build.transforms; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.nio.file.Paths; | ||
import org.junit.jupiter.api.Test; | ||
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 RemoveDeprecatedShapesTest { | ||
@Test | ||
public void removesAllDeprecatedShapes() throws Exception { | ||
Model model = Model.assembler() | ||
.addImport(Paths.get(getClass().getResource("remove-deprecated.smithy").toURI())) | ||
.assemble() | ||
.unwrap(); | ||
TransformContext context = TransformContext.builder() | ||
.model(model) | ||
.settings(Node.objectNode() | ||
.withMember("relativeDate", Node.from("2024-10-10")) | ||
.withMember("relativeVersion", Node.from("1.1.0"))) | ||
.build(); | ||
Model result = new RemoveDeprecatedShapes().transform(context); | ||
|
||
// Deprecated by date removed | ||
assertFalse(result.getShape(ShapeId.from("smithy.example#FilteredBeforeHyphens")).isPresent()); | ||
assertFalse(result.getShape(ShapeId.from("smithy.example#FilteredVersionBefore")).isPresent()); | ||
|
||
// Equal to the filter retained | ||
assertTrue(result.getShape(ShapeId.from("smithy.example#NotFilteredDateEquals")).isPresent()); | ||
assertTrue(result.getShape(ShapeId.from("smithy.example#NotFilteredVersionEquals")).isPresent()); | ||
|
||
// After filter retained | ||
assertTrue(result.getShape(ShapeId.from("smithy.example#NotFilteredDateAfter")).isPresent()); | ||
assertTrue(result.getShape(ShapeId.from("smithy.example#NotFilteredVersionAfter")).isPresent()); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...build/src/test/resources/software/amazon/smithy/build/transforms/remove-deprecated.smithy
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,33 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
@deprecated(message: "Should be filtered as it is deprecated before date", since: "2024-10-09") | ||
structure FilteredDateBefore { | ||
field: String | ||
} | ||
|
||
@deprecated(message: "Should NOT be filtered as it is deprecated at the same date as the filter", since: "2024-10-10") | ||
structure NotFilteredDateEquals { | ||
field: String | ||
} | ||
|
||
@deprecated(message: "Should NOT be filtered as it is deprecated after the filter date", since: "2024-10-11") | ||
structure NotFilteredDateAfter { | ||
field: String | ||
} | ||
|
||
@deprecated(message: "Should be filtered as it is deprecated before version", since: "1.0.0") | ||
structure FilteredVersionBefore { | ||
field: String | ||
} | ||
|
||
@deprecated(message: "Should NOT be filtered as it is deprecated at the same version as the filter", since: "1.1.0") | ||
structure NotFilteredVersionEquals { | ||
field: String | ||
} | ||
|
||
@deprecated(message: "Should NOT be filtered as it is deprecated after the filter version", since: "1.1.1") | ||
structure NotFilteredVersionAfter { | ||
field: String | ||
} |