-
Notifications
You must be signed in to change notification settings - Fork 244
Add flattenNamespaces transformer #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
srchase
merged 3 commits into
smithy-lang:master
from
srchase:namespace-flattening-transformer
Sep 29, 2020
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
smithy-build/src/main/java/software/amazon/smithy/build/transforms/FlattenNamespaces.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * Copyright 2020 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.Collections; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.stream.Collectors; | ||
| import software.amazon.smithy.build.SmithyBuildException; | ||
| import software.amazon.smithy.build.TransformContext; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.knowledge.NeighborProviderIndex; | ||
| import software.amazon.smithy.model.loader.Prelude; | ||
| import software.amazon.smithy.model.neighbor.Walker; | ||
| import software.amazon.smithy.model.shapes.ServiceShape; | ||
| import software.amazon.smithy.model.shapes.Shape; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
| import software.amazon.smithy.model.transform.ModelTransformer; | ||
| import software.amazon.smithy.utils.FunctionalUtils; | ||
| import software.amazon.smithy.utils.Pair; | ||
|
|
||
| /** | ||
| * {@code flattenNamespaces} updates a model by flattening the namespaces of | ||
| * shapes connected to a service into a single, target namespace. When | ||
| * configuring the transformer, a service and target namespace must be | ||
| * specified. Optionally, tags can be specified for including any additional | ||
| * shapes that should be flattened into the the target namespace. Any shape | ||
| * from outside the service closure that is included via the application of a | ||
| * tag will not be included if it conflicts with a shape in the service closure. | ||
| */ | ||
| public final class FlattenNamespaces extends ConfigurableProjectionTransformer<FlattenNamespaces.Config> { | ||
|
|
||
| /** | ||
| * {@code removeTraitShapes} configuration settings. | ||
| */ | ||
| public static final class Config { | ||
|
|
||
| private String namespace; | ||
| private ShapeId service; | ||
| private Set<String> tags = Collections.emptySet(); | ||
|
|
||
| /** | ||
| * Sets the target namespace that existing namespaces will be flattened | ||
| * into. | ||
| * | ||
| * @param namespace The target namespace to use in the model. | ||
| */ | ||
| public void setNamespace(String namespace) { | ||
| this.namespace = namespace; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the target namespace that existing namespaces will be flattened | ||
| * into. | ||
| * | ||
| * @return the target namespace to be used in the model. | ||
| */ | ||
| public String getNamespace() { | ||
| return namespace; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the service ShapeId that will be flattened into the target | ||
| * namespace. | ||
| * | ||
| * @param service The ID of the service. | ||
| */ | ||
| public void setService(ShapeId service) { | ||
| this.service = service; | ||
| } | ||
|
|
||
| /** | ||
| * @return Gets the service shape ID of the service that will have | ||
| * its shape namespaces updated. | ||
| */ | ||
| public ShapeId getService() { | ||
| return service; | ||
| } | ||
|
|
||
| /** | ||
| * Sets the set of tags that are retained in the model. | ||
| * | ||
| * @param tags The tags to retain in the model. | ||
| */ | ||
| public void setIncludeTagged(Set<String> tags) { | ||
| this.tags = tags; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the set of tags that are retained in the model. | ||
| * | ||
| * @return Returns the tags to retain. | ||
| */ | ||
| public Set<String> getIncludeTagged() { | ||
| return tags; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Class<Config> getConfigType() { | ||
| return Config.class; | ||
| } | ||
|
|
||
| @Override | ||
| protected Model transformWithConfig(TransformContext context, Config config) { | ||
| if (config.getService() == null || config.getNamespace() == null) { | ||
| throw new SmithyBuildException( | ||
| "'namespace' and 'service'properties must be set on flattenNamespace transformer."); | ||
srchase marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| Model model = context.getModel(); | ||
| Map<ShapeId, ShapeId> shapesToRename = getRenamedShapes(config, model); | ||
| return ModelTransformer.create().renameShapes(model, shapesToRename); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "flattenNamespaces"; | ||
| } | ||
|
|
||
| private Map<ShapeId, ShapeId> getRenamedShapes(Config config, Model model) { | ||
| if (!model.getShape(config.getService()).isPresent()) { | ||
| throw new SmithyBuildException("Service not found in model when performing flattenNamespaces transform."); | ||
srchase marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Map<ShapeId, ShapeId> shapesToRename = getRenamedShapesConnectedToService(config, model); | ||
| Set<ShapeId> taggedShapesToInclude = getTaggedShapesToInclude(config.getIncludeTagged(), model); | ||
|
|
||
| for (ShapeId id : taggedShapesToInclude) { | ||
| ShapeId updatedShapeId = updateNamespace(id, config.getNamespace()); | ||
| // If new shape ID already exists in map of shapes to rename, skip | ||
| // including the additional shape to avoid a conflict. | ||
| if (!shapesToRename.containsValue(updatedShapeId)) { | ||
| shapesToRename.put(id, updatedShapeId); | ||
| } | ||
| } | ||
|
|
||
| return shapesToRename; | ||
| } | ||
|
|
||
| private ShapeId updateNamespace(ShapeId shapeId, String namespace) { | ||
| if (shapeId.getMember().isPresent()) { | ||
| return ShapeId.fromParts(namespace, shapeId.getName(), shapeId.getMember().get()); | ||
| } | ||
| return ShapeId.fromParts(namespace, shapeId.getName()); | ||
| } | ||
|
|
||
| private Map<ShapeId, ShapeId> getRenamedShapesConnectedToService(Config config, Model model) { | ||
| Walker shapeWalker = new Walker(model.getKnowledge(NeighborProviderIndex.class, NeighborProviderIndex::new) | ||
srchase marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| .getProvider()); | ||
| ServiceShape service = model.expectShape(config.getService(), ServiceShape.class); | ||
| return shapeWalker.walkShapes(service).stream() | ||
| .filter(FunctionalUtils.not(Prelude::isPreludeShape)) | ||
| .map(shape -> Pair.of(shape.getId(), updateNamespace(shape.getId(), config.getNamespace()))) | ||
| .collect(Collectors.toMap(Pair::getLeft, Pair::getRight)); | ||
| } | ||
|
|
||
| private Set<ShapeId> getTaggedShapesToInclude(Set<String> tags, Model model) { | ||
| return model.shapes() | ||
| .filter(FunctionalUtils.not(Prelude::isPreludeShape)) | ||
| .filter(shape -> isTagged(tags, shape)) | ||
| .map(Shape::getId) | ||
| .collect(Collectors.toSet()); | ||
| } | ||
|
|
||
| private boolean isTagged(Set<String> tags, Shape shape) { | ||
| return shape.getTags().stream().anyMatch(tags::contains); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.