Class ModelTransformer
- java.lang.Object
-
- software.amazon.smithy.model.transform.ModelTransformer
-
public final class ModelTransformer extends java.lang.Object
Class used to transformModel
s.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ModelTransformer
create()
Creates a ModelTransformer using ModelTransformerPlugin instances discovered using thecom.software.smithy.transform
class loader and any modules found in the module path.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 findsModelTransformerPlugin
service providers using the givenClassLoader
.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 givenComparator
.
-
-
-
Method Detail
-
create
public static ModelTransformer create()
Creates a ModelTransformer using ModelTransformerPlugin instances discovered using thecom.software.smithy.transform
class loader and any modules found in the module path.- Returns:
- Returns the created ModelTransformer.
-
createWithPlugins
public static ModelTransformer createWithPlugins(java.util.List<ModelTransformerPlugin> plugins)
Creates a ModelTransformer using a list of ModelTransformer plugins.- Parameters:
plugins
- Plugins to use with the transformer.- Returns:
- Returns the created ModelTransformer.
-
createWithServiceProviders
public static ModelTransformer createWithServiceProviders(java.lang.ClassLoader classLoader)
Creates a ModelTransformer that findsModelTransformerPlugin
service providers using the givenClassLoader
.- Parameters:
classLoader
- ClassLoader used to find ModelTransformerPlugin instances.- Returns:
- Returns the created ModelTransformer.
-
replaceShapes
public 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.- Parameters:
model
- Model to transform.shapes
- Shapes to add or replace in the model.base.- Returns:
- Returns the transformed model.base.
-
removeShapes
public Model removeShapes(Model model, java.util.Collection<Shape> shapes)
Removes shapes from the model while ensuring that the model is in a consistent state.- Parameters:
model
- Model to transform.shapes
- Shapes to add or replace in the model.base.- Returns:
- Returns the transformed model.base.
-
removeShapesIf
public Model removeShapesIf(Model model, java.util.function.Predicate<Shape> predicate)
Removes shapes from the model that match the given predicate.- Parameters:
model
- Model to transform.predicate
- Predicate that accepts a shape and returns true to remove it.- Returns:
- Returns the transformed model.base.
-
renameShapes
public 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.This transformer ensures that when an aggregate shape is renamed, all members are updated in the model.
- Parameters:
model
- Model to transform.renamed
- Map of shapeIds- Returns:
- Returns the transformed model.base.
-
renameShapes
public 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.This transformer ensures that when an aggregate shape is renamed, all members are updated in the model.
- Parameters:
model
- Model to transform.renamed
- Map of shapeIdsmodelAssemblerSupplier
- Supplier used to createModelAssembler
s in each transform.- Returns:
- Returns the transformed model.
-
filterShapes
public Model filterShapes(Model model, java.util.function.Predicate<Shape> predicate)
Filters shapes out of the model that do not match the given 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.- Parameters:
model
- Model to transform.predicate
- Predicate that filters shapes.- Returns:
- Returns the transformed model.
-
filterTraits
public Model filterTraits(Model model, java.util.function.BiPredicate<Shape,Trait> predicate)
Filters traits out of the model that do not match the given 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.
- Parameters:
model
- Model to transform.predicate
- Predicate that accepts a (Shape, Trait) and returns false if the trait should be removed.- Returns:
- Returns the transformed model.base.
-
removeTraitsIf
public Model removeTraitsIf(Model model, java.util.function.BiPredicate<Shape,Trait> predicate)
Filters traits out of the model that match a predicate function.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.
- Parameters:
model
- Model to transform.predicate
- Predicate that accepts a (Shape, Trait) and returns true if the trait should be removed.- Returns:
- Returns the transformed model.base.
-
filterMetadata
public 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.- Parameters:
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.- Returns:
- Returns the transformed model.base.
-
mapTraits
public 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.An exception is thrown if a trait is returned that targets a different shape than the
Shape
passed into the mapper function.- Parameters:
model
- Model to transform.mapper
- Mapping function that accepts a (Shape, Trait) and returns the mapped Trait.- Returns:
- Returns the transformed model.base.
-
mapTraits
public 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.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.- Parameters:
model
- Model to transform.mappers
- Mapping functions that accepts a (Shape, Trait) and returns the mapped Trait.- Returns:
- Returns the transformed model.base.
- See Also:
for more information.
-
mapShapes
public 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.An exception is thrown if a mapper returns a shape with a different shape ID or a different type.
- Parameters:
model
- Model to transform.mapper
- Mapping function that accepts a shape and returns a shape with the same ID.- Returns:
- Returns the transformed model.base.
-
mapShapes
public 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.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.- Parameters:
model
- Model to transform.mappers
- Mapping functions that accepts a shape and returns a shape with the same ID.- Returns:
- Returns the transformed model.base.
- See Also:
for more information.
-
removeUnreferencedShapes
public Model removeUnreferencedShapes(Model model)
Removes shapes (excluding service shapes) that are not referenced by any other shapes.- Parameters:
model
- Model to transform.- Returns:
- Returns the transformed model.base.
-
removeUnreferencedShapes
public Model removeUnreferencedShapes(Model model, java.util.function.Predicate<Shape> keepFilter)
Removes shapes (excluding service shapes) that are not referenced by any other shapes. Shapes that are part of the prelude or that act as the shape of any trait, regardless of if the trait is in use in the model, are never considered unreferenced.- Parameters:
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.- Returns:
- Returns the transformed model.base.
-
removeUnreferencedTraitDefinitions
public Model removeUnreferencedTraitDefinitions(Model model)
Removes definitions for traits that are not used by any shape in the model.base. Trait definitions that are part of the prelude will not be removed.- Parameters:
model
- Model to transform- Returns:
- Returns the transformed model.base.
-
removeUnreferencedTraitDefinitions
public 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.Trait definitions that are part of the prelude will not be removed.
- Parameters:
model
- Model to transformkeepFilter
- Predicate function that accepts an unreferenced trait shape (that has theTraitDefinition
trait) and returns true to remove the definition or false to keep the definition in the model.base.- Returns:
- Returns the transformed model.base.
-
scrubTraitDefinitions
public 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.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).
- Parameters:
model
- Model to transform.- Returns:
- Returns the transformed model.base.
-
scrubTraitDefinitions
public 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.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).
- Parameters:
model
- Model to transform.keepFilter
- Predicate function that accepts an trait shape (that has theTraitDefinition
trait) and returns true to remove the definition or false to keep the definition in the model.- Returns:
- Returns the transformed model.
-
getModelWithoutTraitShapes
public 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.- Parameters:
model
- Model that contains shapes.- Returns:
- Returns a model that contains matching shapes.
-
getModelWithoutTraitShapes
public 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.- Parameters:
model
- Model that contains shapes.keepFilter
- Predicate function that accepts a trait shape (that has theTraitDefinition
trait) and returns true to remove the definition or false to keep the definition in the model.- Returns:
- Returns a model that contains matching shapes.
-
sortMembers
public Model sortMembers(Model model, java.util.Comparator<MemberShape> comparator)
Reorders the members of structure and union shapes using the givenComparator
.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.
- Parameters:
model
- Model that contains shapes.comparator
- Comparator used to order members of unions and structures.- Returns:
- Returns a model that contains matching shapes.
-
-