codegen/smithy-aws-go-codegen/src/main/java/software/amazon/smithy/aws/go/codegen/customization/S3GetBucketLocation.java
private void writeCustomDeserializer(
            GoWriter goWriter,
            Model model,
            SymbolProvider symbolProvider,
            ServiceShape service,
            OperationShape operation
    ) {
        GoStackStepMiddlewareGenerator middleware = GoStackStepMiddlewareGenerator.createDeserializeStepMiddleware(
                getDeserializeMiddlewareName(service, operation), ProtocolUtils.OPERATION_DESERIALIZER_MIDDLEWARE_ID);

        String errorFunctionName = ProtocolGenerator.getOperationErrorDeserFunctionName(
                operation, service, protocolName);

        middleware.writeMiddleware(goWriter, (generator, writer) -> {
            writer.addUseImports(SmithyGoDependency.FMT);

            writer.write("out, metadata, err = next.$L(ctx, in)", generator.getHandleMethodName());
            writer.write("if err != nil { return out, metadata, err }");
            writer.write("");

            writer.addUseImports(SmithyGoDependency.SMITHY_HTTP_TRANSPORT);
            writer.write("response, ok := out.RawResponse.(*smithyhttp.Response)");
            writer.openBlock("if !ok {", "}", () -> {
                writer.addUseImports(SmithyGoDependency.SMITHY);
                writer.write(String.format("return out, metadata, &smithy.DeserializationError{Err: %s}",
                        "fmt.Errorf(\"unknown transport type %T\", out.RawResponse)"));
            });
            writer.write("");

            writer.openBlock("if response.StatusCode < 200 || response.StatusCode >= 300 {", "}", () -> {
                writer.write("return out, metadata, $L(response, &metadata)", errorFunctionName);
            });

            Shape outputShape = model.expectShape(operation.getOutput()
                    .orElseThrow(() -> new CodegenException("expect output shape for operation: " + operation.getId()))
            );

            Symbol outputSymbol = symbolProvider.toSymbol(outputShape);

            // initialize out.Result as output structure shape
            writer.write("output := &$T{}", outputSymbol);
            writer.write("out.Result = output");
            writer.write("");

            writer.addUseImports(SmithyGoDependency.XML);
            writer.addUseImports(SmithyGoDependency.SMITHY_XML);
            writer.addUseImports(SmithyGoDependency.IO);
            writer.addUseImports(SmithyGoDependency.SMITHY_IO);

            writer.write("var buff [1024]byte");
            writer.write("ringBuffer := smithyio.NewRingBuffer(buff[:])");
            writer.write("body := io.TeeReader(response.Body, ringBuffer)");
            writer.write("rootDecoder := xml.NewDecoder(body)");

            // define a decoder with empty start element since we s3 does not wrap Location Constraint
            // xml tag with operation specific xml tag.
            writer.write("decoder := smithyxml.WrapNodeDecoder(rootDecoder, xml.StartElement{})");

            String deserFuncName = ProtocolGenerator.getDocumentDeserializerFunctionName(outputShape, service, protocolName);
            writer.addUseImports(SmithyGoDependency.IO);

            // delegate to already generated inner body deserializer function.
            writer.write("err = $L(&output, decoder)", deserFuncName);

            // EOF error is valid in this case, as we provide a NOP start element at start.
            // Note that we correctly handle unexpected EOF.
            writer.addUseImports(SmithyGoDependency.IO);
            writer.write("if err == io.EOF { err = nil }");

            XmlProtocolUtils.handleDecodeError(writer, "out, metadata,");

            writer.write("");
            writer.write("return out, metadata, err");
        });
    }
/**
     * writes a custom deserializer middleware for the provided operation.
     *
     * @param goWriter       is the go writer used.
     * @param model          is the generation model.
     * @param symbolProvider is the symbol provider.
     * @param operation      is the operation shape for which custom deserializer is written.
     */
writes a custom deserializer middleware for the provided operation.
@param goWriter       is the go writer used.
@param model          is the generation model.
@param symbolProvider is the symbol provider.
@param operation      is the operation shape for which custom deserializer is written.
writes a custom deserializer middleware for the provided operation . @param goWriter is the go writer used . @param model is the generation model . @param symbolProvider is the symbol provider . @param operation is the operation shape for which custom deserializer is written .
private void writeCustomDeserializer(
            GoWriter goWriter,
            Model model,
            SymbolProvider symbolProvider,
            ServiceShape service,
            OperationShape operation
    ) {
        GoStackStepMiddlewareGenerator middleware = GoStackStepMiddlewareGenerator.createDeserializeStepMiddleware(
                getDeserializeMiddlewareName(service, operation), ProtocolUtils.OPERATION_DESERIALIZER_MIDDLEWARE_ID);
        String errorFunctionName = ProtocolGenerator.getOperationErrorDeserFunctionName(
                operation, service, protocolName);
        middleware.writeMiddleware(goWriter, (generator, writer) -> {
            writer.addUseImports(SmithyGoDependency.FMT);
            writer.write("out, metadata, err = next.$L(ctx, in)", generator.getHandleMethodName());
            writer.write("if err != nil { return out, metadata, err }");
            writer.write("");
            writer.addUseImports(SmithyGoDependency.SMITHY_HTTP_TRANSPORT);
            writer.write("response, ok := out.RawResponse.(*smithyhttp.Response)");
            writer.openBlock("if !ok {", "}", () -> {
                writer.addUseImports(SmithyGoDependency.SMITHY);
                writer.write(String.format("return out, metadata, &smithy.DeserializationError{Err: %s}",
                        "fmt.Errorf(\"unknown transport type %T\", out.RawResponse)"));
            });
            writer.write("");
            writer.openBlock("if response.StatusCode < 200 || response.StatusCode >= 300 {", "}", () -> {
                writer.write("return out, metadata, $L(response, &metadata)", errorFunctionName);
            });
            Shape outputShape = model.expectShape(operation.getOutput()
                    .orElseThrow(() -> new CodegenException("expect output shape for operation: " + operation.getId()))
            );
            Symbol outputSymbol = symbolProvider.toSymbol(outputShape);
            writer.write("output := &$T{}", outputSymbol);
            writer.write("out.Result = output");
            writer.write("");
            writer.addUseImports(SmithyGoDependency.XML);
            writer.addUseImports(SmithyGoDependency.SMITHY_XML);
            writer.addUseImports(SmithyGoDependency.IO);
            writer.addUseImports(SmithyGoDependency.SMITHY_IO);
            writer.write("var buff [1024]byte");
            writer.write("ringBuffer := smithyio.NewRingBuffer(buff[:])");
            writer.write("body := io.TeeReader(response.Body, ringBuffer)");
            writer.write("rootDecoder := xml.NewDecoder(body)");
            writer.write("decoder := smithyxml.WrapNodeDecoder(rootDecoder, xml.StartElement{})");
            String deserFuncName = ProtocolGenerator.getDocumentDeserializerFunctionName(outputShape, service, protocolName);
            writer.addUseImports(SmithyGoDependency.IO);
            writer.write("err = $L(&output, decoder)", deserFuncName);
            writer.addUseImports(SmithyGoDependency.IO);
            writer.write("if err == io.EOF { err = nil }");
            XmlProtocolUtils.handleDecodeError(writer, "out, metadata,");
            writer.write("");
            writer.write("return out, metadata, err");
        });
    }
private void writeCustomDeserializer ( GoWriter goWriter , Model model , SymbolProvider symbolProvider , ServiceShape service , OperationShape operation ) { GoStackStepMiddlewareGenerator middleware = GoStackStepMiddlewareGenerator . createDeserializeStepMiddleware ( getDeserializeMiddlewareName ( service , operation ) , ProtocolUtils . OPERATION_DESERIALIZER_MIDDLEWARE_ID ) ; String errorFunctionName = ProtocolGenerator . getOperationErrorDeserFunctionName ( operation , service , protocolName ) ; middleware . writeMiddleware ( goWriter , ( generator , writer ) -> { writer . addUseImports ( SmithyGoDependency . FMT ) ; writer . write ( "out, metadata, err = next.$L(ctx, in)" , generator . getHandleMethodName ( ) ) ; writer . write ( "if err != nil { return out, metadata, err }" ) ; writer . write ( "" ) ; writer . addUseImports ( SmithyGoDependency . SMITHY_HTTP_TRANSPORT ) ; writer . write ( "response, ok := out.RawResponse.(*smithyhttp.Response)" ) ; writer . openBlock ( "if !ok {" , "}" , ( ) -> { writer . addUseImports ( SmithyGoDependency . SMITHY ) ; writer . write ( String . format ( "return out, metadata, &smithy.DeserializationError{Err: %s}" , "fmt.Errorf(\"unknown transport type %T\", out.RawResponse)" ) ) ; } ) ; writer . write ( "" ) ; writer . openBlock ( "if response.StatusCode < 200 || response.StatusCode >= 300 {" , "}" , ( ) -> { writer . write ( "return out, metadata, $L(response, &metadata)" , errorFunctionName ) ; } ) ; Shape outputShape = model . expectShape ( operation . getOutput ( ) . orElseThrow ( ( ) -> new CodegenException ( "expect output shape for operation: " + operation . getId ( ) ) ) ) ; Symbol outputSymbol = symbolProvider . toSymbol ( outputShape ) ; writer . write ( "output := &$T{}" , outputSymbol ) ; writer . write ( "out.Result = output" ) ; writer . write ( "" ) ; writer . addUseImports ( SmithyGoDependency . XML ) ; writer . addUseImports ( SmithyGoDependency . SMITHY_XML ) ; writer . addUseImports ( SmithyGoDependency . IO ) ; writer . addUseImports ( SmithyGoDependency . SMITHY_IO ) ; writer . write ( "var buff [1024]byte" ) ; writer . write ( "ringBuffer := smithyio.NewRingBuffer(buff[:])" ) ; writer . write ( "body := io.TeeReader(response.Body, ringBuffer)" ) ; writer . write ( "rootDecoder := xml.NewDecoder(body)" ) ; writer . write ( "decoder := smithyxml.WrapNodeDecoder(rootDecoder, xml.StartElement{})" ) ; String deserFuncName = ProtocolGenerator . getDocumentDeserializerFunctionName ( outputShape , service , protocolName ) ; writer . addUseImports ( SmithyGoDependency . IO ) ; writer . write ( "err = $L(&output, decoder)" , deserFuncName ) ; writer . addUseImports ( SmithyGoDependency . IO ) ; writer . write ( "if err == io.EOF { err = nil }" ) ; XmlProtocolUtils . handleDecodeError ( writer , "out, metadata," ) ; writer . write ( "" ) ; writer . write ( "return out, metadata, err" ) ; } ) ; }
writes a custom deserializer middleware for the provided operation.
writes a custom deserializer middleware for the provided operation .
// initialize out.Result as output structure shape // define a decoder with empty start element since we s3 does not wrap Location Constraint // xml tag with operation specific xml tag. // delegate to already generated inner body deserializer function. // EOF error is valid in this case, as we provide a NOP start element at start. // Note that we correctly handle unexpected EOF.