From 0b8259cf50ffbddcc8eb88a7ffc721610455a576 Mon Sep 17 00:00:00 2001 From: nathanhit Date: Mon, 26 Aug 2024 14:30:35 -0400 Subject: [PATCH 1/3] Deserializer Implementation w/o Error Deserialization --- .../aws/AwsJson10ProtocolGenerator.java | 65 ++---- .../protocol/aws/DeserializeMiddleware.java | 219 ++++++++++++++++++ .../protocol/JsonDeserializerGenerator.java | 33 +-- 3 files changed, 256 insertions(+), 61 deletions(-) create mode 100644 codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/DeserializeMiddleware.java diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/AwsJson10ProtocolGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/AwsJson10ProtocolGenerator.java index 1edc8ce0..8d1b62ab 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/AwsJson10ProtocolGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/AwsJson10ProtocolGenerator.java @@ -16,17 +16,15 @@ package software.amazon.smithy.go.codegen.protocol.aws; import static software.amazon.smithy.go.codegen.ApplicationProtocol.createDefaultHttpApplicationProtocol; -import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; import static software.amazon.smithy.go.codegen.serde.SerdeUtil.getShapesToSerde; import java.util.HashSet; -import java.util.Map; import java.util.Set; import software.amazon.smithy.aws.traits.protocols.AwsJson1_0Trait; import software.amazon.smithy.go.codegen.ApplicationProtocol; import software.amazon.smithy.go.codegen.GoWriter; -import software.amazon.smithy.go.codegen.SmithyGoDependency; import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; +import software.amazon.smithy.go.codegen.server.protocol.JsonDeserializerGenerator; import software.amazon.smithy.go.codegen.server.protocol.JsonSerializerGenerator; import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.Shape; @@ -72,11 +70,24 @@ public void generateSharedSerializers(GenerationContext context, GoWriter writer @Override public void generateResponseDeserializers(GenerationContext context) { var writer = context.getWriter().get(); - var model = context.getModel(); - var ops = model.getOperationShapes(); + var ops = context.getModel().getOperationShapes(); for (var op : ops) { - responseDeserializerCode(op, writer); + var middle = new DeserializeMiddleware(context, op, writer); + writer.write(middle.generate()); + writer.write("\n"); + } + generateSharedDeserializers(context, writer, ops); + } + + private void generateSharedDeserializers(GenerationContext context, GoWriter writer, Set ops) { + Set shared = new HashSet<>(); + for (var op : ops) { + Set shapes = getShapesToSerde(context.getModel(), context.getModel().expectShape( + op.getOutputShape())); + shared.addAll(shapes); } + var generator = new JsonDeserializerGenerator(context.getModel(), context.getSymbolProvider()); + writer.write(generator.generate(shared)); } @Override @@ -98,46 +109,4 @@ public void generateProtocolDocumentUnmarshalerMarshalDocument(GenerationContext public void generateProtocolDocumentUnmarshalerUnmarshalDocument(GenerationContext context) { // TODO } - - private void responseDeserializerCode(OperationShape op, GoWriter writer) { - var opName = "awsAwsjson10_deserializeOp" + op.toShapeId().getName(); - - /* Struct Definition */ - var struct = goTemplate(""" - type $L struct{ - } - """, opName - ); - writer.write(struct); - - - //ID Function - var idFunction = goTemplate(""" - func (op *$L) ID() string { - return "OperationDeserializer" - } - """, opName - ); - writer.write(idFunction); - - //Handle Serialize Function - var handleFunction = goTemplate( - """ - func (op *$structName:L) HandleDeserialize (ctx $context:T, in $input:T, next $handler:T) ( - out $output:T, metadata $metadata:T, err error) { - return out, metadata, err - } - """, Map.of( - "context", SmithyGoDependency.CONTEXT.interfaceSymbol("Context"), - "input", SmithyGoDependency.SMITHY_MIDDLEWARE.struct("DeserializeInput"), - "handler", SmithyGoDependency.SMITHY_MIDDLEWARE.struct("DeserializeHandler"), - "output", SmithyGoDependency.SMITHY_MIDDLEWARE.struct("DeserializeOutput"), - "metadata", SmithyGoDependency.SMITHY_MIDDLEWARE.struct("Metadata"), - "structName", opName - )); - writer.write(handleFunction); - /* Operation End */ - writer.write("\n"); - } } - diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/DeserializeMiddleware.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/DeserializeMiddleware.java new file mode 100644 index 00000000..abb42aa5 --- /dev/null +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/DeserializeMiddleware.java @@ -0,0 +1,219 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package software.amazon.smithy.go.codegen.protocol.aws; + +import static software.amazon.smithy.go.codegen.GoWriter.goTemplate; +import static software.amazon.smithy.go.codegen.SmithyGoDependency.SMITHY_HTTP_TRANSPORT; +import static software.amazon.smithy.go.codegen.protocol.ProtocolUtil.hasEventStream; +import static software.amazon.smithy.go.codegen.server.protocol.JsonDeserializerGenerator.getDeserializerName; + +import software.amazon.smithy.go.codegen.GoStdlibTypes; +import software.amazon.smithy.go.codegen.GoWriter; +import software.amazon.smithy.go.codegen.SmithyGoDependency; +import software.amazon.smithy.go.codegen.integration.ProtocolGenerator; +import software.amazon.smithy.model.shapes.OperationShape; +import software.amazon.smithy.model.shapes.StructureShape; +import software.amazon.smithy.utils.MapUtils; + +public class DeserializeMiddleware { + + public static final String SMITHY_PROTOCOL_NAME = "awsJson10"; + protected final ProtocolGenerator.GenerationContext ctx; + protected final OperationShape operation; + protected final GoWriter writer; + + protected final StructureShape input; + protected final StructureShape output; + + private String deserialName; + + public DeserializeMiddleware( + ProtocolGenerator.GenerationContext ctx, OperationShape operation, GoWriter writer + ) { + this.ctx = ctx; + this.operation = operation; + this.writer = writer; + + this.input = ctx.getModel().expectShape(operation.getInputShape(), StructureShape.class); + this.output = ctx.getModel().expectShape(operation.getOutputShape(), StructureShape.class); + } + + public static String getMiddlewareName(OperationShape operation) { + return "awsAwsjson10_deserializeOp" + operation.toShapeId().getName(); + } + + public GoWriter.Writable generate() { + deserialName = getMiddlewareName(operation); + + return goTemplate(""" + + type $opName:L struct{ + } + + func (op *$opName:L) ID() string { + return "OperationDeserializer" + } + + $handleSerialize:W + + """, + MapUtils.of( + "opName", deserialName, + "handleSerialize", generateHandleDeserialize() + )); + } + + private GoWriter.Writable generateHandleDeserialize() { + return goTemplate( + """ + + func (op *$opName:L) HandleDeserialize (ctx $context:T, in $input:T, next $handler:T) ( + out $output:T, metadata $metadata:T, err error) { + + $body:W + + return out, metadata, nil + } + + """, MapUtils.of( + "context", SmithyGoDependency.CONTEXT.interfaceSymbol("Context"), + "input", SmithyGoDependency.SMITHY_MIDDLEWARE.struct("DeserializeInput"), + "handler", SmithyGoDependency.SMITHY_MIDDLEWARE.struct("DeserializeHandler"), + "output", SmithyGoDependency.SMITHY_MIDDLEWARE.struct("DeserializeOutput"), + "metadata", SmithyGoDependency.SMITHY_MIDDLEWARE.struct("Metadata"), + "opName", deserialName, + "body", generateHandleDeserializeBody()) + ); + } + + private GoWriter.Writable generateHandleDeserializeBody() { + return goTemplate(""" + $errors:W + + $response:W + """, + MapUtils.of( + "errors", handleResponseChecks(), + "response", handleResponse() + )); + } + + + private GoWriter.Writable handleResponseChecks() { + return goTemplate(""" + + out, metadata, err = next.HandleDeserialize(ctx, in) + if err != nil { + return out, metadata, err + } + + resp, ok := out.RawResponse.($response:P) + if !ok { + return out, metadata, $errorf:T("unexpected transport type %T", out.RawResponse) + } + + if resp.Header.Get("smithy-protocol") != $protocol:S { + return out, metadata, &$deserError:T{ + Err: $errorf:T( + "unexpected smithy-protocol response header '%s' (HTTP status: %s)", + resp.Header.Get("smithy-protocol"), + resp.Status, + ), + } + } + + if resp.StatusCode != 200 { + return out, metadata, &$deserError:T{} + } + + """, + MapUtils.of( + "response", SMITHY_HTTP_TRANSPORT.pointableSymbol("Response"), + "errorf", GoStdlibTypes.Fmt.Errorf, + "deserError", SmithyGoDependency.SMITHY.struct("DeserializationError"), + "protocol", SMITHY_PROTOCOL_NAME + )); + } + + private GoWriter.Writable handleResponse() { + if (output.members().isEmpty()) { + return discardDeserialize(); + } else if (hasEventStream(ctx.getModel(), output)) { + return deserializeEventStream(); + } + return handlePayload(); + } + + private GoWriter.Writable handlePayload() { + return goTemplate(""" + payload, err := $readAll:T(resp.Body) + if err != nil { + return out, metadata, err + } + + if len(payload) == 0 { + out.Result = &$output:T{} + return out, metadata, nil + } + + decoder := $decoder:T(resp.Body) + var cv map[string]interface{} + err = decoder.Decode(&cv) + if err!= nil { + return out, metadata, err + } + + output, err := $deserialize:L(cv) + if err != nil { + return out, metadata, err + } + + out.Result = output + """, + MapUtils.of( + "readAll", GoStdlibTypes.Io.ReadAll, + "output", ctx.getSymbolProvider() + .toSymbol(ctx.getModel().expectShape(operation.getOutputShape())), + "decoder", GoStdlibTypes.Encoding.Json.NewDecoder, + "deserialize", getDeserializerName(output) + )); + } + + private GoWriter.Writable discardDeserialize() { + return goTemplate(""" + if _, err = $copy:T($discard:T, resp.Body); err != nil { + return out, metadata, $errorf:T("discard response body: %w", err) + } + + out.Result = &$result:T{} + """, + MapUtils.of( + "copy", GoStdlibTypes.Io.Copy, + "discard", GoStdlibTypes.Io.IoUtil.Discard, + "errorf", GoStdlibTypes.Fmt.Errorf, + "result", ctx.getSymbolProvider().toSymbol(output) + )); + } + + // Basically a no-op. Event stream deserializer middleware, implemented elsewhere, will handle the wire-up here, + // including handling the initial-response message to deserialize any non-stream members to output. + // Taken straight from CBOR implementation + private GoWriter.Writable deserializeEventStream() { + return goTemplate("out.Result = &$T{}", ctx.getSymbolProvider().toSymbol(output)); + } +} + + diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java index 9cf4e498..f6c9cc80 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java @@ -89,7 +89,7 @@ private GoWriter.Writable generateOpaqueAssert(Shape shape) { case MAP, STRUCTURE, UNION -> goTemplate("map[string]interface{}"); default -> - throw new CodegenException("Unsupported: " + shape.getType()); + throw new CodegenException("Unsupported: " + shape.getType() + " ShapeID: " + shape.getId()); }; } @@ -110,18 +110,21 @@ private GoWriter.Writable generateZeroValue(Shape shape) { case TIMESTAMP -> goTemplate("$T{}", GoStdlibTypes.Time.Time); default -> - throw new CodegenException("Unsupported: " + shape.getType()); + throw new CodegenException("Unsupported: " + shape.getType() + " ShapeID: " + shape.getId()); }; } private GoWriter.Writable generateDeserializeAssertedValue(Shape shape, String ident) { return switch (shape.getType()) { - case BYTE -> generateDeserializeIntegral(ident, "int8", Byte.MIN_VALUE, Byte.MAX_VALUE); - case SHORT -> generateDeserializeIntegral(ident, "int16", Short.MIN_VALUE, Short.MAX_VALUE); - case INTEGER -> generateDeserializeIntegral(ident, "int32", Integer.MIN_VALUE, Integer.MAX_VALUE); - case LONG -> generateDeserializeIntegral(ident, "int64", Long.MIN_VALUE, Long.MAX_VALUE); + case BYTE -> generateDeserializeIntegral(ident, "int8", "Int64", Byte.MIN_VALUE, Byte.MAX_VALUE); + case SHORT -> generateDeserializeIntegral(ident, "int16", "Int64", Short.MIN_VALUE, Short.MAX_VALUE); + case INTEGER -> generateDeserializeIntegral(ident, "int32", "Int64", Integer.MIN_VALUE, Integer.MAX_VALUE); + case LONG -> generateDeserializeIntegral(ident, "int64", "Int64", Long.MIN_VALUE, Long.MAX_VALUE); case STRING, BOOLEAN -> goTemplate("return $L, nil", ident); - case ENUM -> goTemplate("return $T($L), nil", symbolProvider.toSymbol(shape), ident); + //Int_Enum implementation needs to be tested + case ENUM, INT_ENUM -> goTemplate("return $T($L), nil", symbolProvider.toSymbol(shape), ident); + case FLOAT -> generateDeserializeIntegral(ident, "float32", "Float64", + (long) Float.MIN_VALUE, (long) Float.MAX_VALUE); case BLOB -> goTemplate(""" p, err := $b64:T.DecodeString($ident:L) if err != nil { @@ -243,20 +246,21 @@ yield goTemplate(""" ).compose(false) )); case TIMESTAMP -> goTemplate(""" - dts, err := $T(serializedValue) + dts, err := $T(av) if err != nil { - return nil, err + return time.Time{}, err } return dts, nil """, SmithyGoTypes.Time.ParseDateTime); default -> - throw new CodegenException("Unsupported: " + shape.getType()); + throw new CodegenException("Unsupported: " + shape.getType() + " ShapeID: " + shape.getId()); }; } - private GoWriter.Writable generateDeserializeIntegral(String ident, String castTo, long min, long max) { + private GoWriter.Writable generateDeserializeIntegral(String ident, String castTo, String typecast, + long min, long max) { return goTemplate(""" - $nextident:L, err := $ident:L.Int64() + $nextident:L, err := $ident:L.$typecast:L() if err != nil { return 0, err } @@ -271,7 +275,8 @@ private GoWriter.Writable generateDeserializeIntegral(String ident, String castT "nextident", ident + "_", "min", min, "max", max, - "cast", castTo + "cast", castTo, + "typecast", typecast )); } @@ -287,6 +292,8 @@ private GoWriter.Writable generateStructFieldDeref(MemberShape member, String id case LONG -> goTemplate("$T($L)", SmithyGoTypes.Ptr.Int64, ident); case STRING -> goTemplate("$T($L)", SmithyGoTypes.Ptr.String, ident); case BOOLEAN -> goTemplate("$T($L)", SmithyGoTypes.Ptr.Bool, ident); + case FLOAT -> goTemplate("$T($L)", SmithyGoTypes.Ptr.Float32, ident); + case TIMESTAMP -> goTemplate("$T($L)", SmithyGoTypes.Ptr.Time, ident); default -> goTemplate(ident); }; } From 9cbcce86c3b0e274cc8e17ce799126133f24ec53 Mon Sep 17 00:00:00 2001 From: nathanhit Date: Tue, 27 Aug 2024 16:31:05 -0400 Subject: [PATCH 2/3] DeserializeMiddleware Revision 1 --- .../protocol/aws/DeserializeMiddleware.java | 32 +++++---------- .../protocol/aws/SerializeMiddleware.java | 4 +- .../protocol/JsonDeserializerGenerator.java | 40 ++++++++++++++----- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/DeserializeMiddleware.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/DeserializeMiddleware.java index abb42aa5..7b03233b 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/DeserializeMiddleware.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/DeserializeMiddleware.java @@ -30,7 +30,6 @@ public class DeserializeMiddleware { - public static final String SMITHY_PROTOCOL_NAME = "awsJson10"; protected final ProtocolGenerator.GenerationContext ctx; protected final OperationShape operation; protected final GoWriter writer; @@ -49,6 +48,8 @@ public DeserializeMiddleware( this.input = ctx.getModel().expectShape(operation.getInputShape(), StructureShape.class); this.output = ctx.getModel().expectShape(operation.getOutputShape(), StructureShape.class); + + deserialName = getMiddlewareName(operation); } public static String getMiddlewareName(OperationShape operation) { @@ -56,8 +57,6 @@ public static String getMiddlewareName(OperationShape operation) { } public GoWriter.Writable generate() { - deserialName = getMiddlewareName(operation); - return goTemplate(""" type $opName:L struct{ @@ -125,17 +124,7 @@ private GoWriter.Writable handleResponseChecks() { return out, metadata, $errorf:T("unexpected transport type %T", out.RawResponse) } - if resp.Header.Get("smithy-protocol") != $protocol:S { - return out, metadata, &$deserError:T{ - Err: $errorf:T( - "unexpected smithy-protocol response header '%s' (HTTP status: %s)", - resp.Header.Get("smithy-protocol"), - resp.Status, - ), - } - } - - if resp.StatusCode != 200 { + if resp.StatusCode < 200 || resp.StatusCode >= 300 { return out, metadata, &$deserError:T{} } @@ -143,8 +132,7 @@ private GoWriter.Writable handleResponseChecks() { MapUtils.of( "response", SMITHY_HTTP_TRANSPORT.pointableSymbol("Response"), "errorf", GoStdlibTypes.Fmt.Errorf, - "deserError", SmithyGoDependency.SMITHY.struct("DeserializationError"), - "protocol", SMITHY_PROTOCOL_NAME + "deserError", SmithyGoDependency.SMITHY.struct("DeserializationError") )); } @@ -170,16 +158,16 @@ private GoWriter.Writable handlePayload() { } decoder := $decoder:T(resp.Body) - var cv map[string]interface{} - err = decoder.Decode(&cv) + var jv map[string]interface{} + err = decoder.Decode(&jv) if err!= nil { return out, metadata, err } - output, err := $deserialize:L(cv) - if err != nil { - return out, metadata, err - } + output, err := $deserialize:L(jv) + if err != nil { + return out, metadata, err + } out.Result = output """, diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/SerializeMiddleware.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/SerializeMiddleware.java index 9dd9ee12..a0582431 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/SerializeMiddleware.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/protocol/aws/SerializeMiddleware.java @@ -49,6 +49,8 @@ public SerializeMiddleware( this.input = ctx.getModel().expectShape(operation.getInputShape(), StructureShape.class); this.output = ctx.getModel().expectShape(operation.getOutputShape(), StructureShape.class); + + serialName = getMiddlewareName(operation); } public static String getMiddlewareName(OperationShape operation) { @@ -56,8 +58,6 @@ public static String getMiddlewareName(OperationShape operation) { } public GoWriter.Writable generate() { - serialName = getMiddlewareName(operation); - return goTemplate(""" type $opName:L struct{ diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java index f6c9cc80..c540f57e 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java @@ -116,14 +116,13 @@ private GoWriter.Writable generateZeroValue(Shape shape) { private GoWriter.Writable generateDeserializeAssertedValue(Shape shape, String ident) { return switch (shape.getType()) { - case BYTE -> generateDeserializeIntegral(ident, "int8", "Int64", Byte.MIN_VALUE, Byte.MAX_VALUE); - case SHORT -> generateDeserializeIntegral(ident, "int16", "Int64", Short.MIN_VALUE, Short.MAX_VALUE); - case INTEGER -> generateDeserializeIntegral(ident, "int32", "Int64", Integer.MIN_VALUE, Integer.MAX_VALUE); - case LONG -> generateDeserializeIntegral(ident, "int64", "Int64", Long.MIN_VALUE, Long.MAX_VALUE); + case BYTE -> generateDeserializeIntegral(ident, "int8", Byte.MIN_VALUE, Byte.MAX_VALUE); + case SHORT -> generateDeserializeIntegral(ident, "int16", Short.MIN_VALUE, Short.MAX_VALUE); + case INTEGER -> generateDeserializeIntegral(ident, "int32", Integer.MIN_VALUE, Integer.MAX_VALUE); + case LONG -> generateDeserializeIntegral(ident, "int64", Long.MIN_VALUE, Long.MAX_VALUE); case STRING, BOOLEAN -> goTemplate("return $L, nil", ident); - //Int_Enum implementation needs to be tested case ENUM, INT_ENUM -> goTemplate("return $T($L), nil", symbolProvider.toSymbol(shape), ident); - case FLOAT -> generateDeserializeIntegral(ident, "float32", "Float64", + case FLOAT -> generateDeserializeFloat(ident, "float32", (long) Float.MIN_VALUE, (long) Float.MAX_VALUE); case BLOB -> goTemplate(""" p, err := $b64:T.DecodeString($ident:L) @@ -257,10 +256,9 @@ yield goTemplate(""" }; } - private GoWriter.Writable generateDeserializeIntegral(String ident, String castTo, String typecast, - long min, long max) { + private GoWriter.Writable generateDeserializeIntegral(String ident, String castTo, long min, long max) { return goTemplate(""" - $nextident:L, err := $ident:L.$typecast:L() + $nextident:L, err := $ident:L.Int64() if err != nil { return 0, err } @@ -275,8 +273,28 @@ private GoWriter.Writable generateDeserializeIntegral(String ident, String castT "nextident", ident + "_", "min", min, "max", max, - "cast", castTo, - "typecast", typecast + "cast", castTo + )); + } + + private GoWriter.Writable generateDeserializeFloat(String ident, String castTo, long min, long max) { + return goTemplate(""" + $nextident:L, err := $ident:L.Float64() + if err != nil { + return 0, err + } + if $nextident:L < $min:L || $nextident:L > $max:L { + return 0, $errorf:T("invalid") + } + return $cast:L($nextident:L), nil + """, + MapUtils.of( + "errorf", GoStdlibTypes.Fmt.Errorf, + "ident", ident, + "nextident", ident + "_", + "min", min, + "max", max, + "cast", castTo )); } From 65601f136ce4e0cd4cb640affd14df11067dbdd6 Mon Sep 17 00:00:00 2001 From: nathanhit Date: Tue, 27 Aug 2024 16:55:06 -0400 Subject: [PATCH 3/3] Deserializers Revision 2 - JsonDeserialzieGenerator Float Function fixed --- .../server/protocol/JsonDeserializerGenerator.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java index c540f57e..7b9f7a3d 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/server/protocol/JsonDeserializerGenerator.java @@ -122,8 +122,7 @@ private GoWriter.Writable generateDeserializeAssertedValue(Shape shape, String i case LONG -> generateDeserializeIntegral(ident, "int64", Long.MIN_VALUE, Long.MAX_VALUE); case STRING, BOOLEAN -> goTemplate("return $L, nil", ident); case ENUM, INT_ENUM -> goTemplate("return $T($L), nil", symbolProvider.toSymbol(shape), ident); - case FLOAT -> generateDeserializeFloat(ident, "float32", - (long) Float.MIN_VALUE, (long) Float.MAX_VALUE); + case FLOAT -> generateDeserializeFloat(ident); case BLOB -> goTemplate(""" p, err := $b64:T.DecodeString($ident:L) if err != nil { @@ -277,7 +276,7 @@ private GoWriter.Writable generateDeserializeIntegral(String ident, String castT )); } - private GoWriter.Writable generateDeserializeFloat(String ident, String castTo, long min, long max) { + private GoWriter.Writable generateDeserializeFloat(String ident) { return goTemplate(""" $nextident:L, err := $ident:L.Float64() if err != nil { @@ -286,15 +285,14 @@ private GoWriter.Writable generateDeserializeFloat(String ident, String castTo, if $nextident:L < $min:L || $nextident:L > $max:L { return 0, $errorf:T("invalid") } - return $cast:L($nextident:L), nil + return float32($nextident:L), nil """, MapUtils.of( "errorf", GoStdlibTypes.Fmt.Errorf, "ident", ident, "nextident", ident + "_", - "min", min, - "max", max, - "cast", castTo + "min", Float.MIN_VALUE, + "max", Float.MAX_VALUE )); }