Skip to content

Commit 2df620b

Browse files
authored
feat: command classBuilder codegen (#1120)
* feat: command codegen * fix test * update ts pkg * wip * command class builder * fix: add check dependency script * rebase * empty changeset * update unit tests * remove unused import
1 parent 685553f commit 2df620b

File tree

6 files changed

+212
-259
lines changed

6 files changed

+212
-259
lines changed

.changeset/eight-onions-joke.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

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

Lines changed: 162 additions & 234 deletions
Large diffs are not rendered by default.

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2Generator.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
import java.nio.file.Paths;
1919
import java.util.Collections;
20+
import java.util.HashSet;
2021
import java.util.List;
2122
import java.util.Map;
23+
import java.util.Set;
2224
import software.amazon.smithy.codegen.core.SymbolDependency;
2325
import software.amazon.smithy.model.Model;
2426
import software.amazon.smithy.model.node.ObjectNode;
@@ -154,6 +156,34 @@ private void generateEndpointParameters() {
154156
}
155157
);
156158

159+
writer.write("");
160+
161+
writer.openBlock(
162+
"export const commonParams = {", "} as const",
163+
() -> {
164+
RuleSetParameterFinder parameterFinder = new RuleSetParameterFinder(service);
165+
Set<String> paramNames = new HashSet<>();
166+
167+
parameterFinder.getClientContextParams().forEach((name, type) -> {
168+
if (!paramNames.contains(name)) {
169+
writer.write(
170+
"$L: { type: \"clientContextParams\", name: \"$L\" },",
171+
name, EndpointsParamNameMap.getLocalName(name));
172+
}
173+
paramNames.add(name);
174+
});
175+
176+
parameterFinder.getBuiltInParams().forEach((name, type) -> {
177+
if (!paramNames.contains(name)) {
178+
writer.write(
179+
"$L: { type: \"builtInParams\", name: \"$L\" },",
180+
name, EndpointsParamNameMap.getLocalName(name));
181+
}
182+
paramNames.add(name);
183+
});
184+
}
185+
);
186+
157187
writer.write("");
158188
writer.openBlock(
159189
"export interface EndpointParameters extends __EndpointParameters {",

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/CommandGeneratorTest.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,26 @@
1212
public class CommandGeneratorTest {
1313
@Test
1414
public void addsCommandSpecificPlugins() {
15-
testCommmandCodegen("output-structure.smithy",
16-
" resolveMiddleware(\n" +
17-
" clientStack: MiddlewareStack<ServiceInputTypes, ServiceOutputTypes>,\n" +
18-
" configuration: ExampleClientResolvedConfig,\n" +
19-
" options?: __HttpHandlerOptions\n" +
20-
" ): Handler<GetFooCommandInput, GetFooCommandOutput> {\n" +
21-
" this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));\n" +
22-
"\n" +
23-
" const stack = clientStack.concat(this.middlewareStack);");
15+
testCommmandCodegen(
16+
"output-structure.smithy",
17+
"getSerdePlugin(config, this.serialize, this.deserialize)"
18+
);
2419
}
2520

2621
@Test
2722
public void writesSerializer() {
28-
testCommmandCodegen("output-structure.smithy",
29-
" private serialize(\n" +
30-
" input: GetFooCommandInput,\n" +
31-
" context: __SerdeContext\n" +
32-
" ): Promise<__HttpRequest> {");
23+
testCommmandCodegen(
24+
"output-structure.smithy",
25+
".ser("
26+
);
3327
}
3428

3529
@Test
3630
public void writesDeserializer() {
37-
testCommmandCodegen("output-structure.smithy",
38-
" private deserialize(\n" +
39-
" output: __HttpResponse,\n" +
40-
" context: __SerdeContext\n" +
41-
" ): Promise<GetFooCommandOutput> {");
31+
testCommmandCodegen(
32+
"output-structure.smithy",
33+
".de("
34+
);
4235
}
4336

4437
private void testCommmandCodegen(String file, String expectedType) {

smithy-typescript-codegen/src/test/java/software/amazon/smithy/typescript/codegen/endpointsV2/EndpointsV2GeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private MockManifest testEndpoints(String filename) {
8080
.build();
8181

8282
new TypeScriptCodegenPlugin().execute(context);
83-
83+
8484
assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/endpoint/EndpointParameters.ts"),
8585
is(true));
8686
assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/endpoint/endpointResolver.ts"),

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,21 @@ private void testInjects(String filename, String extra) {
6161
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(),
6262
containsString("from \"../middleware/HttpApiKeyAuth\""));
6363
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(),
64-
containsString("this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration" + extra + "));"));
64+
containsString("getHttpApiKeyAuthPlugin(config" + extra + ")"));
6565

6666
// Ensure that the GetBar operation does not import the middleware or use it.
6767
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetBarCommand.ts").get(),
6868
not(containsString("from \"../middleware/HttpApiKeyAuth\"")));
6969
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetBarCommand.ts").get(),
70-
not(containsString("this.middlewareStack.use(getHttpApiKeyAuthPlugin")));
70+
not(containsString("getHttpApiKeyAuthPlugin")));
7171

7272
// Make sure that the middleware file was written and exports the plugin symbol.
7373
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth/index.ts").get(),
7474
containsString("export const getHttpApiKeyAuthPlugin"));
7575

7676
// Ensure that the middleware was being exported in the index file.
7777
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/index.ts").get(),
78-
containsString("from \"./middleware/HttpApiKeyAuth\""));
78+
containsString("from \"./middleware/HttpApiKeyAuth\""));
7979
}
8080

8181
private MockManifest generate(String filename)
@@ -124,14 +124,14 @@ private void testDoesNotInject(String filename) {
124124
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(),
125125
not(containsString("from \"../middleware/HttpApiKeyAuth\"")));
126126
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(),
127-
not(containsString("this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration")));
127+
not(containsString("getHttpApiKeyAuthPlugin(configuration")));
128128

129129
// Make sure that the middleware file was not written.
130130
assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/middleware/HttpApiKeyAuth/index.ts"),
131131
is(false));
132132

133133
// Ensure that the middleware was not being exported in the index file.
134134
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/index.ts").get(),
135-
not(containsString("from \"./middleware/HttpApiKeyAuth\"")));
135+
not(containsString("from \"./middleware/HttpApiKeyAuth\"")));
136136
}
137137
}

0 commit comments

Comments
 (0)