Skip to content

Update for error generation refactoring #491

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
Dec 5, 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 @@ -81,7 +81,7 @@ protected void serializeInputDocument(
TypeScriptWriter writer = context.getWriter();

// Input documents are wrapped in an input shape named wrapper, build that.
writer.openBlock("let wrappedBody: any = {", "};", () -> {
writer.openBlock("const wrappedBody: any = {", "};", () -> {
writer.write("$L: $L,", inputStructure.getId().getName(),
inputStructure.accept(getMemberSerVisitor(context, "input")));
});
Expand All @@ -96,7 +96,7 @@ private DocumentMemberSerVisitor getMemberSerVisitor(GenerationContext context,
protected void writeErrorCodeParser(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

writer.write("let errorTypeParts: String = data[\"__type\"].split('#');");
writer.write("const errorTypeParts: String = data[\"__type\"].split('#');");
writer.write("errorCode = (errorTypeParts[1] === undefined) ? errorTypeParts[0] : errorTypeParts[1];");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import software.amazon.smithy.model.shapes.ShapeIndex;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.shapes.UnionShape;
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.JsonNameTrait;
import software.amazon.smithy.model.traits.TimestampFormatTrait.Format;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
Expand Down Expand Up @@ -88,16 +87,12 @@ protected void deserializeMap(GenerationContext context, MapShape shape) {
@Override
protected void deserializeStructure(GenerationContext context, StructureShape shape) {
TypeScriptWriter writer = context.getWriter();
boolean isError = shape.hasTrait(ErrorTrait.class);

// Prepare the document contents structure.
// Use a TreeMap to sort the members.
Map<String, MemberShape> members = new TreeMap<>(shape.getAllMembers());
writer.openBlock("let contents: any = {", "};", () -> {
writer.write("__type: $S,", shape.getId().getName());
if (isError) {
writer.write("$$fault: $S,", shape.getTrait(ErrorTrait.class).get().getValue());
}
// Set all the members to undefined to meet type constraints.
members.forEach((memberName, memberShape) -> writer.write("$L: undefined,", memberName));
});
Expand All @@ -116,11 +111,7 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
});
});

if (isError) {
writer.write("return Object.assign(new Error($S), contents);", shape.getId().getName());
} else {
writer.write("return contents;");
}
writer.write("return contents;");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void serializeInputDocument(
SymbolProvider symbolProvider = context.getSymbolProvider();
TypeScriptWriter writer = context.getWriter();

writer.write("let bodyParams: any = {};");
writer.write("const bodyParams: any = {};");
for (HttpBinding binding : documentBindings) {
MemberShape member = binding.getMember();
// The name of the member to get from the input shape.
Expand Down Expand Up @@ -122,7 +122,7 @@ protected void writeErrorCodeParser(GenerationContext context) {
@Override
public void deserializeOutputDocument(
GenerationContext context,
OperationShape operation,
Shape operationOrError,
List<HttpBinding> documentBindings
) {
TypeScriptWriter writer = context.getWriter();
Expand Down