Skip to content

Commit

Permalink
Make trait -> traits to accept a list
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Aug 18, 2020
1 parent 95ca1fa commit 4516cd8
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
17 changes: 9 additions & 8 deletions docs/source/1.0/guides/building-models/build-config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The following is an example ``smithy-build.json`` configuration:
{
"name": "excludeShapesByTrait",
"args": {
"trait": "internal"
"traits": ["internal"]
}
}
],
Expand Down Expand Up @@ -292,7 +292,7 @@ the :ref:`tags trait <tags-trait>`.
excludeShapesByTrait
--------------------

Removes shapes if they are marked with a specific trait.
Removes shapes if they are marked with one or more specific traits.

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

.. tabs::

Expand All @@ -319,7 +320,7 @@ Removes shapes if they are marked with a specific trait.
{
"name": "excludeShapesByTrait",
"args": {
"trait": "internal"
"traits": ["internal"]
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion docs/source/1.0/guides/building-models/gradle-plugin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ projection. For example:
{
"name": "excludeShapesByTrait",
"args": {
"trait": "internal"
"traits": ["internal"]
}
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

package software.amazon.smithy.build.transforms;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import software.amazon.smithy.build.TransformContext;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.loader.Prelude;
Expand All @@ -26,24 +29,26 @@
public final class ExcludeShapesByTrait extends ConfigurableProjectionTransformer<ExcludeShapesByTrait.Config> {

public static final class Config {
private String trait;
private Set<String> traits = Collections.emptySet();

/**
* Gets the shape ID of the trait to filter shapes by.
* Gets the shape IDs of the traits to filter shapes by.
*
* @return Returns the trait shape ID.
* <p>Relative shape IDs are assumed to be in the smithy.api namespace.
*
* @return Returns the trait shape IDs.
*/
public String getTrait() {
return trait;
public Set<String> getTraits() {
return traits;
}

/**
* Sets the shape ID of the trait to filter shapes by.
* Sets the shape IDs of the traits to filter shapes by.
*
* @param trait The shape ID of the trait that if present causes a shape to be removed.
* @param traits The shape IDs of the traits that if present causes a shape to be removed.
*/
public void setTrait(String trait) {
this.trait = trait;
public void setTraits(Set<String> traits) {
this.traits = traits;
}
}

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

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());
// Resolve relative IDs by defaulting to smithy.api# if the given trait ID is relative.
Set<ShapeId> ids = new HashSet<>(config.getTraits().size());
for (String id : config.getTraits()) {
ids.add(ShapeId.fromOptionalNamespace(Prelude.NAMESPACE, id));
}

return context.getTransformer().removeShapesIf(context.getModel(), shape -> shape.hasTrait(traitId));
return context.getTransformer().removeShapesIf(context.getModel(), shape -> {
return ids.stream().anyMatch(shape::hasTrait);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void removesShapesByTrait(String trait) {
.unwrap();
TransformContext context = TransformContext.builder()
.model(model)
.settings(Node.objectNode().withMember("trait", Node.from(trait)))
.settings(Node.objectNode().withMember("traits", Node.fromStrings(trait)))
.build();
Model result = new ExcludeShapesByTrait().transform(context);

Expand Down

0 comments on commit 4516cd8

Please sign in to comment.