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

add support for go-integrations overriding error code for error types #315

Merged
merged 1 commit into from
Jul 14, 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
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ void execute() {
});
}


LOGGER.fine("Flushing go writers");
List<SymbolDependency> dependencies = writers.getDependencies();
writers.flushWriters();
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,21 +45,24 @@ final class StructureGenerator implements Runnable {
private final StructureShape shape;
private final Symbol symbol;
private final ServiceShape service;
private final ProtocolGenerator protocolGenerator;

StructureGenerator(
Model model,
SymbolProvider symbolProvider,
GoWriter writer,
ServiceShape service,
StructureShape shape,
Symbol symbol
Symbol symbol,
ProtocolGenerator protocolGenerator
) {
this.model = model;
this.symbolProvider = symbolProvider;
this.writer = writer;
this.service = service;
this.shape = shape;
this.symbol = symbol;
this.protocolGenerator = protocolGenerator;
}

@Override
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -260,6 +262,19 @@ default Map<String, ShapeId> 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.
*/
Expand Down