Skip to content

Commit

Permalink
address feedbacks: fix indentation; readResponseBody; add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Jan 8, 2020
1 parent def3531 commit faba30a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import software.amazon.smithy.model.shapes.NumberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeType;
import software.amazon.smithy.model.shapes.StringShape;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.TimestampShape;
Expand Down Expand Up @@ -721,6 +720,7 @@ private List<HttpBinding> readResponseBody(
.orElse(false);

if (!documentBindings.isEmpty()) {
// If response has document binding, the body can be parsed to JavaScript object.
writer.write("const data: any = await parseBody(output.body, context);");
deserializeOutputDocument(context, operationOrError, documentBindings);
return documentBindings;
Expand All @@ -735,12 +735,11 @@ private List<HttpBinding> readResponseBody(
} else if (target instanceof BlobShape) {
// If payload is blob, only need to collect stream to binary data(Uint8Array).
writer.write("const data: any = await collectBody(output.body, context);");
} else if (target instanceof CollectionShape || target instanceof StructureShape) {
// If body is Collection or Structure, they we need to parse the string into JavaScript object.
} else if (target instanceof StructureShape || target instanceof UnionShape) {
// If body is Structure or Union, they we need to parse the string into JavaScript object.
writer.write("const data: any = await parseBody(output.body, context);");
} else {
// If payload is other scalar types(not Collection or Structure), because payload will be values in
// string instead of valid JSON or XML. So we need to collect body and encode binary to string.
// If payload is string, we need to collect body and encode binary to string.
writer.write("const data: any = await collectBodyString(output.body, context);");
}
writer.write("contents.$L = $L;", binding.getMemberName(), getOutputValue(context,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,17 @@ static void generateMetadataDeserializer(GenerationContext context, SymbolRefere
writer.write("");
}

/**
* Writes a response body stream collector. This function converts the low-level response body stream to
* Uint8Array binary data.
*
* @param context The generation context.
*/
static void generateCollectBody(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.write("// Collect low-level response body stream to Uint8Array.");
writer.openBlock("const collectBody = (streamBody: any, context: __SerdeContext): Promise<Uint8Array> => {",
"};", () -> {
writer.write("return context.streamCollector(streamBody) || new Uint8Array();");
Expand All @@ -120,10 +127,17 @@ static void generateCollectBody(GenerationContext context) {
writer.write("");
}

/**
* Writes a function converting the low-level response body stream to utf-8 encoded string. It depends on
* response body stream collector{@link #generateCollectBody(GenerationContext)}.
*
* @param context The generation context
*/
static void generateCollectBodyString(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.addImport("SerdeContext", "__SerdeContext", "@aws-sdk/types");
writer.write("// Encode Uint8Array data into string with utf-8.");
writer.openBlock("const collectBodyString = (streamBody: any, context: __SerdeContext): Promise<string> => {",
"};", () -> {
writer.write("return collectBody(streamBody, context).then(body => context.utf8Encoder(body));");
Expand Down Expand Up @@ -167,11 +181,12 @@ static Set<StructureShape> generateErrorDispatcher(
// Prepare error response for parsing error code. If error code needs to be parsed from response body
// then we collect body and parse it to JS object, otherwise leave the response body as is.
writer.openBlock(
"const $L: any = {", "};", shouldParseErrorBody ? "parsedOutput" : "errorOutput", () -> {
writer.write("...output,");
writer.write("body: $L,",
shouldParseErrorBody ? "await parseBody(output.body, context)" : "output.body");
});
"const $L: any = {", "};", shouldParseErrorBody ? "parsedOutput" : "errorOutput",
() -> {
writer.write("...output,");
writer.write("body: $L,",
shouldParseErrorBody ? "await parseBody(output.body, context)" : "output.body");
});

// Error responses must be at least SmithyException and MetadataBearer implementations.
writer.addImport("SmithyException", "__SmithyException",
Expand Down

0 comments on commit faba30a

Please sign in to comment.