Skip to content

Commit c3afb19

Browse files
trivikrsrchase
authored andcommitted
Enable passing dynamic parameters to RuntimeClientPlugin functions (smithy-lang#353)
1 parent aa8bf70 commit c3afb19

File tree

5 files changed

+169
-62
lines changed

5 files changed

+169
-62
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
20+
import java.util.Map;
2021
import java.util.stream.Collectors;
22+
import software.amazon.smithy.codegen.core.CodegenException;
2123
import software.amazon.smithy.codegen.core.Symbol;
2224
import software.amazon.smithy.model.Model;
2325
import software.amazon.smithy.model.knowledge.EventStreamIndex;
@@ -130,6 +132,40 @@ static void writeStreamingMemberType(
130132
memberName, optionalSuffix, containerSymbol));
131133
}
132134

135+
/**
136+
* Returns the list of function parameter key-value pairs to be written for
137+
* provided parameters map.
138+
*
139+
* @param paramsMap Map of paramters to generate a parameters string for.
140+
* @return The list of parameters to be written.
141+
*/
142+
static List<String> getFunctionParametersList(Map<String, Object> paramsMap) {
143+
List<String> functionParametersList = new ArrayList<String>();
144+
145+
if (!paramsMap.isEmpty()) {
146+
for (Map.Entry<String, Object> param : paramsMap.entrySet()) {
147+
String key = param.getKey();
148+
Object value = param.getValue();
149+
if (value instanceof Symbol) {
150+
String symbolName = ((Symbol) value).getName();
151+
if (key.equals(symbolName)) {
152+
functionParametersList.add(key);
153+
} else {
154+
functionParametersList.add(String.format("%s: %s", key, symbolName));
155+
}
156+
} else if (value instanceof String) {
157+
functionParametersList.add(String.format("%s: '%s'", key, value));
158+
} else {
159+
// Future support for param type should be added in else if.
160+
throw new CodegenException("Plugin function parameters not supported for type "
161+
+ value.getClass());
162+
}
163+
}
164+
}
165+
166+
return functionParametersList;
167+
}
168+
133169
/**
134170
* Ease the input streaming member restriction so that users don't need to construct a stream every time.
135171
* This is used for inline type declarations (such as parameters) that need to take more permissive inputs

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeStreamingMemberType;
2020

2121
import java.util.List;
22+
import java.util.Map;
2223
import java.util.Optional;
2324
import java.util.stream.Collectors;
2425
import software.amazon.smithy.codegen.core.Symbol;
@@ -248,7 +249,10 @@ private void addCommandSpecificPlugins() {
248249
// the service's middleware stack.
249250
for (RuntimeClientPlugin plugin : runtimePlugins) {
250251
plugin.getPluginFunction().ifPresent(symbol -> {
251-
List<String> additionalParameters = plugin.getAdditionalPluginFunctionParameters();
252+
Map<String, Object> paramsMap = plugin.getAdditionalPluginFunctionParameters(
253+
model, service, operation);
254+
List<String> additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap);
255+
252256
String additionalParamsString = additionalParameters.isEmpty()
253257
? ""
254258
: ", { " + String.join(", ", additionalParameters) + "}";

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Comparator;
1919
import java.util.List;
20+
import java.util.Map;
2021
import java.util.Optional;
2122
import java.util.Set;
2223
import java.util.function.Consumer;
@@ -313,7 +314,10 @@ private void generateConstructor() {
313314
for (RuntimeClientPlugin plugin : runtimePlugins) {
314315
if (plugin.getResolveFunction().isPresent()) {
315316
configVariable++;
316-
List<String> additionalParameters = plugin.getAdditionalResolveFunctionParameters();
317+
Map<String, Object> paramsMap = plugin.getAdditionalResolveFunctionParameters(
318+
model, service, null);
319+
List<String> additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap);
320+
317321
String additionalParamsString = additionalParameters.isEmpty()
318322
? ""
319323
: ", " + String.join(", ", additionalParameters);

0 commit comments

Comments
 (0)