Skip to content

Commit

Permalink
feat(codegen): add Smithy RPCv2 CBOR protocol as an option
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe committed Aug 19, 2024
1 parent ed000cc commit c07537f
Show file tree
Hide file tree
Showing 13 changed files with 1,134 additions and 2,039 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.List;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
import software.amazon.smithy.typescript.codegen.protocols.cbor.SmithyRpcV2Cbor;
import software.amazon.smithy.utils.ListUtils;
import software.amazon.smithy.utils.SmithyInternalApi;

Expand All @@ -28,13 +27,20 @@
@SmithyInternalApi
public class AddProtocols implements TypeScriptIntegration {

/**
* This order differs from the base protocol selection specification
* in that for JavaScript runtimes, JSON-based protocols have higher default priority than CBOR-based.
* This behavior may be fine-tuned at the service level in a case-by-case basis.
*
* @return a list of ProtocolGenerators in the default priority order, highest first.
*/
@Override
public List<ProtocolGenerator> getProtocolGenerators() {
return ListUtils.of(
new SmithyRpcV2Cbor(),
new AwsJsonRpc1_0(),
new AwsJsonRpc1_1(),
new AwsRestJson1(),
new AwsSmithyRpcV2Cbor(),
new AwsRestXml(),
new AwsQuery(),
new AwsEc2()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.aws.typescript.codegen;

import software.amazon.smithy.aws.traits.protocols.AwsQueryCompatibleTrait;
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
import software.amazon.smithy.typescript.codegen.protocols.cbor.SmithyRpcV2Cbor;

/**
* Extension of the Smithy RPCv2 CBOR protocol generator, adding
* AWSQuery compatibility.
*/
public final class AwsSmithyRpcV2Cbor extends SmithyRpcV2Cbor {
@Override
public void generateSharedComponents(GenerationContext context) {
super.generateSharedComponents(context);
if (context.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
TypeScriptWriter writer = context.getWriter();
writer.addImport("HeaderBag", "__HeaderBag", TypeScriptDependency.SMITHY_TYPES);
writer.write("""
const populateBodyWithQueryCompatibility = (parsedOutput: any, headers: __HeaderBag) => {
const queryErrorHeader = headers["x-amzn-query-error"];
if (parsedOutput.body !== undefined && queryErrorHeader != null) {
const codeAndType = queryErrorHeader.split(";");
parsedOutput.body.Code = codeAndType[0];
parsedOutput.body.Type = codeAndType[1];
}
};
""");
}
}

@Override
protected void writeErrorCodeParser(GenerationContext generationContext) {
super.writeErrorCodeParser(generationContext);
TypeScriptWriter writer = generationContext.getWriter();

if (generationContext.getService().hasTrait(AwsQueryCompatibleTrait.class)) {
// Populate parsedOutput.body with 'Code' and 'Type' fields
// "x-amzn-query-error" header is available when AwsQueryCompatibleTrait is applied to a service
// The header value contains query error Code and Type joined by ';'
// E.g. "MalformedInput;Sender" or "InternalFailure;Receiver"
writer.write("populateBodyWithQueryCompatibility(parsedOutput, output.headers);");
}
}
}
198 changes: 87 additions & 111 deletions private/aws-protocoltests-ec2/test/functional/ec2query.spec.ts

Large diffs are not rendered by default.

Loading

0 comments on commit c07537f

Please sign in to comment.