From 2c4a800b54eff926219f2c52d51b910f52ab8b4f Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Mon, 7 Jun 2021 13:27:50 +0000 Subject: [PATCH 1/6] Enable passing dynamic parameters to RuntimeClientPlugin functions This is a requirement for advanced use cases of Endpoint Discovery logic. Currently, only Symbols and strings are generated for parameters. In future, boolean and Map support would be added. --- .../typescript/codegen/CodegenUtils.java | 36 +++++ .../typescript/codegen/CommandGenerator.java | 5 +- .../typescript/codegen/ServiceGenerator.java | 5 +- .../integration/RuntimeClientPlugin.java | 151 +++++++++++------- .../integration/RuntimeClientPluginTest.java | 18 ++- 5 files changed, 153 insertions(+), 62 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java index 8be87ace8bf..36404e95345 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java @@ -17,7 +17,9 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; +import software.amazon.smithy.codegen.core.CodegenException; import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.knowledge.EventStreamIndex; @@ -134,4 +136,38 @@ static void writeStreamingMemberType( + " `%2$s` defined in {@link %1$s}", containerSymbol.getName(), memberName)); writer.write("export interface $1L extends $1LType {}", typeName); } + + /** + * Returns the list of function parameter key-value pairs to be written for + * provided parameters map. + * + * @param paramsMap Map of paramters to generate a parameters string for. + * @return The list of parameters to be written. + */ + static List getFunctionParametersList(Map paramsMap) { + List functionParametersList = new ArrayList(); + + if (!paramsMap.isEmpty()) { + for (Map.Entry param : paramsMap.entrySet()) { + String key = param.getKey(); + Object value = param.getValue(); + if (value instanceof Symbol) { + String symbolName = ((Symbol) value).getName(); + if (key.equals(symbolName)) { + functionParametersList.add(key); + } else { + functionParametersList.add(String.format("%s: %s", key, symbolName)); + } + } else if (value instanceof String) { + functionParametersList.add(String.format("%s: '%s'", key, value)); + } else { + // Future support for param type should be added in else if. + throw new CodegenException("plugin function parameters not supported for type" + + value.toString()); + } + } + } + + return functionParametersList; + } } diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java index f5974648e6d..559e39f00ea 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java @@ -19,6 +19,7 @@ import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeStreamingMemberType; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; import software.amazon.smithy.codegen.core.Symbol; @@ -248,7 +249,9 @@ private void addCommandSpecificPlugins() { // the service's middleware stack. for (RuntimeClientPlugin plugin : runtimePlugins) { plugin.getPluginFunction().ifPresent(symbol -> { - List additionalParameters = plugin.getAdditionalPluginFunctionParameters(); + Map paramsMap = plugin.getPluginFunctionParameters(model, service, operation); + List additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap); + String additionalParamsString = additionalParameters.isEmpty() ? "" : ", { " + String.join(", ", additionalParameters) + "}"; diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java index 1077ed36c89..417be661aed 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java @@ -17,6 +17,7 @@ import java.util.Comparator; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.function.Consumer; @@ -313,7 +314,9 @@ private void generateConstructor() { for (RuntimeClientPlugin plugin : runtimePlugins) { if (plugin.getResolveFunction().isPresent()) { configVariable++; - List additionalParameters = plugin.getAdditionalResolveFunctionParameters(); + Map paramsMap = plugin.getResolveFunctionParameters(model, service, null); + List additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap); + String additionalParamsString = additionalParameters.isEmpty() ? "" : ", " + String.join(", ", additionalParameters); diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java index b4b2977892d..4ec503f56d5 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java @@ -15,8 +15,8 @@ package software.amazon.smithy.typescript.codegen.integration; -import java.util.ArrayList; -import java.util.List; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -28,7 +28,6 @@ import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.typescript.codegen.TypeScriptDependency; -import software.amazon.smithy.utils.ListUtils; import software.amazon.smithy.utils.SmithyBuilder; import software.amazon.smithy.utils.SmithyUnstableApi; import software.amazon.smithy.utils.StringUtils; @@ -49,9 +48,9 @@ public final class RuntimeClientPlugin implements ToSmithyBuilder additionalResolveFunctionParameters; + private final FunctionParamsSupplier resolveFunctionParamsSupplier; private final SymbolReference pluginFunction; - private final List additionalPluginFunctionParameters; + private final FunctionParamsSupplier pluginFunctionParamsSupplier; private final SymbolReference destroyFunction; private final BiPredicate servicePredicate; private final OperationPredicate operationPredicate; @@ -60,21 +59,13 @@ private RuntimeClientPlugin(Builder builder) { inputConfig = builder.inputConfig; resolvedConfig = builder.resolvedConfig; resolveFunction = builder.resolveFunction; - additionalResolveFunctionParameters = ListUtils.copyOf(builder.additionalResolveFunctionParameters); + resolveFunctionParamsSupplier = builder.resolveFunctionParamsSupplier; pluginFunction = builder.pluginFunction; - additionalPluginFunctionParameters = ListUtils.copyOf(builder.additionalPluginFunctionParameters); + pluginFunctionParamsSupplier = builder.pluginFunctionParamsSupplier; destroyFunction = builder.destroyFunction; operationPredicate = builder.operationPredicate; servicePredicate = builder.servicePredicate; - if (!additionalResolveFunctionParameters.isEmpty() && resolveFunction == null) { - 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)) { @@ -102,6 +93,19 @@ public interface OperationPredicate { boolean test(Model model, ServiceShape service, OperationShape operation); } + @FunctionalInterface + public interface FunctionParamsSupplier { + /** + * Returns dynamic parameters for resolve function. + * + * @param model Model the operation belongs to. + * @param service Service the operation belongs to. + * @param operation Operation to test. + * @return Returns true if middleware should be applied to the operation. + */ + Map apply(Model model, ServiceShape service, OperationShape operation); + } + /** * Gets the optionally present symbol reference that points to the * Input configuration interface for the plugin. @@ -163,7 +167,6 @@ public Optional getResolvedConfig() { * * @return Returns the optionally present resolve function. * @see #getInputConfig() - * @see #getAdditionalResolveFunctionParameters() * @see #getResolvedConfig() */ public Optional getResolveFunction() { @@ -173,14 +176,23 @@ public Optional getResolveFunction() { /** * Gets a list of additional parameters to be supplied to the * resolve function. These parameters are to be supplied to resolve - * function as Nth(N > 1) positional arguments. The list is empty if - * there are no additional parameters. + * function as second argument. The map is empty if there are + * no additional parameters. * - * @return Returns the optionally present list of parameters. - * @see #getResolveFunction() + * @param model Model the operation belongs to. + * @param service Service the operation belongs to. + * @param operation Operation to test against. + * @return Returns the optionally present map of parameters. */ - public List getAdditionalResolveFunctionParameters() { - return additionalResolveFunctionParameters; + public Map getResolveFunctionParameters( + Model model, + ServiceShape service, + OperationShape operation + ) { + if (resolveFunctionParamsSupplier != null) { + return resolveFunctionParamsSupplier.apply(model, service, operation); + } + return new HashMap(); } /** @@ -211,14 +223,23 @@ public Optional getPluginFunction() { /** * 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. + * function as second argument. The map is empty if there are + * no additional parameters. * - * @return Returns the optionally present list of parameters. - * @see #getPluginFunction() + * @param model Model the operation belongs to. + * @param service Service the operation belongs to. + * @param operation Operation to test against. + * @return Returns the optionally present map of parameters. */ - public List getAdditionalPluginFunctionParameters() { - return additionalPluginFunctionParameters; + public Map getPluginFunctionParameters( + Model model, + ServiceShape service, + OperationShape operation + ) { + if (pluginFunctionParamsSupplier != null) { + return pluginFunctionParamsSupplier.apply(model, service, operation); + } + return new HashMap(); } /** @@ -284,7 +305,9 @@ public Builder toBuilder() { .inputConfig(inputConfig) .resolvedConfig(resolvedConfig) .resolveFunction(resolveFunction) + .resolveFunctionParamsSupplier(resolveFunctionParamsSupplier) .pluginFunction(pluginFunction) + .pluginFunctionParamsSupplier(pluginFunctionParamsSupplier) .destroyFunction(destroyFunction); // Set these directly since their setters have mutual side-effects. @@ -300,9 +323,7 @@ public String toString() { + "inputConfig=" + inputConfig + ", resolvedConfig=" + resolvedConfig + ", resolveFunction=" + resolveFunction - + ", additionalResolveFunctionParameters=" + additionalResolveFunctionParameters + ", pluginFunction=" + pluginFunction - + ", additionalPluginFunctionParameters=" + additionalPluginFunctionParameters + ", destroyFunction=" + destroyFunction + '}'; } @@ -319,9 +340,9 @@ public boolean equals(Object o) { return Objects.equals(inputConfig, that.inputConfig) && Objects.equals(resolvedConfig, that.resolvedConfig) && Objects.equals(resolveFunction, that.resolveFunction) - && Objects.equals(additionalResolveFunctionParameters, that.additionalResolveFunctionParameters) + && Objects.equals(resolveFunctionParamsSupplier, that.resolveFunctionParamsSupplier) && Objects.equals(pluginFunction, that.pluginFunction) - && Objects.equals(additionalPluginFunctionParameters, that.additionalPluginFunctionParameters) + && Objects.equals(pluginFunctionParamsSupplier, that.pluginFunctionParamsSupplier) && Objects.equals(destroyFunction, that.destroyFunction) && servicePredicate.equals(that.servicePredicate) && operationPredicate.equals(that.operationPredicate); @@ -339,9 +360,9 @@ public static final class Builder implements SmithyBuilder private SymbolReference inputConfig; private SymbolReference resolvedConfig; private SymbolReference resolveFunction; - private List additionalResolveFunctionParameters = new ArrayList<>(); + private FunctionParamsSupplier resolveFunctionParamsSupplier; private SymbolReference pluginFunction; - private List additionalPluginFunctionParameters = new ArrayList<>(); + private FunctionParamsSupplier pluginFunctionParamsSupplier; private SymbolReference destroyFunction; private BiPredicate servicePredicate = (model, service) -> true; private OperationPredicate operationPredicate = (model, service, operation) -> false; @@ -433,13 +454,16 @@ public Builder resolveFunction(SymbolReference resolveFunction) { * {@link #inputConfig} must also be set. * * @param resolveFunction Function used to convert input to resolved. - * @param additionalParameters Additional parameters to be generated as resolve function input. + * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input. * @return Returns the builder. * @see #getResolveFunction() */ - public Builder resolveFunction(SymbolReference resolveFunction, String... additionalParameters) { + public Builder resolveFunction( + SymbolReference resolveFunction, + FunctionParamsSupplier resolveFunctionParamsSupplier + ) { this.resolveFunction = resolveFunction; - this.additionalResolveFunctionParameters = ListUtils.of(additionalParameters); + this.resolveFunctionParamsSupplier = resolveFunctionParamsSupplier; return this; } @@ -466,27 +490,33 @@ public Builder resolveFunction(Symbol resolveFunction) { * {@link #inputConfig} must also be set. * * @param resolveFunction Function used to convert input to resolved. - * @param additionalParameters Additional parameters to be generated as resolve function input. + * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input. * @return Returns the builder. * @see #getResolveFunction() */ - public Builder resolveFunction(Symbol resolveFunction, String... additionalParameters) { - return resolveFunction(SymbolReference.builder().symbol(resolveFunction).build(), additionalParameters); + public Builder resolveFunction( + Symbol resolveFunction, + FunctionParamsSupplier resolveFunctionParamsSupplier + ) { + return resolveFunction( + SymbolReference.builder().symbol(resolveFunction).build(), + resolveFunctionParamsSupplier + ); } /** - * Set additional positional input parameters to resolve function. Set - * this with no arguments to remove the current parameters. + * Set function which returns input parameters to resolve function. Set + * function to return empty map to remove the current parameters. * *

If this is set, then all of {@link #resolveFunction}, * {@link #resolvedConfig} and {@link #inputConfig} must also be set. * - * @param additionalParameters Additional parameters to be generated as resolve function input. + * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input. * @return Returns the builder. * @see #getResolveFunction() */ - public Builder additionalResolveFunctionParameters(String... additionalParameters) { - this.additionalResolveFunctionParameters = ListUtils.of(additionalParameters); + public Builder resolveFunctionParamsSupplier(FunctionParamsSupplier resolveFunctionParamsSupplier) { + this.resolveFunctionParamsSupplier = resolveFunctionParamsSupplier; return this; } @@ -508,13 +538,16 @@ public Builder pluginFunction(SymbolReference pluginFunction) { * 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. + * @param pluginFunctionParamsSupplier Function which returns params to be passed as plugin function input. * @return Returns the builder. * @see #getPluginFunction() */ - public Builder pluginFunction(SymbolReference pluginFunction, String... additionalParameters) { + public Builder pluginFunction( + SymbolReference pluginFunction, + FunctionParamsSupplier pluginFunctionParamsSupplier + ) { this.pluginFunction = pluginFunction; - this.additionalPluginFunctionParameters = ListUtils.of(additionalParameters); + this.pluginFunctionParamsSupplier = pluginFunctionParamsSupplier; return this; } @@ -535,24 +568,30 @@ public Builder pluginFunction(Symbol pluginFunction) { * use a specific middleware function. * * @param pluginFunction Plugin function symbol to invoke. - * @param additionalParameters Additional parameters to be generated as plugin function input. + * @param pluginFunctionParamsSupplier Function which returns params to be passed 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); + public Builder pluginFunction( + Symbol pluginFunction, + FunctionParamsSupplier pluginFunctionParamsSupplier + ) { + return pluginFunction( + SymbolReference.builder().symbol(pluginFunction).build(), + pluginFunctionParamsSupplier + ); } /** - * Set additional positional input parameters to plugin function. Set - * this with no arguments to remove the current parameters. + * Set function which returns input parameters to plugin function. Set + * function to return empty map to remove the current parameters. * - * @param additionalParameters Additional parameters to be generated as plugin function input. + * @param pluginFunctionParamsSupplier Function which returns params to be passed as plugin function input. * @return Returns the builder. * @see #getPluginFunction() */ - public Builder additionalPluginFunctionParameters(String... additionalParameters) { - this.additionalPluginFunctionParameters = ListUtils.of(additionalParameters); + public Builder pluginFunctionParamsSupplier(FunctionParamsSupplier pluginFunctionParamsSupplier) { + this.pluginFunctionParamsSupplier = pluginFunctionParamsSupplier; return this; } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java index 3bc88d0dd70..0eb06ddd453 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java @@ -3,6 +3,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; +import java.util.HashMap; +import java.util.Map; + import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import software.amazon.smithy.codegen.core.Symbol; @@ -67,10 +70,17 @@ public void allowsConfigurableServicePredicate() { @Test public void configuresWithDefaultConventions() { + Map resolveFunctionParams = new HashMap() {{ + put("resolveFunctionParam", "resolveFunctionParam"); + }}; + Map pluginFunctionParams = new HashMap() {{ + put("pluginFunctionParam", "pluginFunctionParam"); + }}; + RuntimeClientPlugin plugin = RuntimeClientPlugin.builder() .withConventions("foo/baz", "1.0.0", "Foo") - .additionalResolveFunctionParameters("resolveFunctionParam") - .additionalPluginFunctionParameters("pluginFunctionParam") + .resolveFunctionParamsSupplier((m, s, o) -> resolveFunctionParams) + .pluginFunctionParamsSupplier((m, s, o) -> pluginFunctionParams) .build(); assertThat(plugin.getInputConfig().get().getSymbol().getNamespace(), equalTo("foo/baz")); @@ -82,12 +92,12 @@ 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("resolveFunctionParam"))); + assertThat(plugin.getResolveFunctionParameters(null, null, null), equalTo(resolveFunctionParams)); 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.getPluginFunctionParameters(null, null, null), equalTo(pluginFunctionParams)); assertThat(plugin.getInputConfig().get().getSymbol().getDependencies().get(0).getPackageName(), equalTo("foo/baz")); From 3c6255b014f96321b228bf54fde666e7b52efec7 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 24 Jun 2021 20:41:39 +0000 Subject: [PATCH 2/6] chore: print getClass() in CodegenException --- .../amazon/smithy/typescript/codegen/CodegenUtils.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java index 36404e95345..094334ba314 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java @@ -162,8 +162,8 @@ static List getFunctionParametersList(Map paramsMap) { functionParametersList.add(String.format("%s: '%s'", key, value)); } else { // Future support for param type should be added in else if. - throw new CodegenException("plugin function parameters not supported for type" - + value.toString()); + throw new CodegenException("Plugin function parameters not supported for type " + + value.getClass()); } } } From b8e3683e0eb09ad6f604a5e14afe47cd848a1e67 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 24 Jun 2021 20:48:49 +0000 Subject: [PATCH 3/6] docs: FunctionParamsSupplier returns map of parameters --- .../typescript/codegen/integration/RuntimeClientPlugin.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java index 4ec503f56d5..d212a5e4b85 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java @@ -96,12 +96,12 @@ public interface OperationPredicate { @FunctionalInterface public interface FunctionParamsSupplier { /** - * Returns dynamic parameters for resolve function. + * Returns parameters to be passed to a function which can be computed dynamically. * * @param model Model the operation belongs to. * @param service Service the operation belongs to. * @param operation Operation to test. - * @return Returns true if middleware should be applied to the operation. + * @return Returns the map of parameters to be passed to a function. */ Map apply(Model model, ServiceShape service, OperationShape operation); } From 043774aeaf64a4f0c0fd6d540f5665c462f071dd Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 24 Jun 2021 20:58:22 +0000 Subject: [PATCH 4/6] docs: add details for key and value of FunctionParamsSupplier --- .../codegen/integration/RuntimeClientPlugin.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java index d212a5e4b85..2233d4b2511 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java @@ -101,7 +101,8 @@ public interface FunctionParamsSupplier { * @param model Model the operation belongs to. * @param service Service the operation belongs to. * @param operation Operation to test. - * @return Returns the map of parameters to be passed to a function. + * @return Returns the map of parameters to be passed to a function. The key is the key + * for a parameter, and value is the value for a parameter. */ Map apply(Model model, ServiceShape service, OperationShape operation); } @@ -182,7 +183,8 @@ public Optional getResolveFunction() { * @param model Model the operation belongs to. * @param service Service the operation belongs to. * @param operation Operation to test against. - * @return Returns the optionally present map of parameters. + * @return Returns the optionally present map of parameters. The key is the key + * for a parameter, and value is the value for a parameter. */ public Map getResolveFunctionParameters( Model model, @@ -229,7 +231,8 @@ public Optional getPluginFunction() { * @param model Model the operation belongs to. * @param service Service the operation belongs to. * @param operation Operation to test against. - * @return Returns the optionally present map of parameters. + * @return Returns the optionally present map of parameters. The key is the key + * for a parameter, and value is the value for a parameter. */ public Map getPluginFunctionParameters( Model model, From 482ea044081dd2fe40e89ced5ea03bb030f46c25 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 24 Jun 2021 21:29:04 +0000 Subject: [PATCH 5/6] chore: reintroduce additional for function params --- .../amazon/smithy/typescript/codegen/CommandGenerator.java | 3 ++- .../amazon/smithy/typescript/codegen/ServiceGenerator.java | 3 ++- .../typescript/codegen/integration/RuntimeClientPlugin.java | 4 ++-- .../codegen/integration/RuntimeClientPluginTest.java | 6 ++++-- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java index 559e39f00ea..15df0b388af 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java @@ -249,7 +249,8 @@ private void addCommandSpecificPlugins() { // the service's middleware stack. for (RuntimeClientPlugin plugin : runtimePlugins) { plugin.getPluginFunction().ifPresent(symbol -> { - Map paramsMap = plugin.getPluginFunctionParameters(model, service, operation); + Map paramsMap = plugin.getAdditionalPluginFunctionParameters( + model, service, operation); List additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap); String additionalParamsString = additionalParameters.isEmpty() diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java index 417be661aed..4d6d85cb221 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/ServiceGenerator.java @@ -314,7 +314,8 @@ private void generateConstructor() { for (RuntimeClientPlugin plugin : runtimePlugins) { if (plugin.getResolveFunction().isPresent()) { configVariable++; - Map paramsMap = plugin.getResolveFunctionParameters(model, service, null); + Map paramsMap = plugin.getAdditionalResolveFunctionParameters( + model, service, null); List additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap); String additionalParamsString = additionalParameters.isEmpty() diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java index 2233d4b2511..9b2648dc108 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java @@ -186,7 +186,7 @@ public Optional getResolveFunction() { * @return Returns the optionally present map of parameters. The key is the key * for a parameter, and value is the value for a parameter. */ - public Map getResolveFunctionParameters( + public Map getAdditionalResolveFunctionParameters( Model model, ServiceShape service, OperationShape operation @@ -234,7 +234,7 @@ public Optional getPluginFunction() { * @return Returns the optionally present map of parameters. The key is the key * for a parameter, and value is the value for a parameter. */ - public Map getPluginFunctionParameters( + public Map getAdditionalPluginFunctionParameters( Model model, ServiceShape service, OperationShape operation diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java index 0eb06ddd453..965579e6a0a 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java @@ -92,12 +92,14 @@ public void configuresWithDefaultConventions() { assertThat(plugin.getResolveFunction().get().getSymbol().getNamespace(), equalTo("foo/baz")); assertThat(plugin.getResolveFunction().get().getSymbol().getName(), equalTo("resolveFooConfig")); - assertThat(plugin.getResolveFunctionParameters(null, null, null), equalTo(resolveFunctionParams)); + assertThat(plugin.getAdditionalResolveFunctionParameters(null, null, null), + equalTo(resolveFunctionParams)); assertThat(plugin.getPluginFunction().get().getSymbol().getNamespace(), equalTo("foo/baz")); assertThat(plugin.getPluginFunction().get().getSymbol().getName(), equalTo("getFooPlugin")); - assertThat(plugin.getPluginFunctionParameters(null, null, null), equalTo(pluginFunctionParams)); + assertThat(plugin.getAdditionalPluginFunctionParameters(null, null, null), + equalTo(pluginFunctionParams)); assertThat(plugin.getInputConfig().get().getSymbol().getDependencies().get(0).getPackageName(), equalTo("foo/baz")); From 015d5e4da24d2d3f8892af9f17768db3f70107e3 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 24 Jun 2021 22:01:29 +0000 Subject: [PATCH 6/6] chore: add additional prefix for other params --- .../integration/RuntimeClientPlugin.java | 69 +++++++++++-------- .../integration/RuntimeClientPluginTest.java | 4 +- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java index 9b2648dc108..a89d81c67df 100644 --- a/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java +++ b/smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPlugin.java @@ -48,9 +48,9 @@ public final class RuntimeClientPlugin implements ToSmithyBuilder servicePredicate; private final OperationPredicate operationPredicate; @@ -59,9 +59,9 @@ private RuntimeClientPlugin(Builder builder) { inputConfig = builder.inputConfig; resolvedConfig = builder.resolvedConfig; resolveFunction = builder.resolveFunction; - resolveFunctionParamsSupplier = builder.resolveFunctionParamsSupplier; + additionalResolveFunctionParamsSupplier = builder.additionalResolveFunctionParamsSupplier; pluginFunction = builder.pluginFunction; - pluginFunctionParamsSupplier = builder.pluginFunctionParamsSupplier; + additionalPluginFunctionParamsSupplier = builder.additionalPluginFunctionParamsSupplier; destroyFunction = builder.destroyFunction; operationPredicate = builder.operationPredicate; servicePredicate = builder.servicePredicate; @@ -191,8 +191,8 @@ public Map getAdditionalResolveFunctionParameters( ServiceShape service, OperationShape operation ) { - if (resolveFunctionParamsSupplier != null) { - return resolveFunctionParamsSupplier.apply(model, service, operation); + if (additionalResolveFunctionParamsSupplier != null) { + return additionalResolveFunctionParamsSupplier.apply(model, service, operation); } return new HashMap(); } @@ -239,8 +239,8 @@ public Map getAdditionalPluginFunctionParameters( ServiceShape service, OperationShape operation ) { - if (pluginFunctionParamsSupplier != null) { - return pluginFunctionParamsSupplier.apply(model, service, operation); + if (additionalPluginFunctionParamsSupplier != null) { + return additionalPluginFunctionParamsSupplier.apply(model, service, operation); } return new HashMap(); } @@ -308,9 +308,9 @@ public Builder toBuilder() { .inputConfig(inputConfig) .resolvedConfig(resolvedConfig) .resolveFunction(resolveFunction) - .resolveFunctionParamsSupplier(resolveFunctionParamsSupplier) + .additionalResolveFunctionParamsSupplier(additionalResolveFunctionParamsSupplier) .pluginFunction(pluginFunction) - .pluginFunctionParamsSupplier(pluginFunctionParamsSupplier) + .additionalPluginFunctionParamsSupplier(additionalPluginFunctionParamsSupplier) .destroyFunction(destroyFunction); // Set these directly since their setters have mutual side-effects. @@ -343,9 +343,9 @@ public boolean equals(Object o) { return Objects.equals(inputConfig, that.inputConfig) && Objects.equals(resolvedConfig, that.resolvedConfig) && Objects.equals(resolveFunction, that.resolveFunction) - && Objects.equals(resolveFunctionParamsSupplier, that.resolveFunctionParamsSupplier) + && Objects.equals(additionalResolveFunctionParamsSupplier, that.additionalResolveFunctionParamsSupplier) && Objects.equals(pluginFunction, that.pluginFunction) - && Objects.equals(pluginFunctionParamsSupplier, that.pluginFunctionParamsSupplier) + && Objects.equals(additionalPluginFunctionParamsSupplier, that.additionalPluginFunctionParamsSupplier) && Objects.equals(destroyFunction, that.destroyFunction) && servicePredicate.equals(that.servicePredicate) && operationPredicate.equals(that.operationPredicate); @@ -363,9 +363,9 @@ public static final class Builder implements SmithyBuilder private SymbolReference inputConfig; private SymbolReference resolvedConfig; private SymbolReference resolveFunction; - private FunctionParamsSupplier resolveFunctionParamsSupplier; + private FunctionParamsSupplier additionalResolveFunctionParamsSupplier; private SymbolReference pluginFunction; - private FunctionParamsSupplier pluginFunctionParamsSupplier; + private FunctionParamsSupplier additionalPluginFunctionParamsSupplier; private SymbolReference destroyFunction; private BiPredicate servicePredicate = (model, service) -> true; private OperationPredicate operationPredicate = (model, service, operation) -> false; @@ -457,16 +457,17 @@ public Builder resolveFunction(SymbolReference resolveFunction) { * {@link #inputConfig} must also be set. * * @param resolveFunction Function used to convert input to resolved. - * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input. + * @param additionalResolveFunctionParamsSupplier Function which returns params to be passed + * as resolve function input. * @return Returns the builder. * @see #getResolveFunction() */ public Builder resolveFunction( SymbolReference resolveFunction, - FunctionParamsSupplier resolveFunctionParamsSupplier + FunctionParamsSupplier additionalResolveFunctionParamsSupplier ) { this.resolveFunction = resolveFunction; - this.resolveFunctionParamsSupplier = resolveFunctionParamsSupplier; + this.additionalResolveFunctionParamsSupplier = additionalResolveFunctionParamsSupplier; return this; } @@ -493,17 +494,18 @@ public Builder resolveFunction(Symbol resolveFunction) { * {@link #inputConfig} must also be set. * * @param resolveFunction Function used to convert input to resolved. - * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input. + * @param additionalResolveFunctionParamsSupplier Function which returns params to be passed + * as resolve function input. * @return Returns the builder. * @see #getResolveFunction() */ public Builder resolveFunction( Symbol resolveFunction, - FunctionParamsSupplier resolveFunctionParamsSupplier + FunctionParamsSupplier additionalResolveFunctionParamsSupplier ) { return resolveFunction( SymbolReference.builder().symbol(resolveFunction).build(), - resolveFunctionParamsSupplier + additionalResolveFunctionParamsSupplier ); } @@ -514,12 +516,15 @@ public Builder resolveFunction( *

If this is set, then all of {@link #resolveFunction}, * {@link #resolvedConfig} and {@link #inputConfig} must also be set. * - * @param resolveFunctionParamsSupplier Function which returns params to be passed as resolve function input. + * @param additionalResolveFunctionParamsSupplier Function which returns params to be passed + * as resolve function input. * @return Returns the builder. * @see #getResolveFunction() */ - public Builder resolveFunctionParamsSupplier(FunctionParamsSupplier resolveFunctionParamsSupplier) { - this.resolveFunctionParamsSupplier = resolveFunctionParamsSupplier; + public Builder additionalResolveFunctionParamsSupplier( + FunctionParamsSupplier additionalResolveFunctionParamsSupplier + ) { + this.additionalResolveFunctionParamsSupplier = additionalResolveFunctionParamsSupplier; return this; } @@ -550,7 +555,7 @@ public Builder pluginFunction( FunctionParamsSupplier pluginFunctionParamsSupplier ) { this.pluginFunction = pluginFunction; - this.pluginFunctionParamsSupplier = pluginFunctionParamsSupplier; + this.additionalPluginFunctionParamsSupplier = pluginFunctionParamsSupplier; return this; } @@ -571,17 +576,18 @@ public Builder pluginFunction(Symbol pluginFunction) { * use a specific middleware function. * * @param pluginFunction Plugin function symbol to invoke. - * @param pluginFunctionParamsSupplier Function which returns params to be passed as plugin function input. + * @param additionalPluginFunctionParamsSupplier Function which returns params to be passed + * as plugin function input. * @return Returns the builder. * @see #getPluginFunction() */ public Builder pluginFunction( Symbol pluginFunction, - FunctionParamsSupplier pluginFunctionParamsSupplier + FunctionParamsSupplier additionalPluginFunctionParamsSupplier ) { return pluginFunction( SymbolReference.builder().symbol(pluginFunction).build(), - pluginFunctionParamsSupplier + additionalPluginFunctionParamsSupplier ); } @@ -589,12 +595,15 @@ public Builder pluginFunction( * Set function which returns input parameters to plugin function. Set * function to return empty map to remove the current parameters. * - * @param pluginFunctionParamsSupplier Function which returns params to be passed as plugin function input. + * @param additionalPluginFunctionParamsSupplier Function which returns params to be passed + * as plugin function input. * @return Returns the builder. * @see #getPluginFunction() */ - public Builder pluginFunctionParamsSupplier(FunctionParamsSupplier pluginFunctionParamsSupplier) { - this.pluginFunctionParamsSupplier = pluginFunctionParamsSupplier; + public Builder additionalPluginFunctionParamsSupplier( + FunctionParamsSupplier additionalPluginFunctionParamsSupplier + ) { + this.additionalPluginFunctionParamsSupplier = additionalPluginFunctionParamsSupplier; return this; } diff --git a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java index 965579e6a0a..ef92cbeaf3f 100644 --- a/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java +++ b/smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/integration/RuntimeClientPluginTest.java @@ -79,8 +79,8 @@ public void configuresWithDefaultConventions() { RuntimeClientPlugin plugin = RuntimeClientPlugin.builder() .withConventions("foo/baz", "1.0.0", "Foo") - .resolveFunctionParamsSupplier((m, s, o) -> resolveFunctionParams) - .pluginFunctionParamsSupplier((m, s, o) -> pluginFunctionParams) + .additionalResolveFunctionParamsSupplier((m, s, o) -> resolveFunctionParams) + .additionalPluginFunctionParamsSupplier((m, s, o) -> pluginFunctionParams) .build(); assertThat(plugin.getInputConfig().get().getSymbol().getNamespace(), equalTo("foo/baz"));