Skip to content
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

Update to Smithy 1.12.0 #448

Merged
merged 1 commit into from
Oct 7, 2021
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
6 changes: 3 additions & 3 deletions smithy-typescript-codegen-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ extra["moduleName"] = "software.amazon.smithy.typescript.codegen.test"
tasks["jar"].enabled = false

plugins {
id("software.amazon.smithy").version("0.5.2")
id("software.amazon.smithy").version("0.5.3")
}

repositories {
Expand All @@ -29,6 +29,6 @@ repositories {

dependencies {
implementation(project(":smithy-typescript-codegen"))
implementation("software.amazon.smithy:smithy-waiters:[1.5.0, 2.0[")
implementation("software.amazon.smithy:smithy-protocol-test-traits:[1.5.0, 2.0[")
implementation("software.amazon.smithy:smithy-waiters:[1.12.0, 2.0[")
implementation("software.amazon.smithy:smithy-protocol-test-traits:[1.12.0, 2.0[")
}
6 changes: 3 additions & 3 deletions smithy-typescript-codegen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extra["displayName"] = "Smithy :: Typescript :: Codegen"
extra["moduleName"] = "software.amazon.smithy.typescript.codegen"

dependencies {
api("software.amazon.smithy:smithy-codegen-core:[1.5.0, 2.0[")
api("software.amazon.smithy:smithy-waiters:[1.5.0, 2.0[")
implementation("software.amazon.smithy:smithy-protocol-test-traits:[1.5.0, 2.0[")
api("software.amazon.smithy:smithy-codegen-core:[1.12.0, 2.0[")
api("software.amazon.smithy:smithy-waiters:[1.12.0, 2.0[")
implementation("software.amazon.smithy:smithy-protocol-test-traits:[1.12.0, 2.0[")
}
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ private void checkValidationSettings() {
List<String> unvalidatedOperations = TopDownIndex.of(model)
.getContainedOperations(service)
.stream()
.filter(o -> operationIndex.getErrors(o).stream()
.filter(o -> operationIndex.getErrors(o, service).stream()
.noneMatch(e -> e.getId().equals(VALIDATION_EXCEPTION_SHAPE)))
.map(s -> s.getId().toString())
.sorted()
Expand Down Expand Up @@ -467,12 +467,13 @@ private void generateServiceInterface(ServiceShape shape) {
}

private void generateServerErrors(ServiceShape service) {
final OperationIndex operationIndex = OperationIndex.of(model);

TopDownIndex.of(model)
.getContainedOperations(service)
.stream()
.flatMap(o -> o.getErrors().stream())
.flatMap(o -> operationIndex.getErrors(o, service).stream())
.distinct()
.map(id -> model.expectShape(id).asStructureShape().orElseThrow(IllegalArgumentException::new))
.sorted()
.forEachOrdered(error -> writers.useShapeWriter(service, symbolProvider, writer -> {
new ServerErrorGenerator(settings, model, error, symbolProvider, writer).run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void generateClientOperationTests(OperationShape operation, OperationInd
}
});
// 3. Generate test cases for each error on each operation.
for (StructureShape error : operationIndex.getErrors(operation)) {
for (StructureShape error : operationIndex.getErrors(operation, service)) {
if (!error.hasTag("server-only")) {
error.getTrait(HttpResponseTestsTrait.class).ifPresent(trait -> {
for (HttpResponseTestCase testCase : trait.getTestCasesFor(AppliesTo.CLIENT)) {
Expand Down Expand Up @@ -208,7 +208,7 @@ private void generateServerOperationTests(OperationShape operation, OperationInd
}
});
// 3. Generate test cases for each error on each operation.
for (StructureShape error : operationIndex.getErrors(operation)) {
for (StructureShape error : operationIndex.getErrors(operation, service)) {
if (!error.hasTag("client-only")) {
error.getTrait(HttpResponseTestsTrait.class).ifPresent(trait -> {
for (HttpResponseTestCase testCase : trait.getTestCasesFor(AppliesTo.SERVER)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static software.amazon.smithy.typescript.codegen.CodegenUtils.getBlobStreamingMembers;
import static software.amazon.smithy.typescript.codegen.CodegenUtils.writeStreamingMemberType;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
Expand All @@ -27,7 +28,6 @@
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.typescript.codegen.integration.ProtocolGenerator;
import software.amazon.smithy.utils.SmithyInternalApi;
Expand All @@ -49,6 +49,7 @@ final class ServerCommandGenerator implements Runnable {
private final Symbol errorsType;
private final ProtocolGenerator protocolGenerator;
private final ApplicationProtocol applicationProtocol;
private final List<StructureShape> errors;

ServerCommandGenerator(
TypeScriptSettings settings,
Expand All @@ -72,6 +73,7 @@ final class ServerCommandGenerator implements Runnable {
inputType = operationSymbol.expectProperty("inputType", Symbol.class);
outputType = operationSymbol.expectProperty("outputType", Symbol.class);
errorsType = operationSymbol.expectProperty("errorsType", Symbol.class);
errors = Collections.unmodifiableList(operationIndex.getErrors(operation, settings.getService()));
}

@Override
Expand Down Expand Up @@ -135,12 +137,12 @@ private void writeOutputType(String typeName, Optional<StructureShape> outputSha
}

private void writeErrorType() {
if (operation.getErrors().isEmpty()) {
if (errors.isEmpty()) {
writer.write("export type $L = never;", errorsType.getName());
} else {
writer.writeInline("export type $L = ", errorsType.getName());
for (Iterator<ShapeId> iter = operation.getErrors().iterator(); iter.hasNext();) {
writer.writeInline("$T", symbolProvider.toSymbol(model.expectShape(iter.next())));
for (Iterator<StructureShape> iter = errors.iterator(); iter.hasNext();) {
writer.writeInline("$T", symbolProvider.toSymbol(iter.next()));
if (iter.hasNext()) {
writer.writeInline(" | ");
}
Expand Down Expand Up @@ -185,12 +187,12 @@ private void writeOperationSerializer() {

private void writeErrorChecker() {
writer.openBlock("isOperationError(error: any): error is $T {", "};", errorsType, () -> {
if (operation.getErrors().isEmpty()) {
if (errors.isEmpty()) {
writer.write("return false;");
} else {
writer.writeInline("const names: $T['name'][] = [", errorsType);
for (Iterator<ShapeId> iter = operation.getErrors().iterator(); iter.hasNext();) {
writer.writeInline("$S", iter.next().getName());
for (Iterator<StructureShape> iter = errors.iterator(); iter.hasNext();) {
writer.writeInline("$S", iter.next().getId().getName());
if (iter.hasNext()) {
writer.writeInline(", ");
}
Expand All @@ -206,12 +208,12 @@ private void writeErrorHandler() {
writer.addImport("ServerSerdeContext", null, "@aws-smithy/server-common");
writer.openBlock("serializeError(error: $T, ctx: ServerSerdeContext): Promise<$T> {", "}",
errorsType, applicationProtocol.getResponseType(), () -> {
if (operation.getErrors().isEmpty()) {
if (errors.isEmpty()) {
writer.write("throw error;");
} else {
writer.openBlock("switch (error.name) {", "}", () -> {
for (ShapeId errorId : operation.getErrors()) {
writeErrorHandlerCase(errorId);
for (StructureShape error : errors) {
writeErrorHandlerCase(error);
}
writer.openBlock("default: {", "}", () -> writer.write("throw error;"));
});
Expand All @@ -220,13 +222,13 @@ private void writeErrorHandler() {
writer.write("");
}

private void writeErrorHandlerCase(ShapeId errorId) {
Symbol errorSymbol = symbolProvider.toSymbol(model.expectShape(errorId));
private void writeErrorHandlerCase(StructureShape error) {
Symbol errorSymbol = symbolProvider.toSymbol(error);
String serializerFunction = ProtocolGenerator.getGenericSerFunctionName(errorSymbol) + "Error";
writer.addImport(serializerFunction, null,
"./" + CodegenUtils.SOURCE_FOLDER + "/protocols/"
+ ProtocolGenerator.getSanitizedName(protocolGenerator.getName()));
writer.openBlock("case $S: {", "}", errorId.getName(), () -> {
writer.openBlock("case $S: {", "}", error.getId().getName(), () -> {
writer.write("return $L(error, ctx);", serializerFunction);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ final class SymbolVisitor implements SymbolProvider, ShapeVisitor<Symbol> {
// Get each structure that's used an error.
OperationIndex operationIndex = OperationIndex.of(model);
model.shapes(OperationShape.class).forEach(operationShape -> {
errorShapes.addAll(operationIndex.getErrors(operationShape));
errorShapes.addAll(operationIndex.getErrors(operationShape, settings.getService()));
});

moduleNameDelegator = new ModuleNameDelegator(shapeChunkSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.HttpBinding;
import software.amazon.smithy.model.knowledge.HttpBindingIndex;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.pattern.SmithyPattern.Segment;
import software.amazon.smithy.model.shapes.BlobShape;
Expand All @@ -50,7 +51,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.ShapeId;
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 @@ -519,9 +519,7 @@ private void generateOperationResponseSerializer(
});
writer.write("");

for (ShapeId errorShapeId : operation.getErrors()) {
serializingErrorShapes.add(context.getModel().expectShape(errorShapeId).asStructureShape().get());
}
serializingErrorShapes.addAll(OperationIndex.of(context.getModel()).getErrors(operation, context.getService()));
}

private void calculateContentLength(GenerationContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import software.amazon.smithy.codegen.core.SymbolProvider;
import software.amazon.smithy.codegen.core.SymbolReference;
import software.amazon.smithy.model.knowledge.HttpBinding.Location;
import software.amazon.smithy.model.knowledge.OperationIndex;
import software.amazon.smithy.model.pattern.SmithyPattern;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.EndpointTrait;
import software.amazon.smithy.model.traits.MediaTypeTrait;
Expand Down Expand Up @@ -321,6 +323,7 @@ static Set<StructureShape> generateErrorDispatcher(
) {
TypeScriptWriter writer = context.getWriter();
SymbolProvider symbolProvider = context.getSymbolProvider();
OperationIndex operationIndex = OperationIndex.of(context.getModel());
Set<StructureShape> errorShapes = new TreeSet<>();

Symbol symbol = symbolProvider.toSymbol(operation);
Expand Down Expand Up @@ -352,8 +355,8 @@ static Set<StructureShape> generateErrorDispatcher(
errorCodeGenerator.accept(context);
writer.openBlock("switch (errorCode) {", "}", () -> {
// Generate the case statement for each error, invoking the specific deserializer.
new TreeSet<>(operation.getErrors()).forEach(errorId -> {
StructureShape error = context.getModel().expectShape(errorId).asStructureShape().get();
new TreeSet<>(operationIndex.getErrors(operation, context.getService())).forEach(error -> {
final ShapeId errorId = error.getId();
// Track errors bound to the operation so their deserializers may be generated.
errorShapes.add(error);
Symbol errorSymbol = symbolProvider.toSymbol(error);
Expand Down