Skip to content

Commit

Permalink
Throw when service is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
Chase Coalwell committed Sep 28, 2020
1 parent 33259f8 commit fe2c03d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
7 changes: 4 additions & 3 deletions docs/source/1.0/guides/building-models/build-config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,16 @@ namespace. Shapes not connected to a service will not be flattened.
- **REQUIRED** The target namespace.
* - service
- ``shapeId``
- **REQUIRED** The service to be flattened.
- **REQUIRED** The service to be flattened. All shapes within this
:ref:`service closure <service-closure>` will be replaced with equivalent
shapes in the target namespace.
* - includeTagged
- ``[string]``
- The set of tags that, if found on a shape not connected to the service,
forces the shape to have its namespace flattened into the target
namespace. When additional shapes are included, the shapes are replaced
entirely, along with any references to the shapes which may exist within
separate :ref:`service closure <service-closure>`.

separate :ref:`service closures <service-closure>`.

The following example will flatten the namespaces of the shapes connected to
the ``ns.bar#MyService`` service into the target namespace, ``ns.foo``. Shapes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public Class<Config> getConfigType() {
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.");
"'namespace' and 'service' properties must be set on flattenNamespace transformer.");
}
Model model = context.getModel();
Map<ShapeId, ShapeId> shapesToRename = getRenamedShapes(config, model);
Expand All @@ -132,7 +132,8 @@ public String getName() {

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.");
throw new SmithyBuildException("Configured service, " + config.getService()
+ ", not found in model when performing flattenNamespaces transform.");
}

Map<ShapeId, ShapeId> shapesToRename = getRenamedShapesConnectedToService(config, model);
Expand All @@ -158,8 +159,7 @@ private ShapeId updateNamespace(ShapeId shapeId, String namespace) {
}

private Map<ShapeId, ShapeId> getRenamedShapesConnectedToService(Config config, Model model) {
Walker shapeWalker = new Walker(model.getKnowledge(NeighborProviderIndex.class, NeighborProviderIndex::new)
.getProvider());
Walker shapeWalker = new Walker(NeighborProviderIndex.of(model).getProvider());
ServiceShape service = model.expectShape(config.getService(), ServiceShape.class);
return shapeWalker.walkShapes(service).stream()
.filter(FunctionalUtils.not(Prelude::isPreludeShape))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.smithy.build.plugins.SourcesPlugin;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.loader.Prelude;
import software.amazon.smithy.model.node.ExpectationNotMetException;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.ObjectNode;
import software.amazon.smithy.model.shapes.Shape;
Expand Down Expand Up @@ -161,4 +162,20 @@ public void throwsWhenServiceCannotBeFoundInModel() {
.build();
Assertions.assertThrows(SmithyBuildException.class, () -> new FlattenNamespaces().transform(context));
}

@Test
public void throwsWhenServiceIsInvalidInModel() {
Model model = Model.assembler()
.addUnparsedModel("N/A", "{ \"smithy\": \"1.0\", \"shapes\": { \"ns.foo#InvalidService\": { \"type\": \"string\" } } }")
.assemble()
.unwrap();
ObjectNode config = Node.objectNode()
.withMember("namespace", Node.from("ns.qux"))
.withMember("service", Node.from("ns.foo#InvalidService"));
TransformContext context = TransformContext.builder()
.model(model)
.settings(config)
.build();
Assertions.assertThrows(ExpectationNotMetException.class, () -> new FlattenNamespaces().transform(context));
}
}

0 comments on commit fe2c03d

Please sign in to comment.