public final class ModelTransformer
extends java.lang.Object
Model
s.Modifier and Type | Method and Description |
---|---|
Model |
changeShapeType(Model model,
java.util.Map<ShapeId,ShapeType> shapeToType)
Changes the type of each given shape.
|
Model |
copyServiceErrorsToOperations(Model model,
ServiceShape forService)
Copies the errors defined on the given service onto each operation bound to the
service, effectively flattening service error inheritance.
|
static ModelTransformer |
create()
Creates a ModelTransformer using ModelTransformerPlugin instances
discovered using the
com.software.smithy.transform class
loader and any modules found in the module path. |
Model |
createDedicatedInputAndOutput(Model model,
java.lang.String inputSuffix,
java.lang.String outputSuffix)
Updates the model so that every operation has a dedicated input shape marked
with the
input trait and output shape marked with the output
trait, and the targeted shapes all have a consistent shape name of
OperationName + inputSuffix / outputSuffix depending on the
context. |
static ModelTransformer |
createWithPlugins(java.util.List<ModelTransformerPlugin> plugins)
Creates a ModelTransformer using a list of ModelTransformer plugins.
|
static ModelTransformer |
createWithServiceProviders(java.lang.ClassLoader classLoader)
Creates a ModelTransformer that finds
ModelTransformerPlugin
service providers using the given ClassLoader . |
Model |
filterMetadata(Model model,
java.util.function.BiPredicate<java.lang.String,Node> predicate)
Filters out metadata key-value pairs from a model that do not match
a predicate.
|
Model |
filterShapes(Model model,
java.util.function.Predicate<Shape> predicate)
Filters shapes out of the model that do not match the given predicate.
|
Model |
filterTraits(Model model,
java.util.function.BiPredicate<Shape,Trait> predicate)
Filters traits out of the model that do not match the given predicate.
|
Model |
getModelWithoutTraitShapes(Model model)
Gets all shapes from a model where shapes that define traits or shapes
that are only used as part of a trait definition have been removed.
|
Model |
getModelWithoutTraitShapes(Model model,
java.util.function.Predicate<Shape> keepFilter)
Gets all shapes from a model where shapes that define traits or shapes
that are only used as part of a trait definition have been removed.
|
Model |
mapShapes(Model model,
java.util.function.Function<Shape,Shape> mapper)
Maps over all shapes in the model using a mapping function, allowing
shapes to be replaced with completely different shapes or slightly
modified shapes.
|
Model |
mapShapes(Model model,
java.util.List<java.util.function.Function<Shape,Shape>> mappers)
Maps over all shapes in the model using multiple mapping functions.
|
Model |
mapTraits(Model model,
java.util.function.BiFunction<Shape,Trait,Trait> mapper)
Maps over all traits in the model using a mapping function that accepts
the shape the trait is applied to, a trait, and returns a trait,
possibly even a different kind of trait.
|
Model |
mapTraits(Model model,
java.util.List<java.util.function.BiFunction<Shape,Trait,Trait>> mappers)
Maps over all traits in the model using multiple mapping functions.
|
Model |
removeShapes(Model model,
java.util.Collection<Shape> shapes)
Removes shapes from the model while ensuring that the model is in a
consistent state.
|
Model |
removeShapesIf(Model model,
java.util.function.Predicate<Shape> predicate)
Removes shapes from the model that match the given predicate.
|
Model |
removeTraitsIf(Model model,
java.util.function.BiPredicate<Shape,Trait> predicate)
Filters traits out of the model that match a predicate function.
|
Model |
removeUnreferencedShapes(Model model)
Removes shapes (excluding service shapes) that are not referenced by
any other shapes.
|
Model |
removeUnreferencedShapes(Model model,
java.util.function.Predicate<Shape> keepFilter)
Removes shapes (excluding service shapes) that are not referenced by
any other shapes.
|
Model |
removeUnreferencedTraitDefinitions(Model model)
Removes definitions for traits that are not used by any shape in the
model.base.
|
Model |
removeUnreferencedTraitDefinitions(Model model,
java.util.function.Predicate<Shape> keepFilter)
Removes trait definitions for traits that are not used by any shape
in the model.
|
Model |
renameShapes(Model model,
java.util.Map<ShapeId,ShapeId> renamed)
Renames shapes using ShapeId pairs while ensuring that the
transformed model is in a consistent state.
|
Model |
renameShapes(Model model,
java.util.Map<ShapeId,ShapeId> renamed,
java.util.function.Supplier<ModelAssembler> modelAssemblerSupplier)
Renames shapes using ShapeId pairs while ensuring that the
transformed model is in a consistent state.
|
Model |
replaceShapes(Model model,
java.util.Collection<Shape> shapes)
Adds or replaces shapes into the model while ensuring that the model
is in a consistent state.
|
Model |
scrubTraitDefinitions(Model model)
Removes all trait definitions from a model and all shapes that are
only connected to the graph either directly or transitively by a
trait definition shape.
|
Model |
scrubTraitDefinitions(Model model,
java.util.function.Predicate<Shape> keepFilter)
Removes trait definitions from a model and all shapes that are
only connected to the graph either directly or transitively by a
trait definition shape.
|
Model |
sortMembers(Model model,
java.util.Comparator<MemberShape> comparator)
Reorders the members of structure and union shapes using the given
Comparator . |
public static ModelTransformer create()
com.software.smithy.transform
class
loader and any modules found in the module path.public static ModelTransformer createWithPlugins(java.util.List<ModelTransformerPlugin> plugins)
plugins
- Plugins to use with the transformer.public static ModelTransformer createWithServiceProviders(java.lang.ClassLoader classLoader)
ModelTransformerPlugin
service providers using the given ClassLoader
.classLoader
- ClassLoader used to find ModelTransformerPlugin instances.public Model replaceShapes(Model model, java.util.Collection<Shape> shapes)
model
- Model to transform.shapes
- Shapes to add or replace in the model.base.public Model removeShapes(Model model, java.util.Collection<Shape> shapes)
model
- Model to transform.shapes
- Shapes to add or replace in the model.base.public Model removeShapesIf(Model model, java.util.function.Predicate<Shape> predicate)
model
- Model to transform.predicate
- Predicate that accepts a shape and returns true to
remove it.public Model renameShapes(Model model, java.util.Map<ShapeId,ShapeId> renamed)
This transformer ensures that when an aggregate shape is renamed, all members are updated in the model.
model
- Model to transform.renamed
- Map of shapeIdspublic Model renameShapes(Model model, java.util.Map<ShapeId,ShapeId> renamed, java.util.function.Supplier<ModelAssembler> modelAssemblerSupplier)
This transformer ensures that when an aggregate shape is renamed, all members are updated in the model.
model
- Model to transform.renamed
- Map of shapeIdsmodelAssemblerSupplier
- Supplier used to create ModelAssembler
s in each transform.public Model filterShapes(Model model, java.util.function.Predicate<Shape> predicate)
This filter will never filter out shapes that are part of the
prelude. Use the removeShapes(software.amazon.smithy.model.Model, java.util.Collection<software.amazon.smithy.model.shapes.Shape>)
method directly if you need
to remove traits that are in the prelude.
model
- Model to transform.predicate
- Predicate that filters shapes.public Model filterTraits(Model model, java.util.function.BiPredicate<Shape,Trait> predicate)
The predicate function accepts the shape that a trait is attached to and the trait. If the predicate returns false, then the trait is removed from the shape.
model
- Model to transform.predicate
- Predicate that accepts a (Shape, Trait) and returns
false if the trait should be removed.public Model removeTraitsIf(Model model, java.util.function.BiPredicate<Shape,Trait> predicate)
The predicate function accepts the shape that a trait is attached to and the trait. If the predicate returns true, then the trait is removed from the shape.
model
- Model to transform.predicate
- Predicate that accepts a (Shape, Trait) and returns
true if the trait should be removed.public Model filterMetadata(Model model, java.util.function.BiPredicate<java.lang.String,Node> predicate)
model
- Model to transform.predicate
- A predicate that accepts a metadata key-value pair.
If the predicate returns true, then the metadata key-value pair is
kept. Otherwise, it is removed.public Model mapTraits(Model model, java.util.function.BiFunction<Shape,Trait,Trait> mapper)
An exception is thrown if a trait is returned that targets a
different shape than the Shape
passed into the mapper function.
model
- Model to transform.mapper
- Mapping function that accepts a (Shape, Trait) and returns
the mapped Trait.public Model mapTraits(Model model, java.util.List<java.util.function.BiFunction<Shape,Trait,Trait>> mappers)
Note: passing in a list of mappers is much more efficient than
invoking mapTraits
multiple times because it reduces the number
of intermediate models that are needed to perform the transformation.
model
- Model to transform.mappers
- Mapping functions that accepts a (Shape, Trait) and
returns the mapped Trait.for more information.
public Model mapShapes(Model model, java.util.function.Function<Shape,Shape> mapper)
An exception is thrown if a mapper returns a shape with a different shape ID or a different type.
model
- Model to transform.mapper
- Mapping function that accepts a shape and returns a shape
with the same ID.public Model mapShapes(Model model, java.util.List<java.util.function.Function<Shape,Shape>> mappers)
Note: passing in a list of mappers is much more efficient than
invoking mapShapes
multiple times because it reduces the
number of intermediate models that are needed to perform the
transformation.
model
- Model to transform.mappers
- Mapping functions that accepts a shape and returns a
shape with the same ID.for more information.
public Model removeUnreferencedShapes(Model model)
model
- Model to transform.public Model removeUnreferencedShapes(Model model, java.util.function.Predicate<Shape> keepFilter)
model
- Model to transform.keepFilter
- Predicate function that accepts an unreferenced
shape and returns true to remove the shape or false to keep the shape
in the model.base.public Model removeUnreferencedTraitDefinitions(Model model)
model
- Model to transformpublic Model removeUnreferencedTraitDefinitions(Model model, java.util.function.Predicate<Shape> keepFilter)
Trait definitions that are part of the prelude will not be removed.
model
- Model to transformkeepFilter
- Predicate function that accepts an unreferenced trait
shape (that has the TraitDefinition
trait) and returns true to
remove the definition or false to keep the definition in the model.base.public Model scrubTraitDefinitions(Model model)
This can be useful when serializing a Smithy model to a format that does not include trait definitions and the shapes used by trait definitions would have no meaning (e.g., OpenAPI).
model
- Model to transform.public Model scrubTraitDefinitions(Model model, java.util.function.Predicate<Shape> keepFilter)
This can be useful when serializing a Smithy model to a format that does not include trait definitions and the shapes used by trait definitions would have no meaning (e.g., OpenAPI).
model
- Model to transform.keepFilter
- Predicate function that accepts an trait shape (that
has the TraitDefinition
trait) and returns true to remove the
definition or false to keep the definition in the model.public Model getModelWithoutTraitShapes(Model model)
model
- Model that contains shapes.public Model getModelWithoutTraitShapes(Model model, java.util.function.Predicate<Shape> keepFilter)
model
- Model that contains shapes.keepFilter
- Predicate function that accepts a trait shape (that
has the TraitDefinition
trait) and returns true to remove the
definition or false to keep the definition in the model.public Model sortMembers(Model model, java.util.Comparator<MemberShape> comparator)
Comparator
.
Note that by default, Smithy models retain the order in which members are defined in the model. However, in programming languages where this isn't important, it may be desirable to order members alphabetically or using some other kind of order.
model
- Model that contains shapes.comparator
- Comparator used to order members of unions and structures.public Model changeShapeType(Model model, java.util.Map<ShapeId,ShapeType> shapeToType)
The following transformations are permitted:
model
- Model to transform.shapeToType
- Map of shape IDs to the new type to use for the shape.ModelTransformException
- if an incompatible type transform is attempted.public Model copyServiceErrorsToOperations(Model model, ServiceShape forService)
model
- Model to modify.forService
- Service shape to use as the basis for copying errors to operations.public Model createDedicatedInputAndOutput(Model model, java.lang.String inputSuffix, java.lang.String outputSuffix)
input
trait and output shape marked with the output
trait, and the targeted shapes all have a consistent shape name of
OperationName + inputSuffix
/ outputSuffix
depending on the
context.
If an operation's input already targets a shape marked with the input
trait, then the existing input shape is used as input, though the shape will
be renamed if it does not use the given inputSuffix
. If an operation's
output already targets a shape marked with the output
trait, then the
existing output shape is used as output, though the shape will be renamed if it
does not use the given outputSuffix
.
If the operation's input shape starts with the name of the operation and is
only used throughout the model as the input of the operation, then it is updated
to have the input
trait, and the name remains unaltered.
If the operation's output shape starts with the name of the operation and is
only used throughout the model as the output of the operation, then it is updated
to have the output
trait, and the name remains unaltered.
If the operation's input shape does not start with the operation's name or
is used in other places throughout the model, a copy of the targeted input
structure is created, the name of the shape becomes OperationName + inputSuffix
,
and the input
trait is added to the shape. The operation is then updated
to target the created shape, and the original shape is left as-is in the model.
If the operation's output shape does not start with the operation's name or
is used in other places throughout the model, a copy of the targeted output
structure is created, the name of the shape becomes OperationName + outputSuffix
,
and the output
trait is added to the shape. The operation is then updated
to target the created shape, and the original shape is left as-is in the model.
If a naming conflict occurs while attempting to create a new shape, then
the default naming conflict resolver will attempt to name the shape
OperationName + "Operation" + inputSuffix
/ outputSuffix
depending on the context. If the name is still in conflict with other
shapes in the model, then a ModelTransformException
is thrown.
Any time a shape is renamed, the original shape ID of the shape is captured
on the shape using the synthetic OriginalShapeIdTrait
. This might be
useful for protocols that need to serialize input and output shape names.
model
- Model to update.inputSuffix
- Suffix to append to dedicated input shapes (e.g., "Input").outputSuffix
- Suffix to append to dedicated input shapes (e.g., "Output").ModelTransformException
- if an input or output shape name conflict occurs.