Skip to content

Commit e9ba41e

Browse files
authored
Support additional plugin function parameters (#340)
1 parent f1932e3 commit e9ba41e

File tree

3 files changed

+74
-3
lines changed

3 files changed

+74
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,12 @@ private void addCommandSpecificPlugins() {
248248
// the service's middleware stack.
249249
for (RuntimeClientPlugin plugin : runtimePlugins) {
250250
plugin.getPluginFunction().ifPresent(symbol -> {
251-
writer.write("this.middlewareStack.use($T(configuration));", symbol);
251+
List<String> additionalParameters = plugin.getAdditionalPluginFunctionParameters();
252+
String additionalParamsString = additionalParameters.isEmpty()
253+
? ""
254+
: ", { " + String.join(", ", additionalParameters) + "}";
255+
writer.write("this.middlewareStack.use($T(configuration$L));",
256+
symbol, additionalParamsString);
252257
});
253258
}
254259
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public final class RuntimeClientPlugin implements ToSmithyBuilder<RuntimeClientP
5151
private final SymbolReference resolveFunction;
5252
private final List<String> additionalResolveFunctionParameters;
5353
private final SymbolReference pluginFunction;
54+
private final List<String> additionalPluginFunctionParameters;
5455
private final SymbolReference destroyFunction;
5556
private final BiPredicate<Model, ServiceShape> servicePredicate;
5657
private final OperationPredicate operationPredicate;
@@ -61,6 +62,7 @@ private RuntimeClientPlugin(Builder builder) {
6162
resolveFunction = builder.resolveFunction;
6263
additionalResolveFunctionParameters = ListUtils.copyOf(builder.additionalResolveFunctionParameters);
6364
pluginFunction = builder.pluginFunction;
65+
additionalPluginFunctionParameters = ListUtils.copyOf(builder.additionalPluginFunctionParameters);
6466
destroyFunction = builder.destroyFunction;
6567
operationPredicate = builder.operationPredicate;
6668
servicePredicate = builder.servicePredicate;
@@ -69,6 +71,10 @@ private RuntimeClientPlugin(Builder builder) {
6971
throw new IllegalStateException("Additional parameters can only be set if a resolve function is set.");
7072
}
7173

74+
if (!additionalPluginFunctionParameters.isEmpty() && pluginFunction == null) {
75+
throw new IllegalStateException("Additional parameters can only be set if a plugin function is set.");
76+
}
77+
7278
boolean allNull = (inputConfig == null) && (resolvedConfig == null) && (resolveFunction == null);
7379
boolean allSet = (inputConfig != null) && (resolvedConfig != null) && (resolveFunction != null);
7480
if (!(allNull || allSet)) {
@@ -202,6 +208,19 @@ public Optional<SymbolReference> getPluginFunction() {
202208
return Optional.ofNullable(pluginFunction);
203209
}
204210

211+
/**
212+
* Gets a list of additional parameters to be supplied to the
213+
* plugin function. These parameters are to be supplied to plugin
214+
* function as an options hash. The list is empty if
215+
* there are no additional parameters.
216+
*
217+
* @return Returns the optionally present list of parameters.
218+
* @see #getPluginFunction()
219+
*/
220+
public List<String> getAdditionalPluginFunctionParameters() {
221+
return additionalPluginFunctionParameters;
222+
}
223+
205224
/**
206225
* Gets the optionally present symbol reference that points to the
207226
* function that is used to clean up any resources when a client is
@@ -283,6 +302,7 @@ public String toString() {
283302
+ ", resolveFunction=" + resolveFunction
284303
+ ", additionalResolveFunctionParameters=" + additionalResolveFunctionParameters
285304
+ ", pluginFunction=" + pluginFunction
305+
+ ", additionalPluginFunctionParameters=" + additionalPluginFunctionParameters
286306
+ ", destroyFunction=" + destroyFunction
287307
+ '}';
288308
}
@@ -301,6 +321,7 @@ public boolean equals(Object o) {
301321
&& Objects.equals(resolveFunction, that.resolveFunction)
302322
&& Objects.equals(additionalResolveFunctionParameters, that.additionalResolveFunctionParameters)
303323
&& Objects.equals(pluginFunction, that.pluginFunction)
324+
&& Objects.equals(additionalPluginFunctionParameters, that.additionalPluginFunctionParameters)
304325
&& Objects.equals(destroyFunction, that.destroyFunction)
305326
&& servicePredicate.equals(that.servicePredicate)
306327
&& operationPredicate.equals(that.operationPredicate);
@@ -320,6 +341,7 @@ public static final class Builder implements SmithyBuilder<RuntimeClientPlugin>
320341
private SymbolReference resolveFunction;
321342
private List<String> additionalResolveFunctionParameters = new ArrayList<>();
322343
private SymbolReference pluginFunction;
344+
private List<String> additionalPluginFunctionParameters = new ArrayList<>();
323345
private SymbolReference destroyFunction;
324346
private BiPredicate<Model, ServiceShape> servicePredicate = (model, service) -> true;
325347
private OperationPredicate operationPredicate = (model, service, operation) -> false;
@@ -481,6 +503,21 @@ public Builder pluginFunction(SymbolReference pluginFunction) {
481503
return this;
482504
}
483505

506+
/**
507+
* Sets a function symbol reference used to configure clients and
508+
* commands to use a specific middleware function.
509+
*
510+
* @param pluginFunction Plugin function symbol to invoke.
511+
* @param additionalParameters Additional parameters to be generated as plugin function input.
512+
* @return Returns the builder.
513+
* @see #getPluginFunction()
514+
*/
515+
public Builder pluginFunction(SymbolReference pluginFunction, String... additionalParameters) {
516+
this.pluginFunction = pluginFunction;
517+
this.additionalPluginFunctionParameters = ListUtils.of(additionalParameters);
518+
return this;
519+
}
520+
484521
/**
485522
* Sets a function symbol used to configure clients and commands to
486523
* use a specific middleware function.
@@ -493,6 +530,32 @@ public Builder pluginFunction(Symbol pluginFunction) {
493530
return pluginFunction(SymbolReference.builder().symbol(pluginFunction).build());
494531
}
495532

533+
/**
534+
* Sets a function symbol used to configure clients and commands to
535+
* use a specific middleware function.
536+
*
537+
* @param pluginFunction Plugin function symbol to invoke.
538+
* @param additionalParameters Additional parameters to be generated as plugin function input.
539+
* @return Returns the builder.
540+
* @see #getPluginFunction()
541+
*/
542+
public Builder pluginFunction(Symbol pluginFunction, String... additionalParameters) {
543+
return pluginFunction(SymbolReference.builder().symbol(pluginFunction).build(), additionalParameters);
544+
}
545+
546+
/**
547+
* Set additional positional input parameters to plugin function. Set
548+
* this with no arguments to remove the current parameters.
549+
*
550+
* @param additionalParameters Additional parameters to be generated as plugin function input.
551+
* @return Returns the builder.
552+
* @see #getPluginFunction()
553+
*/
554+
public Builder additionalPluginFunctionParameters(String... additionalParameters) {
555+
this.additionalPluginFunctionParameters = ListUtils.of(additionalParameters);
556+
return this;
557+
}
558+
496559
/**
497560
* Sets a function symbol reference to call from a client in the
498561
* {@code destroy} function of a TypeScript client.

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public void allowsConfigurableServicePredicate() {
6969
public void configuresWithDefaultConventions() {
7070
RuntimeClientPlugin plugin = RuntimeClientPlugin.builder()
7171
.withConventions("foo/baz", "1.0.0", "Foo")
72-
.additionalResolveFunctionParameters("this")
72+
.additionalResolveFunctionParameters("resolveFunctionParam")
73+
.additionalPluginFunctionParameters("pluginFunctionParam")
7374
.build();
7475

7576
assertThat(plugin.getInputConfig().get().getSymbol().getNamespace(), equalTo("foo/baz"));
@@ -81,11 +82,13 @@ public void configuresWithDefaultConventions() {
8182
assertThat(plugin.getResolveFunction().get().getSymbol().getNamespace(), equalTo("foo/baz"));
8283
assertThat(plugin.getResolveFunction().get().getSymbol().getName(), equalTo("resolveFooConfig"));
8384

84-
assertThat(plugin.getAdditionalResolveFunctionParameters(), equalTo(ListUtils.of("this")));
85+
assertThat(plugin.getAdditionalResolveFunctionParameters(), equalTo(ListUtils.of("resolveFunctionParam")));
8586

8687
assertThat(plugin.getPluginFunction().get().getSymbol().getNamespace(), equalTo("foo/baz"));
8788
assertThat(plugin.getPluginFunction().get().getSymbol().getName(), equalTo("getFooPlugin"));
8889

90+
assertThat(plugin.getAdditionalPluginFunctionParameters(), equalTo(ListUtils.of("pluginFunctionParam")));
91+
8992
assertThat(plugin.getInputConfig().get().getSymbol().getDependencies().get(0).getPackageName(),
9093
equalTo("foo/baz"));
9194
assertThat(plugin.getResolvedConfig().get().getSymbol().getDependencies().get(0).getPackageName(),

0 commit comments

Comments
 (0)