Skip to content

feat: command classBuilder codegen #1120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.MockManifest;
import software.amazon.smithy.build.PluginContext;
Expand All @@ -12,33 +13,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 @@ -3,6 +3,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.MockManifest;
import software.amazon.smithy.build.PluginContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.MockManifest;
import software.amazon.smithy.build.PluginContext;
Expand Down Expand Up @@ -80,7 +81,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 @@ -20,6 +20,7 @@
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.build.MockManifest;
import software.amazon.smithy.build.PluginContext;
Expand Down Expand Up @@ -61,21 +62,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 +125,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\"")));
}
}