Skip to content

Improve complex serialization support #13

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 1 commit into from
Oct 31, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.utils.StringUtils;

/**
* Generates a client command using plugins.
Expand Down Expand Up @@ -202,7 +201,7 @@ private void writeSerde() {
.write("protocol: string,")
.write("context: SerdeContext")
.dedent()
.openBlock("): $T {", "}", applicationProtocol.getRequestType(), () -> writeSerdeDispatcher("input"));
.openBlock("): $T {", "}", applicationProtocol.getRequestType(), () -> writeSerdeDispatcher(true));

writer.write("")
.write("private deserialize(")
Expand All @@ -211,41 +210,30 @@ private void writeSerde() {
.write("protocol: string,")
.write("context: SerdeContext")
.dedent()
.openBlock("): Promise<$L> {", "}", outputType, () -> writeSerdeDispatcher("output"))
.openBlock("): Promise<$L> {", "}", outputType, () -> writeSerdeDispatcher(false))
.write("");
}

private void writeSerdeDispatcher(String inputOrOutput) {
private void writeSerdeDispatcher(boolean isInput) {
writer.openBlock("switch (protocol) {", "}", () -> {
// Generate case statements for each supported protocol.
// For example:
// case 'aws.rest-json-1.1':
// return getFooCommandAws_RestJson1_1Serialize(input, utils);
// TODO Validate this is the right set of protocols; settings.protocols was empty here.
for (String protocol : settings.resolveServiceProtocols(service)) {
String serdeFunctionName = getSerdeFunctionName(symbol, protocol, inputOrOutput);
String serdeFunctionName = isInput
? ProtocolGenerator.getSerFunctionName(symbol, protocol)
: ProtocolGenerator.getDeserFunctionName(symbol, protocol);
writer.addImport(serdeFunctionName, serdeFunctionName,
"./protocols/" + ProtocolGenerator.getSanitizedName(protocol));
writer.write("case '$L':", protocol)
.write(" return $L($L, context);", serdeFunctionName, inputOrOutput);
.write(" return $L($L, context);", serdeFunctionName, isInput ? "input" : "output");
}

writer.write("default:")
.write(" throw new Error(\"Unknown protocol, \" + protocol + \". Expected one of: $L\");",
settings.getProtocols());
});
}

private static String getSerdeFunctionName(Symbol commandSymbol, String protocol, String inputOrOutput) {
String functionName = StringUtils.uncapitalize(commandSymbol.getName());
functionName += ProtocolGenerator.getSanitizedName(protocol);

if (inputOrOutput.equals("input")) {
functionName += "Serialize";
} else {
functionName += "Deserialize";
}

return functionName;
}
}
Loading