From 70a8eaf3713880bb8c1b2e074fa08b927d968e48 Mon Sep 17 00:00:00 2001 From: Luc Talatinian Date: Wed, 7 Feb 2024 13:40:21 -0500 Subject: [PATCH] restructure some things around ctx --- .../codegen/service/GoServiceIntegration.java | 5 +- .../service/ServiceDirectedCodegen.java | 78 ++++++------- .../service/ServiceProtocolGenerator.java | 3 + .../service/integration/DefaultProtocols.java | 10 +- .../HttpHandlerProtocolGenerator.java | 104 +++++++++++++++++ .../aws/AwsJson10ProtocolGenerator.java | 107 ++++-------------- 6 files changed, 170 insertions(+), 137 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/GoServiceIntegration.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/GoServiceIntegration.java index c2fb79970..7a4d42472 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/GoServiceIntegration.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/GoServiceIntegration.java @@ -26,7 +26,6 @@ import software.amazon.smithy.go.codegen.GoWriter; import software.amazon.smithy.go.codegen.TriConsumer; import software.amazon.smithy.model.Model; -import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.model.shapes.Shape; import software.amazon.smithy.utils.SmithyUnstableApi; @@ -138,9 +137,7 @@ default void writeAdditionalFiles( * * @return Returns the list of protocol generators to register. */ - default List getProtocolGenerators( - Model model, ServiceShape service, SymbolProvider symbolProvider - ) { + default List getProtocolGenerators(GoCodegenContext ctx) { return Collections.emptyList(); } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceDirectedCodegen.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceDirectedCodegen.java index 2f9a1f0b8..d3cae557f 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceDirectedCodegen.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceDirectedCodegen.java @@ -18,6 +18,7 @@ import static java.util.stream.Collectors.toSet; import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; import static software.amazon.smithy.go.codegen.service.ServiceCodegenUtils.getShapesToSerde; +import static software.amazon.smithy.go.codegen.service.ServiceCodegenUtils.isUnit; import static software.amazon.smithy.go.codegen.service.ServiceCodegenUtils.withUnit; import java.util.List; @@ -31,6 +32,7 @@ import software.amazon.smithy.codegen.core.directed.GenerateEnumDirective; import software.amazon.smithy.codegen.core.directed.GenerateErrorDirective; import software.amazon.smithy.codegen.core.directed.GenerateIntEnumDirective; +import software.amazon.smithy.codegen.core.directed.GenerateOperationDirective; import software.amazon.smithy.codegen.core.directed.GenerateServiceDirective; import software.amazon.smithy.codegen.core.directed.GenerateStructureDirective; import software.amazon.smithy.codegen.core.directed.GenerateUnionDirective; @@ -48,37 +50,36 @@ import software.amazon.smithy.go.codegen.SymbolVisitor; import software.amazon.smithy.go.codegen.UnionGenerator; import software.amazon.smithy.model.knowledge.TopDownIndex; +import software.amazon.smithy.model.shapes.EnumShape; import software.amazon.smithy.model.shapes.IntEnumShape; -import software.amazon.smithy.model.shapes.StringShape; import software.amazon.smithy.model.shapes.StructureShape; -import software.amazon.smithy.model.shapes.UnionShape; public class ServiceDirectedCodegen implements DirectedCodegen { @Override - public SymbolProvider createSymbolProvider(CreateSymbolProviderDirective directive) { - return new SymbolVisitor(withUnit(directive.model()), (GoSettings) directive.settings()); + public SymbolProvider createSymbolProvider(CreateSymbolProviderDirective directive) { + return new SymbolVisitor(withUnit(directive.model()), directive.settings()); } @Override - public GoCodegenContext createContext(CreateContextDirective directive) { + public GoCodegenContext createContext(CreateContextDirective directive) { return new GoCodegenContext( withUnit(directive.model()), - (GoSettings) directive.settings(), + directive.settings(), directive.symbolProvider(), directive.fileManifest(), - new WriterDelegator<>(directive.fileManifest(), directive.symbolProvider(), (filename, namespace) -> - new GoWriter(namespace)), + new WriterDelegator<>(directive.fileManifest(), directive.symbolProvider(), + (filename, namespace) -> new GoWriter(namespace)), directive.integrations() ); } @Override - public void generateService(GenerateServiceDirective directive) { - var namespace = ((GoSettings) directive.settings()).getModuleName(); + public void generateService(GenerateServiceDirective directive) { + var namespace = directive.settings().getModuleName(); var delegator = directive.context().writerDelegator(); - var settings = ((GoSettings) directive.settings()); + var settings = directive.settings(); - var protocolGenerator = resolveProtocolGenerator(directive); + var protocolGenerator = resolveProtocolGenerator(directive.context()); var model = directive.model(); var service = directive.service(); @@ -125,11 +126,18 @@ public void generateService(GenerateServiceDirective directive) { } @Override - public void generateStructure(GenerateStructureDirective directive) { + public void generateOperation(GenerateOperationDirective directive) { + var protocolGenerator = resolveProtocolGenerator(directive.context()); + directive.context().writerDelegator().useShapeWriter(directive.shape(), + protocolGenerator.generateHandleOperation(directive.shape())); + } + + @Override + public void generateStructure(GenerateStructureDirective directive) { if (directive.shape().getId().getNamespace().equals(CodegenUtils.getSyntheticTypeNamespace())) { return; } - if (directive.shape().getId().toString().equals("smithy.api#Unit")) { + if (isUnit(directive.shape().getId())) { return; } @@ -138,9 +146,9 @@ public void generateStructure(GenerateStructureDirective directive) { new StructureGenerator( directive.model(), directive.symbolProvider(), - (GoWriter) writer, + writer, directive.service(), - (StructureShape) directive.shape(), + directive.shape(), directive.symbolProvider().toSymbol(directive.shape()), null ).run() @@ -148,15 +156,15 @@ public void generateStructure(GenerateStructureDirective directive) { } @Override - public void generateError(GenerateErrorDirective directive) { + public void generateError(GenerateErrorDirective directive) { var delegator = directive.context().writerDelegator(); delegator.useShapeWriter(directive.shape(), writer -> new StructureGenerator( directive.model(), directive.symbolProvider(), - (GoWriter) writer, + writer, directive.service(), - (StructureShape) directive.shape(), + directive.shape(), directive.symbolProvider().toSymbol(directive.shape()), null ).run() @@ -164,43 +172,35 @@ public void generateError(GenerateErrorDirective directive) { } @Override - public void generateUnion(GenerateUnionDirective directive) { + public void generateUnion(GenerateUnionDirective directive) { var delegator = directive.context().writerDelegator(); delegator.useShapeWriter(directive.shape(), writer -> - new UnionGenerator(directive.model(), directive.symbolProvider(), (UnionShape) directive.shape()) - .generateUnion((GoWriter) writer) + new UnionGenerator(directive.model(), directive.symbolProvider(), directive.shape()) + .generateUnion(writer) ); } @Override - public void generateEnumShape(GenerateEnumDirective directive) { + public void generateEnumShape(GenerateEnumDirective directive) { var delegator = directive.context().writerDelegator(); delegator.useShapeWriter(directive.shape(), writer -> - new EnumGenerator(directive.symbolProvider(), (GoWriter) writer, (StringShape) directive.shape()) - .run() + new EnumGenerator(directive.symbolProvider(), writer, (EnumShape) directive.shape()).run() ); } @Override - public void generateIntEnumShape(GenerateIntEnumDirective directive) { + public void generateIntEnumShape(GenerateIntEnumDirective directive) { directive.context().writerDelegator().useShapeWriter(directive.shape(), writer -> - new IntEnumGenerator( - directive.symbolProvider(), - (GoWriter) writer, - (IntEnumShape) directive.shape() - ).run() + new IntEnumGenerator(directive.symbolProvider(), writer, (IntEnumShape) directive.shape()).run() ); } - private ServiceProtocolGenerator resolveProtocolGenerator( - GenerateServiceDirective directive - ) { - var model = directive.model(); - var service = directive.service(); - var symbolProvider = directive.symbolProvider(); + private ServiceProtocolGenerator resolveProtocolGenerator(GoCodegenContext ctx) { + var model = ctx.model(); + var service = ctx.settings().getService(model); - var protocolGenerators = directive.context().integrations().stream() - .flatMap(it -> it.getProtocolGenerators(model, service, symbolProvider).stream()) + var protocolGenerators = ctx.integrations().stream() + .flatMap(it -> it.getProtocolGenerators(ctx).stream()) .filter(it -> service.hasTrait(it.getProtocol())) .toList(); if (protocolGenerators.isEmpty()) { diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceProtocolGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceProtocolGenerator.java index 54fe93323..b9da3f8a4 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceProtocolGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/ServiceProtocolGenerator.java @@ -18,6 +18,7 @@ import java.util.Set; import software.amazon.smithy.go.codegen.ApplicationProtocol; import software.amazon.smithy.go.codegen.GoWriter; +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.utils.SmithyInternalApi; @@ -32,6 +33,8 @@ public interface ServiceProtocolGenerator { // Go GoWriter.Writable generateHandleRequest(); + GoWriter.Writable generateHandleOperation(OperationShape operation); + GoWriter.Writable generateOptions(); GoWriter.Writable generateDeserializers(Set shape); diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/integration/DefaultProtocols.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/integration/DefaultProtocols.java index 9c57eac53..a7c738a2b 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/integration/DefaultProtocols.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/integration/DefaultProtocols.java @@ -16,21 +16,17 @@ package software.amazon.smithy.go.codegen.service.integration; import java.util.List; -import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.go.codegen.GoCodegenContext; import software.amazon.smithy.go.codegen.service.GoServiceIntegration; import software.amazon.smithy.go.codegen.service.ServiceProtocolGenerator; import software.amazon.smithy.go.codegen.service.protocol.aws.AwsJson10ProtocolGenerator; -import software.amazon.smithy.model.Model; -import software.amazon.smithy.model.shapes.ServiceShape; import software.amazon.smithy.utils.ListUtils; public class DefaultProtocols implements GoServiceIntegration { @Override - public List getProtocolGenerators( - Model model, ServiceShape service, SymbolProvider symbolProvider - ) { + public List getProtocolGenerators(GoCodegenContext ctx) { return ListUtils.of( - new AwsJson10ProtocolGenerator(model, service, symbolProvider) + new AwsJson10ProtocolGenerator(ctx) ); } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/protocol/HttpHandlerProtocolGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/protocol/HttpHandlerProtocolGenerator.java index df07f7bfe..3fee01710 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/protocol/HttpHandlerProtocolGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/protocol/HttpHandlerProtocolGenerator.java @@ -15,13 +15,20 @@ package software.amazon.smithy.go.codegen.service.protocol; +import static software.amazon.smithy.go.codegen.GoWriter.emptyGoTemplate; import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; import software.amazon.smithy.go.codegen.ApplicationProtocol; +import software.amazon.smithy.go.codegen.GoCodegenContext; import software.amazon.smithy.go.codegen.GoStdlibTypes; import software.amazon.smithy.go.codegen.GoWriter; +import software.amazon.smithy.go.codegen.SmithyGoTypes; +import software.amazon.smithy.go.codegen.knowledge.GoValidationIndex; import software.amazon.smithy.go.codegen.service.RequestHandler; import software.amazon.smithy.go.codegen.service.ServiceProtocolGenerator; +import software.amazon.smithy.go.codegen.service.ServiceValidationGenerator; +import software.amazon.smithy.model.shapes.OperationShape; +import software.amazon.smithy.model.shapes.Shape; import software.amazon.smithy.utils.MapUtils; import software.amazon.smithy.utils.SmithyInternalApi; @@ -31,6 +38,16 @@ */ @SmithyInternalApi public abstract class HttpHandlerProtocolGenerator implements ServiceProtocolGenerator { + protected final GoCodegenContext ctx; + + private final GoValidationIndex validationIndex; + + protected HttpHandlerProtocolGenerator(GoCodegenContext ctx) { + this.ctx = ctx; + + this.validationIndex = GoValidationIndex.of(ctx.model()); + } + @Override public ApplicationProtocol getApplicationProtocol() { return ApplicationProtocol.createDefaultHttpApplicationProtocol(); @@ -90,8 +107,95 @@ public GoWriter.Writable generateProtocolSource() { )); } + @Override + public final GoWriter.Writable generateHandleOperation(OperationShape operation) { + var service = ctx.settings().getService(ctx.model()); + var input = ctx.model().expectShape(operation.getInputShape()); + return goTemplate(""" + func (h *$requestHandler:L) $funcName:L(w $rw:T, r $r:P) { + id, err := $newUuid:T($rand:T).GetUUID() + if err != nil { + serializeError(w, err) + return + } + + $beforeDeserialize:W + $deserialize:W + $afterDeserialize:W + + $validate:W + + out, err := h.service.$operation:L(r.Context(), in) + if err != nil { + serializeError(w, err) + return + } + + $beforeSerialize:W + $beforeWriteResponse:W + $serialize:W + } + """, + MapUtils.of( + "requestHandler", RequestHandler.NAME, + "funcName", getOperationHandlerName(operation), + "rw", GoStdlibTypes.Net.Http.ResponseWriter, + "r", GoStdlibTypes.Net.Http.Request + ), + MapUtils.of( + "newUuid", SmithyGoTypes.Rand.NewUUID, + "rand", GoStdlibTypes.Crypto.Rand.Reader, + "deserialize", generateDeserializeRequest(operation), + "validate", validationIndex.operationRequiresValidation(service, operation) + ? generateValidateInput(input) + : emptyGoTemplate(), + "operation", ctx.symbolProvider().toSymbol(operation).getName(), + "serialize", generateSerializeResponse(operation), + "beforeDeserialize", generateInvokeInterceptor("BeforeDeserialize", "r"), + "afterDeserialize", generateInvokeInterceptor("AfterDeserialize", "in"), + "beforeSerialize", generateInvokeInterceptor("BeforeSerialize", "out"), + "beforeWriteResponse", generateInvokeInterceptor("BeforeWriteResponse", "w") + )); + } + /** * Generates the net/http.Handler's ServeHTTP implementation for this protocol. + * Individual operation handlers are generated by generateServeHttpOperation. Implementors should fill in logic here + * to route requests to those methods according to the protocol. */ public abstract GoWriter.Writable generateServeHttp(); + + /** + * Generates a block of logic to convert the input http.Request `r` into the modeled input structure `in`. + */ + public abstract GoWriter.Writable generateDeserializeRequest(OperationShape operation); + + /** + * Generates a block of serialize the modeled output structure `out` to the http.ResponseWriter `w`. + */ + public abstract GoWriter.Writable generateSerializeResponse(OperationShape operation); + + protected final String getOperationHandlerName(OperationShape operation) { + return "serveHTTP" + operation.getId().getName(); + } + + private GoWriter.Writable generateValidateInput(Shape input) { + return goTemplate(""" + if err := $L(in); err != nil { + serializeError(w, err) + return + } + """, ServiceValidationGenerator.getShapeValidatorName(input)); + } + + private GoWriter.Writable generateInvokeInterceptor(String type, String args) { + return goTemplate(""" + for _, i := range h.options.Interceptors.$1L { + if err := i.$1L(r.Context(), id, $2L); err != nil { + serializeError(w, err) + return + } + } + """, type, args); + } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/protocol/aws/AwsJson10ProtocolGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/protocol/aws/AwsJson10ProtocolGenerator.java index 9f4748d29..0ea7dfa99 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/protocol/aws/AwsJson10ProtocolGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/service/protocol/aws/AwsJson10ProtocolGenerator.java @@ -15,30 +15,25 @@ package software.amazon.smithy.go.codegen.service.protocol.aws; -import static software.amazon.smithy.go.codegen.GoWriter.emptyGoTemplate; import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; import static software.amazon.smithy.go.codegen.service.protocol.JsonDeserializerGenerator.getDeserializerName; import static software.amazon.smithy.go.codegen.service.protocol.JsonSerializerGenerator.getSerializerName; import java.util.Set; import software.amazon.smithy.aws.traits.protocols.AwsJson1_0Trait; -import software.amazon.smithy.codegen.core.SymbolProvider; +import software.amazon.smithy.go.codegen.GoCodegenContext; import software.amazon.smithy.go.codegen.GoStdlibTypes; import software.amazon.smithy.go.codegen.GoWriter; import software.amazon.smithy.go.codegen.SmithyGoTypes; -import software.amazon.smithy.go.codegen.knowledge.GoValidationIndex; import software.amazon.smithy.go.codegen.service.NotImplementedError; import software.amazon.smithy.go.codegen.service.RequestHandler; import software.amazon.smithy.go.codegen.service.ServiceCodegenUtils; -import software.amazon.smithy.go.codegen.service.ServiceValidationGenerator; import software.amazon.smithy.go.codegen.service.protocol.HttpHandlerProtocolGenerator; import software.amazon.smithy.go.codegen.service.protocol.JsonDeserializerGenerator; import software.amazon.smithy.go.codegen.service.protocol.JsonSerializerGenerator; -import software.amazon.smithy.model.Model; import software.amazon.smithy.model.knowledge.OperationIndex; import software.amazon.smithy.model.knowledge.TopDownIndex; 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; @@ -52,20 +47,12 @@ */ @SmithyInternalApi public final class AwsJson10ProtocolGenerator extends HttpHandlerProtocolGenerator { - private final Model model; - private final ServiceShape service; - private final SymbolProvider symbolProvider; - private final OperationIndex operationIndex; - private final GoValidationIndex validationIndex; - public AwsJson10ProtocolGenerator(Model model, ServiceShape service, SymbolProvider symbolProvider) { - this.model = model; - this.service = service; - this.symbolProvider = symbolProvider; + public AwsJson10ProtocolGenerator(GoCodegenContext ctx) { + super(ctx); - this.operationIndex = OperationIndex.of(model); - this.validationIndex = GoValidationIndex.of(model); + this.operationIndex = OperationIndex.of(ctx.model()); } @Override @@ -75,13 +62,13 @@ public ShapeId getProtocol() { @Override public GoWriter.Writable generateDeserializers(Set shapes) { - return new JsonDeserializerGenerator(model, symbolProvider).generate(shapes); + return new JsonDeserializerGenerator(ctx.model(), ctx.symbolProvider()).generate(shapes); } @Override public GoWriter.Writable generateSerializers(Set shapes) { return GoWriter.ChainWritable.of( - new JsonSerializerGenerator(model, symbolProvider).generate(shapes), + new JsonSerializerGenerator(ctx.model(), ctx.symbolProvider()).generate(shapes), generateSerializeError() ).compose(); } @@ -90,12 +77,6 @@ public GoWriter.Writable generateSerializers(Set shapes) { public GoWriter.Writable generateServeHttp() { return goTemplate(""" func (h *$requestHandler:L) ServeHTTP(w $rw:T, r $r:P) { - id, err := $newUuid:T($rand:T).GetUUID() - if err != nil { - serializeError(w, err) - return - } - w.Header().Set("Content-Type", "application/x-amz-json-1.0") if r.Method != http.MethodPost { @@ -120,65 +101,24 @@ public GoWriter.Writable generateServeHttp() { } private GoWriter.Writable generateRouteRequest() { + var model = ctx.model(); + var service = ctx.settings().getService(ctx.model()); return GoWriter.ChainWritable.of( TopDownIndex.of(model).getContainedOperations(service).stream() .filter(op -> !ServiceCodegenUtils.operationHasEventStream( model, operationIndex.expectInputShape(op), operationIndex.expectOutputShape(op))) .map(it -> goTemplate(""" if target == $S { - $W + h.$L(w, r) + return } - """, getOperationTarget(it), generateHandleOperation(it))) + """, getOperationTarget(it), getOperationHandlerName(it))) .toList() ).compose(false); } - private GoWriter.Writable generateHandleOperation(OperationShape operation) { - var input = model.expectShape(operation.getInputShape()); - var output = model.expectShape(operation.getOutputShape()); - return goTemplate(""" - $beforeDeserialize:W - $deserialize:W - $afterDeserialize:W - - $validate:W - - out, err := h.service.$operation:L(r.Context(), in) - if err != nil { - serializeError(w, err) - return - } - - $beforeSerialize:W - $beforeWriteResponse:W - $serialize:W - """, - MapUtils.of( - "deserialize", generateDeserialize(input), - "validate", validationIndex.operationRequiresValidation(service, operation) - ? generateValidateInput(input) - : emptyGoTemplate(), - "operation", symbolProvider.toSymbol(operation).getName(), - "serialize", generateSerialize(output), - "beforeDeserialize", generateInvokeInterceptor("BeforeDeserialize", "r"), - "afterDeserialize", generateInvokeInterceptor("AfterDeserialize", "in"), - "beforeSerialize", generateInvokeInterceptor("BeforeSerialize", "out"), - "beforeWriteResponse", generateInvokeInterceptor("BeforeWriteResponse", "w") - )); - } - - private GoWriter.Writable generateInvokeInterceptor(String type, String args) { - return goTemplate(""" - for _, i := range h.options.Interceptors.$1L { - if err := i.$1L(r.Context(), id, $2L); err != nil { - serializeError(w, err) - return - } - } - """, type, args); - } - - private GoWriter.Writable generateDeserialize(Shape input) { + @Override + public GoWriter.Writable generateDeserializeRequest(OperationShape operation) { return goTemplate(""" d := $decoder:T(r.Body) d.UseNumber() @@ -196,20 +136,12 @@ private GoWriter.Writable generateDeserialize(Shape input) { """, MapUtils.of( "decoder", GoStdlibTypes.Encoding.Json.NewDecoder, - "deserialize", getDeserializerName(input) + "deserialize", getDeserializerName(ctx.model().expectShape(operation.getInputShape())) )); } - private GoWriter.Writable generateValidateInput(Shape input) { - return goTemplate(""" - if err := $L(in); err != nil { - serializeError(w, err) - return - } - """, ServiceValidationGenerator.getShapeValidatorName(input)); - } - - private GoWriter.Writable generateSerialize(Shape output) { + @Override + public GoWriter.Writable generateSerializeResponse(OperationShape operation) { return goTemplate(""" e := $encoder:T() if err := $serialize:L(out, e.Value); err != nil { @@ -223,12 +155,12 @@ private GoWriter.Writable generateSerialize(Shape output) { """, MapUtils.of( "encoder", SmithyGoTypes.Encoding.Json.NewEncoder, - "serialize", getSerializerName(output) + "serialize", getSerializerName(ctx.model().expectShape(operation.getOutputShape())) )); } private GoWriter.Writable generateSerializeError() { - var errorShapes = model.getStructureShapesWithTrait(ErrorTrait.class); + var errorShapes = ctx.model().getStructureShapesWithTrait(ErrorTrait.class); return goTemplate(""" func serializeError(w $rw:T, err error) { if _, ok := err.($invalidParams:T); ok { @@ -279,13 +211,14 @@ private GoWriter.Writable generateSerializeError(StructureShape errorShape) { } """, MapUtils.of( - "err", symbolProvider.toSymbol(errorShape), + "err", ctx.symbolProvider().toSymbol(errorShape), "status", httpStatus, "type", errorShape.getId().toString() )); } private String getOperationTarget(OperationShape operation) { + var service = ctx.settings().getService(ctx.model()); return service.getId().getName(service) + "." + operation.getId().getName(service); } }