From a7cadd629c0c4f6b59804c1db8ecf66aabda10f4 Mon Sep 17 00:00:00 2001 From: JordonPhillips Date: Tue, 11 Oct 2022 14:50:09 +0200 Subject: [PATCH] Allow transforms to remove enum members This fixes a bug that was preventing enum members from being removable from transforms, such as removeShapesByTag. --- .../transforms/ExcludeShapesByTagTest.java | 23 +++++++++++++++++++ .../build/transforms/filter-by-tags.smithy | 21 +++++++++++++++++ .../smithy/model/transform/FilterShapes.java | 5 +++- .../CleanStructureAndUnionMembers.java | 22 +++++++++++++++++- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 smithy-build/src/test/resources/software/amazon/smithy/build/transforms/filter-by-tags.smithy diff --git a/smithy-build/src/test/java/software/amazon/smithy/build/transforms/ExcludeShapesByTagTest.java b/smithy-build/src/test/java/software/amazon/smithy/build/transforms/ExcludeShapesByTagTest.java index 7ac3a25a72d..13bbce20a8b 100644 --- a/smithy-build/src/test/java/software/amazon/smithy/build/transforms/ExcludeShapesByTagTest.java +++ b/smithy-build/src/test/java/software/amazon/smithy/build/transforms/ExcludeShapesByTagTest.java @@ -19,11 +19,15 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; +import java.nio.file.Paths; import java.util.Optional; 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.EnumShape; +import software.amazon.smithy.model.shapes.IntEnumShape; +import software.amazon.smithy.model.shapes.ShapeId; import software.amazon.smithy.model.shapes.StringShape; import software.amazon.smithy.model.traits.TagsTrait; @@ -51,4 +55,23 @@ public void removesTraitsNotInList() { assertThat(result.getShape(stringA.getId()), is(Optional.empty())); assertThat(result.getShape(stringB.getId()), not(Optional.empty())); } + + @Test + public void filtersMembers() throws Exception { + Model model = Model.assembler() + .addImport(Paths.get(getClass().getResource("filter-by-tags.smithy").toURI())) + .assemble() + .unwrap(); + TransformContext context = TransformContext.builder() + .model(model) + .settings(Node.objectNode().withMember("tags", Node.fromStrings("filter"))) + .build(); + Model result = new ExcludeShapesByTag().transform(context); + + EnumShape foo = result.expectShape(ShapeId.from("smithy.example#Foo"), EnumShape.class); + assertThat(foo.members().size(), is(1)); + + IntEnumShape bar = result.expectShape(ShapeId.from("smithy.example#Bar"), IntEnumShape.class); + assertThat(bar.members().size(), is(1)); + } } diff --git a/smithy-build/src/test/resources/software/amazon/smithy/build/transforms/filter-by-tags.smithy b/smithy-build/src/test/resources/software/amazon/smithy/build/transforms/filter-by-tags.smithy new file mode 100644 index 00000000000..1760b524943 --- /dev/null +++ b/smithy-build/src/test/resources/software/amazon/smithy/build/transforms/filter-by-tags.smithy @@ -0,0 +1,21 @@ +$version: "2.0" + +namespace smithy.example + +enum Foo { + @tags(["filter"]) + FILTER + + @tags(["keep"]) + KEEP +} + +intEnum Bar { + @tags(["filter"]) + @enumValue(1) + FILTER + + @tags(["keep"]) + @enumValue(2) + KEEP +} \ No newline at end of file diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/transform/FilterShapes.java b/smithy-model/src/main/java/software/amazon/smithy/model/transform/FilterShapes.java index ac30c6c6703..f2aa261c5cc 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/transform/FilterShapes.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/transform/FilterShapes.java @@ -51,7 +51,10 @@ Model transform(ModelTransformer transformer, Model model) { private static boolean canFilterShape(Model model, Shape shape) { return !shape.isMemberShape() || model.getShape(shape.asMemberShape().get().getContainer()) - .filter(container -> container.isStructureShape() || container.isUnionShape()) + .filter(container -> container.isStructureShape() + || container.isUnionShape() + || container.isEnumShape() + || container.isIntEnumShape()) .isPresent(); } } diff --git a/smithy-model/src/main/java/software/amazon/smithy/model/transform/plugins/CleanStructureAndUnionMembers.java b/smithy-model/src/main/java/software/amazon/smithy/model/transform/plugins/CleanStructureAndUnionMembers.java index b71752243d9..0f0fbab1aba 100644 --- a/smithy-model/src/main/java/software/amazon/smithy/model/transform/plugins/CleanStructureAndUnionMembers.java +++ b/smithy-model/src/main/java/software/amazon/smithy/model/transform/plugins/CleanStructureAndUnionMembers.java @@ -28,6 +28,8 @@ import java.util.function.Function; import java.util.stream.Collectors; import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.shapes.EnumShape; +import software.amazon.smithy.model.shapes.IntEnumShape; import software.amazon.smithy.model.shapes.MemberShape; import software.amazon.smithy.model.shapes.Shape; import software.amazon.smithy.model.shapes.ShapeId; @@ -39,7 +41,7 @@ import software.amazon.smithy.utils.Pair; /** - * Cleans up structure and union shapes after shapes are removed. + * Cleans up structure, union, enum, and intEnum shapes after shapes are removed. * *