Skip to content

Commit 60af099

Browse files
committed
Make trait -> traits to accept a list
1 parent 7078a4d commit 60af099

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

docs/source/1.0/guides/building-models/build-config.rst

+9-8
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ The following is an example ``smithy-build.json`` configuration:
7676
{
7777
"name": "excludeShapesByTrait",
7878
"args": {
79-
"trait": "internal"
79+
"traits": ["internal"]
8080
}
8181
}
8282
],
@@ -292,7 +292,7 @@ the :ref:`tags trait <tags-trait>`.
292292
excludeShapesByTrait
293293
--------------------
294294

295-
Removes shapes if they are marked with a specific trait.
295+
Removes shapes if they are marked with one or more specific traits.
296296

297297
.. list-table::
298298
:header-rows: 1
@@ -301,11 +301,12 @@ Removes shapes if they are marked with a specific trait.
301301
* - Property
302302
- Type
303303
- Description
304-
* - trait
305-
- ``string``
306-
- The :ref:`shape ID <shape-id>` of a trait. If this trait is found on
307-
a shape, the shape is removed from the model. Relative shape IDs are
308-
assumed to be in the ``smithy.api`` prelude namespace.
304+
* - traits
305+
- ``[string]``
306+
- A list of trait :ref:`shape IDs <shape-id>`. If any of these traits
307+
are found on a shape, the shape is removed from the model. Relative
308+
shape IDs are assumed to be in the ``smithy.api``
309+
:ref:`prelude <prelude>` namespace.
309310

310311
.. tabs::
311312

@@ -319,7 +320,7 @@ Removes shapes if they are marked with a specific trait.
319320
{
320321
"name": "excludeShapesByTrait",
321322
"args": {
322-
"trait": "internal"
323+
"traits": ["internal"]
323324
}
324325
}
325326
]

docs/source/1.0/guides/building-models/gradle-plugin.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ projection. For example:
248248
{
249249
"name": "excludeShapesByTrait",
250250
"args": {
251-
"trait": "internal"
251+
"traits": ["internal"]
252252
}
253253
},
254254
{

smithy-build/src/main/java/software/amazon/smithy/build/transforms/ExcludeShapesByTrait.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
package software.amazon.smithy.build.transforms;
1717

18+
import java.util.Collections;
19+
import java.util.HashSet;
20+
import java.util.Set;
1821
import software.amazon.smithy.build.TransformContext;
1922
import software.amazon.smithy.model.Model;
2023
import software.amazon.smithy.model.loader.Prelude;
@@ -26,24 +29,26 @@
2629
public final class ExcludeShapesByTrait extends ConfigurableProjectionTransformer<ExcludeShapesByTrait.Config> {
2730

2831
public static final class Config {
29-
private String trait;
32+
private Set<String> traits = Collections.emptySet();
3033

3134
/**
32-
* Gets the shape ID of the trait to filter shapes by.
35+
* Gets the shape IDs of the traits to filter shapes by.
3336
*
34-
* @return Returns the trait shape ID.
37+
* <p>Relative shape IDs are assumed to be in the smithy.api namespace.
38+
*
39+
* @return Returns the trait shape IDs.
3540
*/
36-
public String getTrait() {
37-
return trait;
41+
public Set<String> getTraits() {
42+
return traits;
3843
}
3944

4045
/**
41-
* Sets the shape ID of the trait to filter shapes by.
46+
* Sets the shape IDs of the traits to filter shapes by.
4247
*
43-
* @param trait The shape ID of the trait that if present causes a shape to be removed.
48+
* @param traits The shape IDs of the traits that if present causes a shape to be removed.
4449
*/
45-
public void setTrait(String trait) {
46-
this.trait = trait;
50+
public void setTraits(Set<String> traits) {
51+
this.traits = traits;
4752
}
4853
}
4954

@@ -58,9 +63,14 @@ public Class<Config> getConfigType() {
5863
}
5964

6065
protected Model transformWithConfig(TransformContext context, Config config) {
61-
// Default to smithy.api# if the given trait ID is relative.
62-
ShapeId traitId = ShapeId.fromOptionalNamespace(Prelude.NAMESPACE, config.getTrait());
66+
// Resolve relative IDs by defaulting to smithy.api# if the given trait ID is relative.
67+
Set<ShapeId> ids = new HashSet<>(config.getTraits().size());
68+
for (String id : config.getTraits()) {
69+
ids.add(ShapeId.fromOptionalNamespace(Prelude.NAMESPACE, id));
70+
}
6371

64-
return context.getTransformer().removeShapesIf(context.getModel(), shape -> shape.hasTrait(traitId));
72+
return context.getTransformer().removeShapesIf(context.getModel(), shape -> {
73+
return ids.stream().anyMatch(shape::hasTrait);
74+
});
6575
}
6676
}

smithy-build/src/test/java/software/amazon/smithy/build/transforms/ExcludeShapesByTraitTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void removesShapesByTrait(String trait) {
2323
.unwrap();
2424
TransformContext context = TransformContext.builder()
2525
.model(model)
26-
.settings(Node.objectNode().withMember("trait", Node.from(trait)))
26+
.settings(Node.objectNode().withMember("traits", Node.fromStrings(trait)))
2727
.build();
2828
Model result = new ExcludeShapesByTrait().transform(context);
2929

0 commit comments

Comments
 (0)