Skip to content

Commit b392816

Browse files
committed
code style
1 parent cf374d6 commit b392816

File tree

5 files changed

+106
-79
lines changed

5 files changed

+106
-79
lines changed

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Optional;
3131
import java.util.Set;
3232
import java.util.TreeSet;
33+
import java.util.function.Consumer;
3334
import java.util.function.Function;
3435
import java.util.stream.Collectors;
3536
import software.amazon.smithy.build.FileManifest;
@@ -491,21 +492,21 @@ private void addCommandSpecificPlugins() {
491492
if (additionalParameters.isEmpty() && clientAddParamsWriterConsumers.isEmpty()) {
492493
return;
493494
}
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(",");
495+
writer.openBlock(", { ", " }", () -> {
496+
writer.writeInline(String.join(", ", additionalParameters));
497+
clientAddParamsWriterConsumers.forEach((key, consumer) -> {
498+
writer.writeInline("$L: $C,", key, (Consumer<TypeScriptWriter>) (w -> {
499+
consumer.accept(w, CommandConstructorCodeSection.builder()
500+
.settings(settings)
501+
.model(model)
502+
.service(service)
503+
.symbolProvider(symbolProvider)
504+
.runtimeClientPlugins(runtimePlugins)
505+
.applicationProtocol(applicationProtocol)
506+
.build());
507+
}));
508+
});
507509
});
508-
writer.writeInline(" }");
509510
});
510511
writer.popState();
511512
});

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public TypeScriptCodegenContext createContext(CreateContextDirective<TypeScriptS
103103
});
104104

105105
directive.integrations().forEach(integration -> {
106-
LOGGER.info(() -> "Mutating plugins from TypeScriptIntegration: " + integration.getClass().getName());
106+
LOGGER.info(() -> "Mutating plugins from TypeScriptIntegration: " + integration.name());
107107
integration.mutateClientPlugins(runtimePlugins);
108108
});
109109

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

+15-16
Original file line numberDiff line numberDiff line change
@@ -459,27 +459,26 @@ private void generateConstructor() {
459459
return;
460460
}
461461

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(",");
462+
writer.openBlock(", {", " }", () -> {
463+
writer.writeInline(String.join(", ", additionalParameters));
464+
clientAddParamsWriterConsumers.forEach((key, consumer) -> {
465+
writer.writeInline("$L: $C,", key, (Consumer<TypeScriptWriter>) (w -> {
466+
consumer.accept(w, 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+
}));
476+
});
476477
});
477-
writer.writeInline("}");
478478
});
479479
writer.popState();
480480
});
481481
}
482-
483482
writer.popState();
484483
});
485484
}

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

+68-43
Original file line numberDiff line numberDiff line change
@@ -32,6 +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;
3536
import software.amazon.smithy.typescript.codegen.util.ClientWriterConsumer;
3637
import software.amazon.smithy.utils.SmithyInternalApi;
3738

@@ -51,49 +52,8 @@ public boolean matchesSettings(TypeScriptSettings settings) {
5152
@Override
5253
public List<RuntimeClientPlugin> getClientPlugins() {
5354
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-
}
55+
"httpAuthSchemeParametersProvider", AddHttpAuthSchemePlugin::httpAuthSchemeParametersProvider,
56+
"identityProviderConfigProvider", AddHttpAuthSchemePlugin::identityProviderConfigProvider
9757
);
9858
return List.of(
9959
RuntimeClientPlugin.builder()
@@ -182,6 +142,71 @@ public void customize(TypeScriptCodegenContext codegenContext) {
182142
});
183143
}
184144

145+
/**
146+
* Writes the httpAuthSchemeParametersProvider for input to middleware additional parameters.
147+
* Example:
148+
* ```typescript
149+
* defaultWeatherHttpAuthSchemeParametersProvider;
150+
* ```
151+
*/
152+
private static void httpAuthSchemeParametersProvider(TypeScriptWriter w,
153+
ClientBodyExtraCodeSection clientBodySection) {
154+
String httpAuthSchemeParametersProviderName = "default"
155+
+ CodegenUtils.getServiceName(
156+
clientBodySection.getSettings(),
157+
clientBodySection.getModel(),
158+
clientBodySection.getSymbolProvider()
159+
)
160+
+ "HttpAuthSchemeParametersProvider";
161+
w.addImport(httpAuthSchemeParametersProviderName, null, AuthUtils.AUTH_HTTP_PROVIDER_DEPENDENCY);
162+
w.writeInline(httpAuthSchemeParametersProviderName);
163+
}
164+
165+
/**
166+
* Writes the identityProviderConfigProvider for input to middleware additional parameters.
167+
* Example:
168+
* ```typescript
169+
* async (config: WeatherClientResolvedConfig) => new DefaultIdentityProviderConfig({
170+
* "aws.auth#sigv4": config.credentials,
171+
* "smithy.api#httpApiKeyAuth": config.apiKey,
172+
* "smithy.api#httpBearerAuth": config.token,
173+
* })
174+
* ```
175+
*/
176+
private static void identityProviderConfigProvider(TypeScriptWriter w,
177+
ClientBodyExtraCodeSection s) {
178+
w.addDependency(TypeScriptDependency.SMITHY_CORE);
179+
w.addImport("DefaultIdentityProviderConfig", null, TypeScriptDependency.SMITHY_CORE);
180+
w.openBlock("""
181+
async (config: $LResolvedConfig) => \
182+
new DefaultIdentityProviderConfig({""", "})",
183+
s.getSymbolProvider().toSymbol(s.getService()).getName(),
184+
() -> {
185+
SupportedHttpAuthSchemesIndex authIndex = new SupportedHttpAuthSchemesIndex(
186+
s.getIntegrations(),
187+
s.getModel(),
188+
s.getSettings());
189+
ServiceIndex serviceIndex = ServiceIndex.of(s.getModel());
190+
TopDownIndex topDownIndex = TopDownIndex.of(s.getModel());
191+
Map<ShapeId, HttpAuthScheme> httpAuthSchemes = AuthUtils.getAllEffectiveNoAuthAwareAuthSchemes(
192+
s.getService(), serviceIndex, authIndex, topDownIndex);
193+
for (HttpAuthScheme scheme : httpAuthSchemes.values()) {
194+
if (scheme == null) {
195+
continue;
196+
}
197+
for (ConfigField configField : scheme.getConfigFields()) {
198+
if (configField.type().equals(ConfigField.Type.MAIN)) {
199+
w.writeInline(
200+
"$S: config.$L,",
201+
scheme.getSchemeId().toString(),
202+
configField.name()
203+
);
204+
}
205+
}
206+
}
207+
});
208+
}
209+
185210
/*
186211
export interface HttpAuthSchemeInputConfig {
187212
httpAuthSchemes?: HttpAuthScheme[];

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ public class AddHttpApiKeyAuthPluginTest {
3232
@Test
3333
public void httpApiKeyAuthClientOnService() {
3434
testInjects("http-api-key-auth-trait.smithy",
35-
", { in: 'header', name: 'Authorization', scheme: 'ApiKey' }");
35+
"in: 'header', name: 'Authorization', scheme: 'ApiKey'");
3636
}
3737

3838
@Test
3939
public void httpApiKeyAuthClientOnOperation() {
4040
testInjects("http-api-key-auth-trait-on-operation.smithy",
41-
", { in: 'header', name: 'Authorization', scheme: 'ApiKey' }");
41+
"in: 'header', name: 'Authorization', scheme: 'ApiKey'");
4242
}
4343

4444
// This should be identical to the httpApiKeyAuthClient test except for the parameters provided
4545
// to the middleware.
4646
@Test
4747
public void httpApiKeyAuthClientNoScheme() {
48-
testInjects("http-api-key-auth-trait-no-scheme.smithy", ", { in: 'header', name: 'Authorization' }");
48+
testInjects("http-api-key-auth-trait-no-scheme.smithy", "in: 'header', name: 'Authorization'");
4949
}
5050

5151
private void testInjects(String filename, String extra) {
@@ -60,8 +60,10 @@ private void testInjects(String filename, String extra) {
6060
// Ensure that the GetFoo operation imports the middleware and uses it with all the options.
6161
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(),
6262
containsString("from \"../middleware/HttpApiKeyAuth\""));
63-
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(),
64-
containsString("getHttpApiKeyAuthPlugin(config\n " + extra));
63+
64+
String generatedGetFooCommand = manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get();
65+
assertThat(generatedGetFooCommand, containsString("getHttpApiKeyAuthPlugin(config"));
66+
assertThat(generatedGetFooCommand, containsString(extra));
6567

6668
// Ensure that the GetBar operation does not import the middleware or use it.
6769
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetBarCommand.ts").get(),

0 commit comments

Comments
 (0)