Skip to content

Commit

Permalink
move isErrorCodeInBody flag to constructor and class member
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Jan 8, 2020
1 parent faba30a commit c82964a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public abstract class HttpBindingProtocolGenerator implements ProtocolGenerator
private final Set<Shape> serializingDocumentShapes = new TreeSet<>();
private final Set<Shape> deserializingDocumentShapes = new TreeSet<>();
private final Set<StructureShape> deserializingErrorShapes = new TreeSet<>();
private final boolean isErrorCodeInBody;

/**
* Creates a Http binding protocol generator.
*
* @param isErrorCodeInBody A boolean indicates whether error code is located in error response body.
*/
public HttpBindingProtocolGenerator(boolean isErrorCodeInBody) {
this.isErrorCodeInBody = isErrorCodeInBody;
}

@Override
public ApplicationProtocol getApplicationProtocol() {
Expand Down Expand Up @@ -595,7 +605,7 @@ private void generateOperationDeserializer(

// Write out the error deserialization dispatcher.
Set<StructureShape> errorShapes = HttpProtocolGeneratorUtils.generateErrorDispatcher(
context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody());
context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody);
deserializingErrorShapes.addAll(errorShapes);
}

Expand All @@ -607,13 +617,12 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
Symbol errorSymbol = symbolProvider.toSymbol(error);
String errorDeserMethodName = ProtocolGenerator.getDeserFunctionName(errorSymbol,
context.getProtocolName()) + "Response";
boolean isBodyParsed = this.isErrorCodeInBody();

writer.openBlock("const $L = async (\n"
+ " $L: any,\n"
+ " context: __SerdeContext\n"
+ "): Promise<$T> => {", "};",
errorDeserMethodName, isBodyParsed ? "parsedOutput" : "output", errorSymbol, () -> {
errorDeserMethodName, this.isErrorCodeInBody ? "parsedOutput" : "output", errorSymbol, () -> {
writer.openBlock("const contents: $T = {", "};", errorSymbol, () -> {
writer.write("__type: $S,", error.getId().getName());
writer.write("$$fault: $S,", error.getTrait(ErrorTrait.class).get().getValue());
Expand All @@ -624,7 +633,8 @@ private void generateErrorDeserializer(GenerationContext context, StructureShape
});

readHeaders(context, error, bindingIndex);
List<HttpBinding> documentBindings = readErrorResponseBody(context, error, bindingIndex, isBodyParsed);
List<HttpBinding> documentBindings = readErrorResponseBody(
context, error, bindingIndex, this.isErrorCodeInBody);
// Track all shapes bound to the document so their deserializers may be generated.
documentBindings.forEach(binding -> {
Shape target = model.expectShape(binding.getMember().getTarget());
Expand Down Expand Up @@ -912,10 +922,16 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha
* Writes the code that loads an {@code errorCode} String with the content used
* to dispatch errors to specific serializers.
*
* <p>Three variables will be in scope:
* <p>Two variables will be in scope:
* <ul>
* <li>{@code output}: a value of the HttpResponse type.</li>
* <li>{@code data}: the contents of the response body.</li>
* <li>{@code errorOutput} or {@code parsedOutput}: a value of the HttpResponse type.
* <ul>
* <li>{@code errorOutput} is a raw HttpResponse, available when {@code isErrorCodeInBody} is set to
* {@code false}</li>
* <li>{@code parsedOutput} is a HttpResponse type with body parsed to JavaScript object, available
* when {@code isErrorCodeInBody} is set to {@code true}</li>
* </ul>
* </li>
* <li>{@code context}: the SerdeContext.</li>
* </ul>
*
Expand All @@ -929,14 +945,6 @@ private String getNumberOutputParam(Location bindingType, String dataSource, Sha
*/
protected abstract void writeErrorCodeParser(GenerationContext context);

/**
* A boolean indicates whether body is collected and parsed in error code parser.
* If so, each error shape deserializer should not parse body again.
*
* @return returns whether the error code exists in response body
*/
protected abstract boolean isErrorCodeInBody();

/**
* Writes the code needed to deserialize the output document of a response.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ public abstract class HttpRpcProtocolGenerator implements ProtocolGenerator {
private final Set<Shape> serializingDocumentShapes = new TreeSet<>();
private final Set<Shape> deserializingDocumentShapes = new TreeSet<>();
private final Set<StructureShape> deserializingErrorShapes = new TreeSet<>();
private final boolean isErrorCodeInBody;

/**
* Creates a Http RPC protocol generator.
*
* @param isErrorCodeInBody A boolean indicates whether error code is located in error response body.
*/
public HttpRpcProtocolGenerator(boolean isErrorCodeInBody) {
this.isErrorCodeInBody = isErrorCodeInBody;
}

@Override
public ApplicationProtocol getApplicationProtocol() {
Expand Down Expand Up @@ -254,7 +264,7 @@ private void generateOperationDeserializer(GenerationContext context, OperationS

// Write out the error deserialization dispatcher.
Set<StructureShape> errorShapes = HttpProtocolGeneratorUtils.generateErrorDispatcher(
context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody());
context, operation, responseType, this::writeErrorCodeParser, this.isErrorCodeInBody);
deserializingErrorShapes.addAll(errorShapes);
}

Expand Down Expand Up @@ -314,9 +324,12 @@ private void readResponseBody(GenerationContext context, OperationShape operatio
* <p>Two variables will be in scope:
* <ul>
* <li>{@code errorOutput} or {@code parsedOutput}: a value of the HttpResponse type.
* {@code errorOutput} is a raw HttpResponse whereas {@code parsedOutput} is a HttpResponse type with
* body parsed to JavaScript object.
* The actual value available is determined by {@link #isErrorCodeInBody}
* <ul>
* <li>{@code errorOutput} is a raw HttpResponse, available when {@code isErrorCodeInBody} is set to
* {@code false}</li>
* <li>{@code parsedOutput} is a HttpResponse type with body parsed to JavaScript object, available
* when {@code isErrorCodeInBody} is set to {@code true}</li>
* </ul>
* </li>
* <li>{@code context}: the SerdeContext.</li>
* </ul>
Expand All @@ -331,17 +344,6 @@ private void readResponseBody(GenerationContext context, OperationShape operatio
*/
protected abstract void writeErrorCodeParser(GenerationContext context);

/**
* Indicates whether body is collected and parsed in error dispatcher.
*
* <p>If returns true, {@link #writeErrorCodeParser} will have {@code parsedOutput} in scope
*
* <P>If returns false, {@link #writeErrorCodeParser} will have {@code errorOutput} in scope
*
* @return returns whether the error code exists in response body
*/
protected abstract boolean isErrorCodeInBody();

/**
* Writes the code needed to deserialize the output document of a response.
*
Expand Down

0 comments on commit c82964a

Please sign in to comment.