diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java index de82a55bf..604790ae9 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenVisitor.java @@ -213,7 +213,6 @@ void execute() { }); } - LOGGER.fine("Flushing go writers"); List dependencies = writers.getDependencies(); writers.flushWriters(); @@ -234,7 +233,7 @@ public Void structureShape(StructureShape shape) { } Symbol symbol = symbolProvider.toSymbol(shape); writers.useShapeWriter(shape, writer -> new StructureGenerator( - model, symbolProvider, writer, service, shape, symbol).run()); + model, symbolProvider, writer, service, shape, symbol, protocolGenerator).run()); return null; } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/OperationGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/OperationGenerator.java index 02bfe34c2..417b00475 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/OperationGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/OperationGenerator.java @@ -124,14 +124,14 @@ public void run() { // Write out the input and output structures. These are written out here to prevent naming conflicts with other // shapes in the model. - new StructureGenerator(model, symbolProvider, writer, service, inputShape, inputSymbol) + new StructureGenerator(model, symbolProvider, writer, service, inputShape, inputSymbol, protocolGenerator) .renderStructure(() -> { }, true); // The output structure gets a metadata member added. Symbol metadataSymbol = SymbolUtils.createValueSymbolBuilder("Metadata", SmithyGoDependency.SMITHY_MIDDLEWARE) .build(); - new StructureGenerator(model, symbolProvider, writer, service, outputShape, outputSymbol) + new StructureGenerator(model, symbolProvider, writer, service, outputShape, outputSymbol, protocolGenerator) .renderStructure(() -> { if (outputShape.getMemberNames().size() != 0) { writer.write(""); diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/StructureGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/StructureGenerator.java index ceabe453c..7d5f544d8 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/StructureGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/StructureGenerator.java @@ -19,6 +19,7 @@ import java.util.Set; import software.amazon.smithy.codegen.core.Symbol; import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; import software.amazon.smithy.model.Model; import software.amazon.smithy.model.shapes.MemberShape; import software.amazon.smithy.model.shapes.ServiceShape; @@ -44,6 +45,7 @@ final class StructureGenerator implements Runnable { private final StructureShape shape; private final Symbol symbol; private final ServiceShape service; + private final ProtocolGenerator protocolGenerator; StructureGenerator( Model model, @@ -51,7 +53,8 @@ final class StructureGenerator implements Runnable { GoWriter writer, ServiceShape service, StructureShape shape, - Symbol symbol + Symbol symbol, + ProtocolGenerator protocolGenerator ) { this.model = model; this.symbolProvider = symbolProvider; @@ -59,6 +62,7 @@ final class StructureGenerator implements Runnable { this.service = service; this.shape = shape; this.symbol = symbol; + this.protocolGenerator = protocolGenerator; } @Override @@ -149,8 +153,10 @@ private void renderErrorStructure() { }); writer.write("return *e.Message"); }); - writer.write("func (e *$L) ErrorCode() string { return $S }", - structureSymbol.getName(), shape.getId().getName(service)); + + String errorCode = protocolGenerator == null ? shape.getId().getName(service) + : protocolGenerator.getErrorCode(service, shape); + writer.write("func (e *$L) ErrorCode() string { return $S }", structureSymbol.getName(), errorCode); String fault = "smithy.FaultUnknown"; if (errorTrait.isClientError()) { diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/ProtocolGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/ProtocolGenerator.java index 8b15163be..cf3ca1277 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/ProtocolGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/ProtocolGenerator.java @@ -27,11 +27,13 @@ import software.amazon.smithy.go.codegen.GoWriter; import software.amazon.smithy.go.codegen.SyntheticClone; import software.amazon.smithy.model.Model; +import software.amazon.smithy.model.node.ExpectationNotMetException; import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ServiceShape; 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.ErrorTrait; import software.amazon.smithy.model.traits.Trait; import software.amazon.smithy.utils.CaseUtils; import software.amazon.smithy.utils.StringUtils; @@ -260,6 +262,19 @@ default Map getOperationErrors(GenerationContext context, Opera return HttpProtocolGeneratorUtils.getOperationErrors(context, operation); } + /** + * Returns an error code for an error shape. Defaults to error shape name as error code. + * + * @param service the service enclosure for the error shape. + * @param errorShape the error shape for which error code is retrieved. + * @return the error code associated with the provided shape. + * @throws ExpectationNotMetException if provided shape is not modeled with an {@link ErrorTrait}. + */ + default String getErrorCode(ServiceShape service, StructureShape errorShape) { + errorShape.expectTrait(ErrorTrait.class); + return errorShape.getId().getName(service); + } + /** * Context object used for service serialization and deserialization. */