Skip to content

Commit 1b4ff49

Browse files
committed
Make runtime client plugins more flexible
Runtime client plugins now refer to each part of the plugin using symbol references rather than just naming conventions. This allows different aspects of the plugins to span different packages and naming formats. Methods are provided on the Builder class for a RuntimeClientPlugin to generate symbol references based on recommended naming conventions (e.g., "getFooPlugin", "resolveFooConfig", "FooInputConfig", "FooResolvedConfig", "destroyFooConfig").
1 parent 171b9c8 commit 1b4ff49

File tree

5 files changed

+730
-72
lines changed

5 files changed

+730
-72
lines changed

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class CodegenVisitor extends ShapeVisitor.Default<Void> {
9292
LOGGER.info(() -> "Adding TypeScriptIntegration: " + integration.getClass().getName());
9393
integrations.add(integration);
9494
integration.getClientPlugins().forEach(runtimePlugin -> {
95-
LOGGER.info(() -> "Adding TypeScript runtime plugin: " + runtimePlugin.getSymbol());
95+
LOGGER.info(() -> "Adding TypeScript runtime plugin: " + runtimePlugin);
9696
runtimePlugins.add(runtimePlugin);
9797
});
9898
});

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ final class CommandGenerator implements Runnable {
6969
this.symbolProvider = symbolProvider;
7070
this.writer = writer;
7171
this.runtimePlugins = runtimePlugins.stream()
72-
.filter(plugin -> plugin.getOperationNames().contains(operation.getId().getName()))
72+
.filter(plugin -> plugin.matchesOperation(model, service, operation))
7373
.collect(Collectors.toList());
7474
this.applicationProtocol = applicationProtocol;
7575

@@ -147,7 +147,7 @@ private void generateCommandMiddlewareResolver(String configType) {
147147
.dedent();
148148
writer.openBlock("): Handler<$L, $L> {", "}", inputType, outputType, () -> {
149149
// Add serialization and deserialization plugin.
150-
writer.write("this.use($T(configuration, this.serialize, this.deserialize));", serde);
150+
writer.write("this.middlewareStack.use($T(configuration, this.serialize, this.deserialize));", serde);
151151

152152
// Add customizations.
153153
addCommandSpecificPlugins();
@@ -188,9 +188,9 @@ private void addCommandSpecificPlugins() {
188188
// applied automatically when the Command's middleware stack is copied from
189189
// the service's middleware stack.
190190
for (RuntimeClientPlugin plugin : runtimePlugins) {
191-
if (plugin.hasMiddleware() && plugin.getOperationNames().contains(operation.getId().getName())) {
192-
writer.write("this.use($T.getMiddleware(configuration));", plugin.getSymbol());
193-
}
191+
plugin.getPluginFunction().ifPresent(symbol -> {
192+
writer.write("this.middlewareStack.use($T(configuration));", symbol);
193+
});
194194
}
195195
}
196196

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ final class ServiceGenerator implements Runnable {
7070
this.writer = writer;
7171
this.runtimePlugins = runtimePlugins.stream()
7272
// Only apply plugins that target the entire client.
73-
.filter(plugin -> plugin.getOperationNames().isEmpty())
73+
.filter(plugin -> plugin.matchesService(model, service))
7474
.collect(Collectors.toList());
7575
this.applicationProtocol = applicationProtocol;
7676

@@ -127,22 +127,21 @@ private void generateConfig() {
127127
// Hook for intercepting the client configuration.
128128
writer.pushState(CLIENT_CONFIG_SECTION);
129129

130+
// The default configuration type is always just the base-level
131+
// Smithy configuration requirements.
132+
writer.write("export type $L = SmithyConfiguration<$T>", configType, applicationProtocol.getOptionsType());
133+
130134
// Get the configuration symbol types to reference in code. These are
131135
// all "&"'d together to create a big configuration type that aggregates
132136
// more modular configuration types.
133-
List<SymbolReference> configTypes = runtimePlugins.stream()
134-
.filter(RuntimeClientPlugin::hasConfig)
135-
.map(RuntimeClientPlugin::getSymbol)
137+
List<SymbolReference> inputTypes = runtimePlugins.stream()
138+
.flatMap(p -> OptionalUtils.stream(p.getInputConfig()))
136139
.collect(Collectors.toList());
137140

138-
// The default configuration type is always just the base-level
139-
// Smithy configuration requirements.
140-
writer.write("export type $L = SmithyConfiguration<$T>", configType, applicationProtocol.getOptionsType());
141-
142-
if (!configTypes.isEmpty()) {
141+
if (!inputTypes.isEmpty()) {
143142
writer.indent();
144-
for (SymbolReference symbolReference : configTypes) {
145-
writer.write("& $T.Input", symbolReference);
143+
for (SymbolReference symbolReference : inputTypes) {
144+
writer.write("& $T", symbolReference);
146145
}
147146
writer.dedent();
148147
}
@@ -152,11 +151,12 @@ private void generateConfig() {
152151
writer.write("");
153152
writer.write("export type $L = SmithyResolvedConfiguration<$T>",
154153
resolvedConfigType, applicationProtocol.getOptionsType());
155-
if (!configTypes.isEmpty()) {
154+
155+
if (!inputTypes.isEmpty()) {
156156
writer.indent();
157-
for (SymbolReference symbolReference : configTypes) {
158-
writer.write("& $T.Resolved", symbolReference);
159-
}
157+
runtimePlugins.stream()
158+
.flatMap(p -> OptionalUtils.stream(p.getResolvedConfig()))
159+
.forEach(symbol -> writer.write("& $T", symbol));
160160
writer.dedent();
161161
}
162162

@@ -200,11 +200,11 @@ private void generateConstructor() {
200200
// configuration is updated, the configuration variable is incremented
201201
// (e.g., _config_0, _config_1, etc).
202202
for (RuntimeClientPlugin plugin : runtimePlugins) {
203-
if (plugin.hasConfig()) {
203+
if (plugin.getResolveFunction().isPresent()) {
204204
configVariable++;
205-
writer.write("let $L = $T.resolve($L);",
205+
writer.write("let $L = $T($L);",
206206
generateConfigVariable(configVariable),
207-
plugin.getSymbol(),
207+
plugin.getResolveFunction().get(),
208208
generateConfigVariable(configVariable - 1));
209209
}
210210
}
@@ -215,9 +215,9 @@ private void generateConstructor() {
215215
// Add runtime plugins that contain middleware to the middleware stack
216216
// of the client.
217217
for (RuntimeClientPlugin plugin : runtimePlugins) {
218-
if (plugin.hasMiddleware()) {
219-
writer.write("super.use($T.getMiddleware(this.config));", plugin.getSymbol());
220-
}
218+
plugin.getPluginFunction().ifPresent(symbol -> {
219+
writer.write("this.middlewareStack.use($T(this.config));", symbol);
220+
});
221221
}
222222

223223
writer.popState();
@@ -234,9 +234,9 @@ private void generateDestroyMethod() {
234234
writer.openBlock("destroy(): void {", "}", () -> {
235235
writer.pushState(CLIENT_DESTROY_SECTION);
236236
for (RuntimeClientPlugin plugin : runtimePlugins) {
237-
if (plugin.hasDestroy()) {
238-
writer.write("$T.destroy(this, this.config);", plugin.getSymbol());
239-
}
237+
plugin.getDestroyFunction().ifPresent(destroy -> {
238+
writer.write("$T(this.config);", destroy);
239+
});
240240
}
241241
writer.popState();
242242
});

0 commit comments

Comments
 (0)