Skip to content

Commit

Permalink
Add exclude/include tranforms using selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
sugmanue committed Dec 13, 2022
1 parent f19dfb1 commit b642db0
Show file tree
Hide file tree
Showing 7 changed files with 783 additions and 0 deletions.
84 changes: 84 additions & 0 deletions docs/source-1.0/guides/building-models/build-config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,48 @@ Only the following shape type changes are supported:
}


.. _excludeShapesBySelector-transform:

excludeShapesBySelector
-----------------------

Removes all shapes matching the given :ref:`selector <selectors>`.

.. list-table::
:header-rows: 1
:widths: 10 20 70

* - Property
- Type
- Description
* - selector
- ``string``
- A valid :ref:`selector <selectors>` used to exclude shapes

.. tabs::

.. code-tab:: json

{
"version": "1.0",
"projections": {
"exampleProjection": {
"transforms": [
{
"name": "excludeShapesBySelector",
"args": {
"selector": ":is([id=smithy.example#FooService], [id=smithy.example#FooService] ~> *)"
}
}
]
}
}
}
.. note::

This transformer does not remove shapes from the prelude.

.. _excludeShapesByTag-transform:

excludeShapesByTag
Expand Down Expand Up @@ -384,6 +426,48 @@ Removes shapes if they are marked with one or more specific traits.
}


.. _includeShapesBySelector-transform:

includeShapesBySelector
-----------------------

Includes only the shapes matching the given :ref:`selector <selectors>`.

.. list-table::
:header-rows: 1
:widths: 10 20 70

* - Property
- Type
- Description
* - selector
- ``string``
- A valid :ref:`selector <selectors>` used to include shapes

.. tabs::

.. code-tab:: json

{
"version": "1.0",
"projections": {
"exampleProjection": {
"transforms": [
{
"name": "includeShapesBySelector",
"args": {
"selector": ":is([id=smithy.example#FooService], [id=smithy.example#FooService] ~> *)"
}
}
]
}
}
}
.. note::

This transformer does not remove shapes from the prelude.

.. _includeShapesByTag-transform:

includeShapesByTag
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2022 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 java.util.Set;
import software.amazon.smithy.build.TransformContext;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.selector.Selector;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.utils.FunctionalUtils;

/**
* {@code excludeShapesBySelector} excludes the shapes matching the given selector.
*
* <p>Prelude shapes are not removed by this transformer.
*/
public final class ExcludeShapesBySelector extends BackwardCompatHelper<ExcludeShapesBySelector.Config> {

/**
* {@code excludeShapesBySelector} configuration.
*/
public static final class Config {
private String selector = null;

/**
* Gets the selector used to filter the shapes.
*
* @return The selector used to filter the shapes.
*/
public String getSelector() {
return selector;
}

/**
* Sets the selector used to filter the shapes.
*
* @param selector The selector used to filter the shapes.
*/
public void setSelector(String selector) {
this.selector = selector;
}
}

@Override
public Class<Config> getConfigType() {
return Config.class;
}

@Override
public String getName() {
return "excludeShapesBySelector";
}

@Override
String getBackwardCompatibleNameMapping() {
return "selector";
}

@Override
protected Model transformWithConfig(TransformContext context, Config config) {
String selector = config.getSelector();
ModelTransformer transformer = context.getTransformer();
Model model = context.getModel();
Set<Shape> selected = Selector.parse(selector).select(model);
return transformer.filterShapes(model, FunctionalUtils.not(selected::contains));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2022 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 java.util.Set;
import software.amazon.smithy.build.TransformContext;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.selector.Selector;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.transform.ModelTransformer;

/**
* {@code includeShapesBySelector} includes the shapes matching the given selector.
*
* <p>Prelude shapes are not removed by this transformer.
*/
public final class IncludeShapesBySelector extends BackwardCompatHelper<IncludeShapesBySelector.Config> {

/**
* {@code includeShapesBySelector} configuration.
*/
public static final class Config {
private String selector = null;

/**
* Gets the selector used to filter the shapes.
*
* @return The selector used to filter the shapes.
*/
public String getSelector() {
return selector;
}

/**
* Sets the selector used to filter the shapes.
*
* @param selector The selector used to filter the shapes.
*/
public void setSelector(String selector) {
this.selector = selector;
}
}

@Override
public Class<Config> getConfigType() {
return Config.class;
}

@Override
public String getName() {
return "includeShapesBySelector";
}

@Override
String getBackwardCompatibleNameMapping() {
return "selector";
}

@Override
protected Model transformWithConfig(TransformContext context, Config config) {
String selector = config.getSelector();
ModelTransformer transformer = context.getTransformer();
Model model = context.getModel();
Set<Shape> selected = Selector.parse(selector).select(model);
return transformer.filterShapes(model, selected::contains);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ software.amazon.smithy.build.transforms.Apply
software.amazon.smithy.build.transforms.ChangeStringEnumsToEnumShapes
software.amazon.smithy.build.transforms.ChangeTypes
software.amazon.smithy.build.transforms.ExcludeMetadata
software.amazon.smithy.build.transforms.ExcludeShapesBySelector
software.amazon.smithy.build.transforms.ExcludeShapesByTag
software.amazon.smithy.build.transforms.ExcludeShapesByTrait
software.amazon.smithy.build.transforms.ExcludeTags
Expand All @@ -12,6 +13,7 @@ software.amazon.smithy.build.transforms.FilterSuppressions
software.amazon.smithy.build.transforms.IncludeMetadata
software.amazon.smithy.build.transforms.IncludeNamespaces
software.amazon.smithy.build.transforms.IncludeServices
software.amazon.smithy.build.transforms.IncludeShapesBySelector
software.amazon.smithy.build.transforms.IncludeShapesByTag
software.amazon.smithy.build.transforms.IncludeTags
software.amazon.smithy.build.transforms.IncludeTraits
Expand Down
Loading

0 comments on commit b642db0

Please sign in to comment.