Skip to content
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

Support service renames when flattening namespaces #760

Merged
merged 2 commits into from
Apr 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 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 @@ -894,9 +894,13 @@ namespace. Shapes not connected to a service will not be flattened.
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
tagged with ``baz`` or ``qux`` will also be flattened into the ``ns.foo``
namespace, so long as they don't conflict with a shape within the :ref:`service closure <service-closure>`.
the ``ns.bar#MyService`` service into the target namespace, ``ns.foo``. All
shapes within :ref:`service closure <service-closure>` with be flattened into
the target namespace, including shapes that have been renamed to disambiguate
them through the service shape's ``rename`` property. Shapes tagged with
``baz`` or ``qux`` will also be flattened into the ``ns.foo`` namespace, so
long as they don't conflict with a shape within the
:ref:`service closure <service-closure>`.

.. tabs::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,18 @@ private Map<ShapeId, ShapeId> getRenamedShapesConnectedToService(Config config,
return shapeWalker.walkShapes(service).stream()
.filter(FunctionalUtils.not(Prelude::isPreludeShape))
.map(shape -> Pair.of(shape.getId(), updateNamespace(shape.getId(), config.getNamespace())))
.map(pair -> applyServiceRenames(pair, service))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: ideally we can destructure pairs before calling methods with them. That would give the left and right shape ID a name so it's easier to understand the code.

.collect(Collectors.toMap(Pair::getLeft, Pair::getRight));
}

private Pair<ShapeId, ShapeId> applyServiceRenames(Pair<ShapeId, ShapeId> pair, ServiceShape service) {
if (!service.getRename().containsKey(pair.getLeft())) {
return pair;
}
ShapeId newId = ShapeId.fromParts(pair.getRight().getNamespace(), service.getRename().get(pair.getLeft()));
return Pair.of(pair.getLeft(), newId);
}

private Set<ShapeId> getTaggedShapesToInclude(Set<String> tags, Model model) {
return model.shapes()
.filter(FunctionalUtils.not(Prelude::isPreludeShape))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,33 @@ public void doesNotIncludeAdditionalTaggedShapesWhenTheyConflict() throws Except
"ns.baz#MyOperationOutput$foo", "ns.qux#UnconnectedFromService")));
}

@Test
public void supportsRenamesOnService() throws Exception {
Model model = Model.assembler()
.addImport(Paths.get(getClass().getResource("flatten-namespaces-with-renames.json").toURI()))
.assemble()
.unwrap();
ObjectNode config = Node.objectNode()
.withMember("namespace", Node.from("ns.qux"))
.withMember("service", Node.from("ns.foo#MyService"));
TransformContext context = TransformContext.builder()
.model(model)
.settings(config)
.build();
Model result = new FlattenNamespaces().transform(context);
List<String> ids = result.shapes()
.filter(FunctionalUtils.not(Prelude::isPreludeShape))
.map(Shape::getId)
.map(Object::toString)
.collect(Collectors.toList());

assertThat(ids, containsInAnyOrder("ns.qux#MyService", "ns.qux#GetSomething", "ns.qux#GetSomethingOutput",
"ns.qux#GetSomethingOutput$widget1", "ns.qux#GetSomethingOutput$fooWidget", "ns.qux#Widget",
"ns.qux#FooWidget"));
assertThat(ids, not(containsInAnyOrder("ns.foo#MyService", "ns.foo#GetSomething", "ns.foo#GetSomethingOutput",
"ns.bar#Widget", "foo.example#Widget")));
}

@Test
public void throwsWhenServiceIsNotConfigured() {
Model model = Model.assembler()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"smithy": "1.0",
"shapes": {
"ns.foo#MyService": {
"type": "service",
"version": "2017-02-11",
"operations": [
{
"target": "ns.foo#GetSomething"
}
],
"rename": {
"foo.example#Widget": "FooWidget"
}
},
"ns.foo#GetSomething": {
"type": "operation",
"output": {
"target": "ns.foo#GetSomethingOutput"
}
},
"ns.foo#GetSomethingOutput": {
"type": "structure",
"members": {
"widget1": {
"target": "ns.bar#Widget"
},
"fooWidget": {
"target": "foo.example#Widget"
}
}
},
"ns.bar#Widget": {
"type": "structure"
},
"foo.example#Widget": {
"type": "structure"
}
}
}