Skip to content

Commit cf374d6

Browse files
committed
feat: enable writers for plugin parameters
1 parent fd61cd3 commit cf374d6

File tree

8 files changed

+203
-148
lines changed

8 files changed

+203
-148
lines changed

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

+25-6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import software.amazon.smithy.typescript.codegen.sections.CommandPropertiesCodeSection;
5959
import software.amazon.smithy.typescript.codegen.sections.PreCommandClassCodeSection;
6060
import software.amazon.smithy.typescript.codegen.sections.SmithyContextCodeSection;
61+
import software.amazon.smithy.typescript.codegen.util.CommandWriterConsumer;
6162
import software.amazon.smithy.typescript.codegen.validation.SensitiveDataFinder;
6263
import software.amazon.smithy.utils.SmithyInternalApi;
6364

@@ -467,15 +468,11 @@ private void addCommandSpecificPlugins() {
467468
// applied automatically when the Command's middleware stack is copied from
468469
// the service's middleware stack.
469470
for (RuntimeClientPlugin plugin : runtimePlugins) {
470-
plugin.getWriterConsumer().accept(writer);
471471
plugin.getPluginFunction().ifPresent(pluginSymbol -> {
472472
// Construct additional parameters string
473473
Map<String, Object> paramsMap = plugin.getAdditionalPluginFunctionParameters(
474474
model, service, operation);
475-
List<String> additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap);
476-
String additionalParamsString = additionalParameters.isEmpty()
477-
? ""
478-
: ", { " + String.join(", ", additionalParameters) + " }";
475+
479476

480477
// Construct writer context
481478
Map<String, Object> symbolMap = new HashMap<>();
@@ -487,7 +484,29 @@ private void addCommandSpecificPlugins() {
487484
}
488485
writer.pushState();
489486
writer.putContext(symbolMap);
490-
writer.write("$pluginFn:T(config" + additionalParamsString + "),");
487+
writer.openBlock("$pluginFn:T(config", "),", () -> {
488+
List<String> additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap);
489+
Map<String, CommandWriterConsumer> clientAddParamsWriterConsumers =
490+
plugin.getOperationAddParamsWriterConsumers();
491+
if (additionalParameters.isEmpty() && clientAddParamsWriterConsumers.isEmpty()) {
492+
return;
493+
}
494+
writer.writeInline(", { ");
495+
writer.writeInline(String.join(", ", additionalParameters));
496+
clientAddParamsWriterConsumers.forEach((key, consumer) -> {
497+
writer.writeInline(key).writeInline(": ");
498+
consumer.accept(writer, CommandConstructorCodeSection.builder()
499+
.settings(settings)
500+
.model(model)
501+
.service(service)
502+
.symbolProvider(symbolProvider)
503+
.runtimeClientPlugins(runtimePlugins)
504+
.applicationProtocol(applicationProtocol)
505+
.build());
506+
writer.writeInline(",");
507+
});
508+
writer.writeInline(" }");
509+
});
491510
writer.popState();
492511
});
493512
}

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

+34-16
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import software.amazon.smithy.typescript.codegen.sections.ClientConstructorCodeSection;
4242
import software.amazon.smithy.typescript.codegen.sections.ClientDestroyCodeSection;
4343
import software.amazon.smithy.typescript.codegen.sections.ClientPropertiesCodeSection;
44+
import software.amazon.smithy.typescript.codegen.util.ClientWriterConsumer;
4445
import software.amazon.smithy.utils.OptionalUtils;
4546
import software.amazon.smithy.utils.SmithyInternalApi;
4647

@@ -63,13 +64,13 @@ public final class ServiceBareBonesClientGenerator implements Runnable {
6364
private final ApplicationProtocol applicationProtocol;
6465

6566
ServiceBareBonesClientGenerator(
66-
TypeScriptSettings settings,
67-
Model model,
68-
SymbolProvider symbolProvider,
69-
TypeScriptWriter writer,
70-
List<TypeScriptIntegration> integrations,
71-
List<RuntimeClientPlugin> runtimePlugins,
72-
ApplicationProtocol applicationProtocol
67+
TypeScriptSettings settings,
68+
Model model,
69+
SymbolProvider symbolProvider,
70+
TypeScriptWriter writer,
71+
List<TypeScriptIntegration> integrations,
72+
List<RuntimeClientPlugin> runtimePlugins,
73+
ApplicationProtocol applicationProtocol
7374
) {
7475
this.settings = settings;
7576
this.model = model;
@@ -387,10 +388,6 @@ private void generateConstructor() {
387388
generateConfigVariable(configVariable - 1));
388389
}
389390

390-
for (RuntimeClientPlugin plugin : runtimePlugins) {
391-
plugin.getWriterConsumer().accept(writer);
392-
}
393-
394391
// Add runtime plugin "resolve" method calls. These are invoked one
395392
// after the other until all of the runtime plugins have been called.
396393
// Only plugins that have configuration are called. Each time the
@@ -442,10 +439,6 @@ private void generateConstructor() {
442439
// Construct additional parameters string
443440
Map<String, Object> paramsMap = plugin.getAdditionalPluginFunctionParameters(
444441
model, service, null);
445-
List<String> additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap);
446-
String additionalParamsString = additionalParameters.isEmpty()
447-
? ""
448-
: ", { " + String.join(", ", additionalParameters) + " }";
449442

450443
// Construct writer context
451444
Map<String, Object> symbolMap = new HashMap<>();
@@ -457,7 +450,32 @@ private void generateConstructor() {
457450
}
458451
writer.pushState();
459452
writer.putContext(symbolMap);
460-
writer.write("this.middlewareStack.use($pluginFn:T(this.config" + additionalParamsString + "));");
453+
writer.openBlock("this.middlewareStack.use($pluginFn:T(this.config", "));", () -> {
454+
List<String> additionalParameters = CodegenUtils.getFunctionParametersList(paramsMap);
455+
Map<String, ClientWriterConsumer> clientAddParamsWriterConsumers =
456+
plugin.getClientAddParamsWriterConsumers();
457+
458+
if (additionalParameters.isEmpty() && clientAddParamsWriterConsumers.isEmpty()) {
459+
return;
460+
}
461+
462+
writer.writeInline(", {");
463+
writer.writeInline(String.join(", ", additionalParameters));
464+
clientAddParamsWriterConsumers.forEach((key, consumer) -> {
465+
writer.writeInline(key).writeInline(": ");
466+
consumer.accept(writer, ClientBodyExtraCodeSection.builder()
467+
.settings(settings)
468+
.model(model)
469+
.service(service)
470+
.symbolProvider(symbolProvider)
471+
.integrations(integrations)
472+
.runtimeClientPlugins(runtimePlugins)
473+
.applicationProtocol(applicationProtocol)
474+
.build());
475+
writer.writeInline(",");
476+
});
477+
writer.writeInline("}");
478+
});
461479
writer.popState();
462480
});
463481
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/auth/http/integration/AddHttpAuthSchemePlugin.java

+48-97
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@
3232
import software.amazon.smithy.typescript.codegen.auth.http.sections.ResolveHttpAuthSchemeConfigFunctionReturnBlockCodeSection;
3333
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
3434
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin.Convention;
35-
import software.amazon.smithy.typescript.codegen.sections.ClientBodyExtraCodeSection;
36-
import software.amazon.smithy.utils.CodeInterceptor;
37-
import software.amazon.smithy.utils.CodeSection;
35+
import software.amazon.smithy.typescript.codegen.util.ClientWriterConsumer;
3836
import software.amazon.smithy.utils.SmithyInternalApi;
3937

4038
/**
@@ -52,42 +50,67 @@ public boolean matchesSettings(TypeScriptSettings settings) {
5250

5351
@Override
5452
public List<RuntimeClientPlugin> getClientPlugins() {
53+
Map<String, ClientWriterConsumer> httpAuthSchemeParametersProvider = Map.of(
54+
"httpAuthSchemeParametersProvider", (w, clientBodySection) -> {
55+
String httpAuthSchemeParametersProviderName = "default"
56+
+ CodegenUtils.getServiceName(
57+
clientBodySection.getSettings(),
58+
clientBodySection.getModel(),
59+
clientBodySection.getSymbolProvider()
60+
)
61+
+ "HttpAuthSchemeParametersProvider";
62+
w.addImport(httpAuthSchemeParametersProviderName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY);
63+
w.writeInline(httpAuthSchemeParametersProviderName);
64+
},
65+
"identityProviderConfigProvider", (w, s) -> {
66+
w.addDependency(TypeScriptDependency.SMITHY_CORE);
67+
w.addImport("DefaultIdentityProviderConfig", null, TypeScriptDependency.SMITHY_CORE);
68+
w.openBlock("""
69+
async (config: $LResolvedConfig) => \
70+
new DefaultIdentityProviderConfig({""", "})",
71+
s.getSymbolProvider().toSymbol(s.getService()).getName(),
72+
() -> {
73+
SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(
74+
s.getIntegrations(),
75+
s.getModel(),
76+
s.getSettings());
77+
ServiceIndex serviceIndex = ServiceIndex.of(s.getModel());
78+
TopDownIndex topDownIndex = TopDownIndex.of(s.getModel());
79+
Map<ShapeId, HttpAuthScheme> httpAuthSchemes = AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(
80+
s.getService(), serviceIndex, authIndex, topDownIndex);
81+
for (HttpAuthScheme scheme : httpAuthSchemes.values()) {
82+
if (scheme == null) {
83+
continue;
84+
}
85+
for (ConfigField configField : scheme.getConfigFields()) {
86+
if (configField.type().equals(ConfigField.Type.MAIN)) {
87+
w.writeInline(
88+
"$S: config.$L,",
89+
scheme.getSchemeId().toString(),
90+
configField.name()
91+
);
92+
}
93+
}
94+
}
95+
});
96+
}
97+
);
5598
return List.of(
5699
RuntimeClientPlugin.builder()
57100
.servicePredicate((m, s) -> s.hasTrait(EndpointRuleSetTrait.ID))
58101
.withConventions(
59102
TypeScriptDependency.SMITHY_CORE.dependency,
60103
"HttpAuthSchemeEndpointRuleSet",
61104
Convention.HAS_MIDDLEWARE)
62-
.additionalPluginFunctionParamsSupplier((model, service, operation) -> Map.of(
63-
"httpAuthSchemeParametersProvider", Symbol.builder()
64-
.name("this.getDefaultHttpAuthSchemeParametersProvider()")
65-
.build(),
66-
"identityProviderConfigProvider", Symbol.builder()
67-
.name("this.getIdentityProviderConfigProvider()")
68-
.build()
69-
))
70-
.withWriter(writer -> {
71-
String httpAuthSchemeParametersProviderName = "default"
72-
+ CodegenUtils.getServiceName(s.getSettings(), s.getModel(), s.getSymbolProvider())
73-
+ "HttpAuthSchemeParametersProvider";
74-
w.addImport(httpAuthSchemeParametersProviderName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY);
75-
})
105+
.withAdditionalClientParams(httpAuthSchemeParametersProvider)
76106
.build(),
77107
RuntimeClientPlugin.builder()
78108
.servicePredicate((m, s) -> !s.hasTrait(EndpointRuleSetTrait.ID))
79109
.withConventions(
80110
TypeScriptDependency.SMITHY_CORE.dependency,
81111
"HttpAuthScheme",
82112
Convention.HAS_MIDDLEWARE)
83-
.additionalPluginFunctionParamsSupplier((model, service, operation) -> Map.of(
84-
"httpAuthSchemeParametersProvider", Symbol.builder()
85-
.name("this.getDefaultHttpAuthSchemeParametersProvider()")
86-
.build(),
87-
"identityProviderConfigProvider", Symbol.builder()
88-
.name("this.getIdentityProviderConfigProvider()")
89-
.build()
90-
))
113+
.withAdditionalClientParams(httpAuthSchemeParametersProvider)
91114
.build(),
92115
RuntimeClientPlugin.builder()
93116
.inputConfig(Symbol.builder()
@@ -106,70 +129,6 @@ public List<RuntimeClientPlugin> getClientPlugins() {
106129
);
107130
}
108131

109-
@Override
110-
public List<? extends CodeInterceptor<? extends CodeSection, TypeScriptWriter>> interceptors(
111-
TypeScriptCodegenContext codegenContext
112-
) {
113-
return List.of(CodeInterceptor.appender(ClientBodyExtraCodeSection.class, (w, s) -> {
114-
if (!s.getSettings().generateClient()
115-
|| s.getSettings().useLegacyAuth()
116-
|| !s.getApplicationProtocol().isHttpProtocol()) {
117-
return;
118-
}
119-
120-
/*
121-
private getDefaultHttpAuthSchemeParametersProvider() {
122-
return defaultWeatherHttpAuthSchemeParametersProvider;
123-
}
124-
*/
125-
w.openBlock("private getDefaultHttpAuthSchemeParametersProvider() {", "}", () -> {
126-
String httpAuthSchemeParametersProviderName = "default"
127-
+ CodegenUtils.getServiceName(s.getSettings(), s.getModel(), s.getSymbolProvider())
128-
+ "HttpAuthSchemeParametersProvider";
129-
w.addImport(httpAuthSchemeParametersProviderName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY);
130-
w.write("return " + httpAuthSchemeParametersProviderName + ";");
131-
});
132-
133-
/*
134-
private getIdentityProviderConfigProvider() {
135-
return async (config: WeatherClientResolvedConfig) => new DefaultIdentityProviderConfig({
136-
"aws.auth#sigv4": config.credentials,
137-
"smithy.api#httpApiKeyAuth": config.apiKey,
138-
"smithy.api#httpBearerAuth": config.token,
139-
});
140-
}
141-
*/
142-
w.openBlock("private getIdentityProviderConfigProvider() {", "}", () -> {
143-
w.addDependency(TypeScriptDependency.SMITHY_CORE);
144-
w.addImport("DefaultIdentityProviderConfig", null, TypeScriptDependency.SMITHY_CORE);
145-
w.openBlock("""
146-
return async (config: $LResolvedConfig) => \
147-
new DefaultIdentityProviderConfig({""", "});",
148-
s.getSymbolProvider().toSymbol(s.getService()).getName(),
149-
() -> {
150-
SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(
151-
s.getIntegrations(),
152-
s.getModel(),
153-
s.getSettings());
154-
ServiceIndex serviceIndex = ServiceIndex.of(s.getModel());
155-
TopDownIndex topDownIndex = TopDownIndex.of(s.getModel());
156-
Map<ShapeId, HttpAuthScheme> httpAuthSchemes = AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(
157-
s.getService(), serviceIndex, authIndex, topDownIndex);
158-
for (HttpAuthScheme scheme : httpAuthSchemes.values()) {
159-
if (scheme == null) {
160-
continue;
161-
}
162-
for (ConfigField configField : scheme.getConfigFields()) {
163-
if (configField.type().equals(ConfigField.Type.MAIN)) {
164-
w.write("$S: config.$L,", scheme.getSchemeId().toString(), configField.name());
165-
}
166-
}
167-
}
168-
});
169-
});
170-
}));
171-
}
172-
173132
@Override
174133
public void customize(TypeScriptCodegenContext codegenContext) {
175134
if (!codegenContext.settings().generateClient()
@@ -223,14 +182,6 @@ public void customize(TypeScriptCodegenContext codegenContext) {
223182
});
224183
}
225184

226-
private String inlineHttpAuthSchemeParametersProvider() {
227-
return "";
228-
}
229-
230-
private String inlineIdentityProviderConfigProvider() {
231-
return "";
232-
}
233-
234185
/*
235186
export interface HttpAuthSchemeInputConfig {
236187
httpAuthSchemes?: HttpAuthScheme[];

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

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
@SmithyInternalApi
4545
public final class AddEventStreamDependency implements TypeScriptIntegration {
4646

47+
@Override
48+
public List<String> runAfter() {
49+
return List.of(
50+
AddBuiltinPlugins.class.getCanonicalName()
51+
);
52+
}
53+
4754
@Override
4855
public List<RuntimeClientPlugin> getClientPlugins() {
4956
return ListUtils.of(

0 commit comments

Comments
 (0)