From 40d50fb193ed47eefeb690aa82de53d4c00bf96e Mon Sep 17 00:00:00 2001 From: Sean McGrail Date: Fri, 9 Jul 2021 23:44:57 -0700 Subject: [PATCH] Smithy 1.9 Support --- .../smithy/go/codegen/SmithyGoDependency.java | 5 ++- .../HttpProtocolUnitTestGenerator.java | 37 +++++++++++++------ encoding/httpbinding/encode.go | 9 ++++- encoding/httpbinding/header.go | 12 +++++- encoding/httpbinding/query.go | 12 +++++- encoding/httpbinding/uri.go | 12 +++++- 6 files changed, 70 insertions(+), 17 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java index b5eb971ee..41301ad53 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/SmithyGoDependency.java @@ -59,6 +59,7 @@ public final class SmithyGoDependency { public static final GoDependency GO_CMP_OPTIONS = goCmp("cmp/cmpopts"); public static final GoDependency GO_JMESPATH = goJmespath(null); + public static final GoDependency MATH = stdlib("math"); private static final String SMITHY_SOURCE_PATH = "github.com/aws/smithy-go"; private static final String GO_CMP_SOURCE_PATH = "github.com/google/go-cmp"; @@ -69,6 +70,7 @@ private SmithyGoDependency() { /** * Get a {@link GoDependency} representing the standard library package import path. + * * @param importPath standard library import path * @return the {@link GoDependency} for the package import path */ @@ -78,8 +80,9 @@ public static GoDependency stdlib(String importPath) { /** * Get a {@link GoDependency} representing the standard library package import path with the given alias. + * * @param importPath standard library package import path - * @param alias the package alias + * @param alias the package alias * @return the {@link GoDependency} for the package import path */ public static GoDependency stdlib(String importPath, String alias) { diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpProtocolUnitTestGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpProtocolUnitTestGenerator.java index cbc37fed0..66ea1c054 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpProtocolUnitTestGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpProtocolUnitTestGenerator.java @@ -86,12 +86,14 @@ protected HttpProtocolUnitTestGenerator(Builder builder) { opSymbol = symbolProvider.toSymbol(operation); inputShape = model.expectShape(operation.getInput() - .orElseThrow(() -> new CodegenException("missing input shape for operation: " + operation.getId())), + .orElseThrow(() -> new CodegenException("missing input shape for operation: " + + operation.getId())), StructureShape.class); inputSymbol = symbolProvider.toSymbol(inputShape); outputShape = model.expectShape(operation.getOutput() - .orElseThrow(() -> new CodegenException("missing output shape for operation: " + operation.getId())), + .orElseThrow(() -> new CodegenException("missing output shape for operation: " + + operation.getId())), StructureShape.class); outputSymbol = symbolProvider.toSymbol(outputShape); } @@ -440,17 +442,30 @@ protected void writeAssertComplexEqual( GoWriter writer, String expect, String actual, String[] ignoreTypes ) { writer.addUseImports(SmithyGoDependency.SMITHY_TESTING); + writer.addUseImports(SmithyGoDependency.GO_CMP); writer.addUseImports(SmithyGoDependency.GO_CMP_OPTIONS); + writer.addUseImports(SmithyGoDependency.MATH); + + writer.openBlock("opts := cmp.Options{", "}", () -> { + writer.openBlock("cmpopts.IgnoreUnexported(", "),", () -> { + for (String ignoreType : ignoreTypes) { + writer.write("$L,", ignoreType); + } + }); + writer.write("cmp.FilterValues(func(x, y float64) bool {\n" + + "\treturn math.IsNaN(x) && math.IsNaN(y)\n" + + "}," + + "cmp.Comparer(func(_, _ interface{}) bool { return true })),"); + writer.write("cmp.FilterValues(func(x, y float32) bool {\n" + + "\treturn math.IsNaN(float64(x)) && math.IsNaN(float64(y))\n" + + "}," + + "cmp.Comparer(func(_, _ interface{}) bool { return true })),"); + }); - writer.writeInline("if err := smithytesting.CompareValues($L, $L, cmpopts.IgnoreUnexported(", expect, actual); - - for (String ignoreType : ignoreTypes) { - writer.write("$L,", ignoreType); - } - - writer.writeInline(")); err != nil {"); - writer.write(" t.Errorf(\"expect $L value match:\\n%v\", err)", expect); - writer.write("}"); + writer.openBlock("if err := smithytesting.CompareValues($L, $L, opts...); err != nil {", "}", + expect, actual, () -> { + writer.write("t.Errorf(\"expect $L value match:\\n%v\", err)", expect); + }); } /** diff --git a/encoding/httpbinding/encode.go b/encoding/httpbinding/encode.go index 2314a6e73..9b3eec4c1 100644 --- a/encoding/httpbinding/encode.go +++ b/encoding/httpbinding/encode.go @@ -8,7 +8,12 @@ import ( "strings" ) -const contentLengthHeader = "Content-Length" +const ( + contentLengthHeader = "Content-Length" + floatNaN = "NaN" + floatInfinity = "Infinity" + floatNegInfinity = "-Infinity" +) // An Encoder provides encoding of REST URI path, query, and header components // of an HTTP request. Can also encode a stream as the payload. @@ -106,6 +111,6 @@ func (e *Encoder) AddQuery(key string) QueryValue { // HasQuery returns if a query with the key specified exists with one or // more value. -func(e *Encoder) HasQuery(key string) bool { +func (e *Encoder) HasQuery(key string) bool { return len(e.query.Get(key)) != 0 } diff --git a/encoding/httpbinding/header.go b/encoding/httpbinding/header.go index 618ef6585..f9256e175 100644 --- a/encoding/httpbinding/header.go +++ b/encoding/httpbinding/header.go @@ -2,6 +2,7 @@ package httpbinding import ( "encoding/base64" + "math" "math/big" "net/http" "strconv" @@ -88,7 +89,16 @@ func (h HeaderValue) Double(v float64) { } func (h HeaderValue) float(v float64, bitSize int) { - h.modifyHeader(strconv.FormatFloat(v, 'f', -1, bitSize)) + switch { + case math.IsNaN(v): + h.String(floatNaN) + case math.IsInf(v, 1): + h.String(floatInfinity) + case math.IsInf(v, -1): + h.String(floatNegInfinity) + default: + h.modifyHeader(strconv.FormatFloat(v, 'f', -1, bitSize)) + } } // BigInteger encodes the value v as a query string value diff --git a/encoding/httpbinding/query.go b/encoding/httpbinding/query.go index ee7791f51..c2e7d0a20 100644 --- a/encoding/httpbinding/query.go +++ b/encoding/httpbinding/query.go @@ -2,6 +2,7 @@ package httpbinding import ( "encoding/base64" + "math" "math/big" "net/url" "strconv" @@ -79,7 +80,16 @@ func (qv QueryValue) Double(v float64) { } func (qv QueryValue) float(v float64, bitSize int) { - qv.updateKey(strconv.FormatFloat(v, 'f', -1, bitSize)) + switch { + case math.IsNaN(v): + qv.String(floatNaN) + case math.IsInf(v, 1): + qv.String(floatInfinity) + case math.IsInf(v, -1): + qv.String(floatNegInfinity) + default: + qv.updateKey(strconv.FormatFloat(v, 'f', -1, bitSize)) + } } // BigInteger encodes v as a query string value diff --git a/encoding/httpbinding/uri.go b/encoding/httpbinding/uri.go index 89cfae6d6..64e40121e 100644 --- a/encoding/httpbinding/uri.go +++ b/encoding/httpbinding/uri.go @@ -1,6 +1,7 @@ package httpbinding import ( + "math" "math/big" "strconv" "strings" @@ -64,7 +65,16 @@ func (u URIValue) Double(v float64) error { } func (u URIValue) float(v float64, bitSize int) error { - return u.modifyURI(strconv.FormatFloat(v, 'f', -1, bitSize)) + switch { + case math.IsNaN(v): + return u.String(floatNaN) + case math.IsInf(v, 1): + return u.String(floatInfinity) + case math.IsInf(v, -1): + return u.String(floatNegInfinity) + default: + return u.modifyURI(strconv.FormatFloat(v, 'f', -1, bitSize)) + } } // BigInteger encodes v as a query string value