Skip to content

Commit

Permalink
feat: command classBuilder codegen (#1120)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kuhe authored Dec 22, 2023
1 parent 685553f commit 2df620b
Show file tree
Hide file tree
Showing 6 changed files with 212 additions and 259 deletions.
2 changes: 2 additions & 0 deletions .changeset/eight-onions-joke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import software.amazon.smithy.codegen.core.SymbolDependency;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.node.ObjectNode;
Expand Down Expand Up @@ -154,6 +156,34 @@ private void generateEndpointParameters() {
}
);

writer.write("");

writer.openBlock(
"export const commonParams = {", "} as const",
() -> {
RuleSetParameterFinder parameterFinder = new RuleSetParameterFinder(service);
Set<String> paramNames = new HashSet<>();

parameterFinder.getClientContextParams().forEach((name, type) -> {
if (!paramNames.contains(name)) {
writer.write(
"$L: { type: \"clientContextParams\", name: \"$L\" },",
name, EndpointsParamNameMap.getLocalName(name));
}
paramNames.add(name);
});

parameterFinder.getBuiltInParams().forEach((name, type) -> {
if (!paramNames.contains(name)) {
writer.write(
"$L: { type: \"builtInParams\", name: \"$L\" },",
name, EndpointsParamNameMap.getLocalName(name));
}
paramNames.add(name);
});
}
);

writer.write("");
writer.openBlock(
"export interface EndpointParameters extends __EndpointParameters {",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,26 @@
public class CommandGeneratorTest {
@Test
public void addsCommandSpecificPlugins() {
testCommmandCodegen("output-structure.smithy",
" resolveMiddleware(\n" +
" clientStack: MiddlewareStack<ServiceInputTypes, ServiceOutputTypes>,\n" +
" configuration: ExampleClientResolvedConfig,\n" +
" options?: __HttpHandlerOptions\n" +
" ): Handler<GetFooCommandInput, GetFooCommandOutput> {\n" +
" this.middlewareStack.use(getSerdePlugin(configuration, this.serialize, this.deserialize));\n" +
"\n" +
" const stack = clientStack.concat(this.middlewareStack);");
testCommmandCodegen(
"output-structure.smithy",
"getSerdePlugin(config, this.serialize, this.deserialize)"
);
}

@Test
public void writesSerializer() {
testCommmandCodegen("output-structure.smithy",
" private serialize(\n" +
" input: GetFooCommandInput,\n" +
" context: __SerdeContext\n" +
" ): Promise<__HttpRequest> {");
testCommmandCodegen(
"output-structure.smithy",
".ser("
);
}

@Test
public void writesDeserializer() {
testCommmandCodegen("output-structure.smithy",
" private deserialize(\n" +
" output: __HttpResponse,\n" +
" context: __SerdeContext\n" +
" ): Promise<GetFooCommandOutput> {");
testCommmandCodegen(
"output-structure.smithy",
".de("
);
}

private void testCommmandCodegen(String file, String expectedType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private MockManifest testEndpoints(String filename) {
.build();

new TypeScriptCodegenPlugin().execute(context);

assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/endpoint/EndpointParameters.ts"),
is(true));
assertThat(manifest.hasFile(CodegenUtils.SOURCE_FOLDER + "/endpoint/endpointResolver.ts"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,21 @@ private void testInjects(String filename, String extra) {
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(),
containsString("from \"../middleware/HttpApiKeyAuth\""));
assertThat(manifest.getFileString(CodegenUtils.SOURCE_FOLDER + "/commands/GetFooCommand.ts").get(),
containsString("this.middlewareStack.use(getHttpApiKeyAuthPlugin(configuration" + extra + "));"));
containsString("getHttpApiKeyAuthPlugin(config" + extra + ")"));

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

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

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

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

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

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

0 comments on commit 2df620b

Please sign in to comment.