Skip to content

Support additional plugin function parameters #340

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
merged 3 commits into from
May 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,12 @@ private void addCommandSpecificPlugins() {
// the service's middleware stack.
for (RuntimeClientPlugin plugin : runtimePlugins) {
plugin.getPluginFunction().ifPresent(symbol -> {
writer.write("this.middlewareStack.use($T(configuration));", symbol);
List<String> additionalParameters = plugin.getAdditionalPluginFunctionParameters();
String additionalParamsString = additionalParameters.isEmpty()
? ""
: ", { " + String.join(", ", additionalParameters) + "}";
writer.write("this.middlewareStack.use($T(configuration$L));",
symbol, additionalParamsString);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public final class RuntimeClientPlugin implements ToSmithyBuilder<RuntimeClientP
private final SymbolReference resolveFunction;
private final List<String> additionalResolveFunctionParameters;
private final SymbolReference pluginFunction;
private final List<String> additionalPluginFunctionParameters;
private final SymbolReference destroyFunction;
private final BiPredicate<Model, ServiceShape> servicePredicate;
private final OperationPredicate operationPredicate;
Expand All @@ -61,6 +62,7 @@ private RuntimeClientPlugin(Builder builder) {
resolveFunction = builder.resolveFunction;
additionalResolveFunctionParameters = ListUtils.copyOf(builder.additionalResolveFunctionParameters);
pluginFunction = builder.pluginFunction;
additionalPluginFunctionParameters = ListUtils.copyOf(builder.additionalPluginFunctionParameters);
destroyFunction = builder.destroyFunction;
operationPredicate = builder.operationPredicate;
servicePredicate = builder.servicePredicate;
Expand All @@ -69,6 +71,10 @@ private RuntimeClientPlugin(Builder builder) {
throw new IllegalStateException("Additional parameters can only be set if a resolve function is set.");
}

if (!additionalPluginFunctionParameters.isEmpty() && pluginFunction == null) {
throw new IllegalStateException("Additional parameters can only be set if a plugin function is set.");
}

boolean allNull = (inputConfig == null) && (resolvedConfig == null) && (resolveFunction == null);
boolean allSet = (inputConfig != null) && (resolvedConfig != null) && (resolveFunction != null);
if (!(allNull || allSet)) {
Expand Down Expand Up @@ -202,6 +208,19 @@ public Optional<SymbolReference> getPluginFunction() {
return Optional.ofNullable(pluginFunction);
}

/**
* Gets a list of additional parameters to be supplied to the
* plugin function. These parameters are to be supplied to plugin
* function as an options hash. The list is empty if
* there are no additional parameters.
*
* @return Returns the optionally present list of parameters.
* @see #getPluginFunction()
*/
public List<String> getAdditionalPluginFunctionParameters() {
return additionalPluginFunctionParameters;
}

/**
* Gets the optionally present symbol reference that points to the
* function that is used to clean up any resources when a client is
Expand Down Expand Up @@ -283,6 +302,7 @@ public String toString() {
+ ", resolveFunction=" + resolveFunction
+ ", additionalResolveFunctionParameters=" + additionalResolveFunctionParameters
+ ", pluginFunction=" + pluginFunction
+ ", additionalPluginFunctionParameters=" + additionalPluginFunctionParameters
+ ", destroyFunction=" + destroyFunction
+ '}';
}
Expand All @@ -301,6 +321,7 @@ public boolean equals(Object o) {
&& Objects.equals(resolveFunction, that.resolveFunction)
&& Objects.equals(additionalResolveFunctionParameters, that.additionalResolveFunctionParameters)
&& Objects.equals(pluginFunction, that.pluginFunction)
&& Objects.equals(additionalPluginFunctionParameters, that.additionalPluginFunctionParameters)
&& Objects.equals(destroyFunction, that.destroyFunction)
&& servicePredicate.equals(that.servicePredicate)
&& operationPredicate.equals(that.operationPredicate);
Expand All @@ -320,6 +341,7 @@ public static final class Builder implements SmithyBuilder<RuntimeClientPlugin>
private SymbolReference resolveFunction;
private List<String> additionalResolveFunctionParameters = new ArrayList<>();
private SymbolReference pluginFunction;
private List<String> additionalPluginFunctionParameters = new ArrayList<>();
private SymbolReference destroyFunction;
private BiPredicate<Model, ServiceShape> servicePredicate = (model, service) -> true;
private OperationPredicate operationPredicate = (model, service, operation) -> false;
Expand Down Expand Up @@ -481,6 +503,21 @@ public Builder pluginFunction(SymbolReference pluginFunction) {
return this;
}

/**
* Sets a function symbol reference used to configure clients and
* commands to use a specific middleware function.
*
* @param pluginFunction Plugin function symbol to invoke.
* @param additionalParameters Additional parameters to be generated as plugin function input.
* @return Returns the builder.
* @see #getPluginFunction()
*/
public Builder pluginFunction(SymbolReference pluginFunction, String... additionalParameters) {
this.pluginFunction = pluginFunction;
this.additionalPluginFunctionParameters = ListUtils.of(additionalParameters);
return this;
}

/**
* Sets a function symbol used to configure clients and commands to
* use a specific middleware function.
Expand All @@ -493,6 +530,32 @@ public Builder pluginFunction(Symbol pluginFunction) {
return pluginFunction(SymbolReference.builder().symbol(pluginFunction).build());
}

/**
* Sets a function symbol used to configure clients and commands to
* use a specific middleware function.
*
* @param pluginFunction Plugin function symbol to invoke.
* @param additionalParameters Additional parameters to be generated as plugin function input.
* @return Returns the builder.
* @see #getPluginFunction()
*/
public Builder pluginFunction(Symbol pluginFunction, String... additionalParameters) {
return pluginFunction(SymbolReference.builder().symbol(pluginFunction).build(), additionalParameters);
}

/**
* Set additional positional input parameters to plugin function. Set
* this with no arguments to remove the current parameters.
*
* @param additionalParameters Additional parameters to be generated as plugin function input.
* @return Returns the builder.
* @see #getPluginFunction()
*/
public Builder additionalPluginFunctionParameters(String... additionalParameters) {
this.additionalPluginFunctionParameters = ListUtils.of(additionalParameters);
return this;
}

/**
* Sets a function symbol reference to call from a client in the
* {@code destroy} function of a TypeScript client.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public void allowsConfigurableServicePredicate() {
public void configuresWithDefaultConventions() {
RuntimeClientPlugin plugin = RuntimeClientPlugin.builder()
.withConventions("foo/baz", "1.0.0", "Foo")
.additionalResolveFunctionParameters("this")
.additionalResolveFunctionParameters("resolveFunctionParam")
.additionalPluginFunctionParameters("pluginFunctionParam")
.build();

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

assertThat(plugin.getAdditionalResolveFunctionParameters(), equalTo(ListUtils.of("this")));
assertThat(plugin.getAdditionalResolveFunctionParameters(), equalTo(ListUtils.of("resolveFunctionParam")));

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

assertThat(plugin.getAdditionalPluginFunctionParameters(), equalTo(ListUtils.of("pluginFunctionParam")));

assertThat(plugin.getInputConfig().get().getSymbol().getDependencies().get(0).getPackageName(),
equalTo("foo/baz"));
assertThat(plugin.getResolvedConfig().get().getSymbol().getDependencies().get(0).getPackageName(),
Expand Down