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 1183be3
Show file tree
Hide file tree
Showing 7 changed files with 909 additions and 0 deletions.
82 changes: 82 additions & 0 deletions docs/source-2.0/guides/building-models/build-config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,47 @@ 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.

.. code-block:: json
{
"version": "1.0",
"projections": {
"exampleProjection": {
"transforms": [
{
"name": "excludeShapesBySelector",
"args": {
// Excludes all operations that use event streams.
"selector": "[trait|streaming] :test(<) :is(< member < structure <-[input, output]- operation)"
}
}
]
}
}
}
.. note::

This transformer does not remove shapes from the prelude.

.. _excludeShapesByTag-transform:

excludeShapesByTag
Expand Down Expand Up @@ -374,6 +415,47 @@ 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.

.. code-block:: json
{
"version": "1.0",
"projections": {
"exampleProjection": {
"transforms": [
{
"name": "includeShapesBySelector",
"args": {
// Includes only shapes in the FooService closure.
"selector": "[id=smithy.example#FooService] is(*, ~> *)"
}
}
]
}
}
}
.. 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,76 @@
/*
* 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 ConfigurableProjectionTransformer<ExcludeShapesBySelector.Config> {

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

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

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

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

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

@Override
protected Model transformWithConfig(TransformContext context, Config config) {
Selector selector = config.getSelector();
ModelTransformer transformer = context.getTransformer();
Model model = context.getModel();
Set<Shape> selected = selector.select(model);
return transformer.filterShapes(model, FunctionalUtils.not(selected::contains));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 ConfigurableProjectionTransformer<IncludeShapesBySelector.Config> {

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

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

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

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

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


@Override
protected Model transformWithConfig(TransformContext context, Config config) {
Selector selector = config.getSelector();
ModelTransformer transformer = context.getTransformer();
Model model = context.getModel();
Set<Shape> selected = 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 1183be3

Please sign in to comment.