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

Smithy 1.9 Support #316

Merged
merged 1 commit into from
Jul 13, 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 @@ -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";
Expand All @@ -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
*/
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,14 @@ protected HttpProtocolUnitTestGenerator(Builder<T> 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);
}
Expand Down Expand Up @@ -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);
});
}

/**
Expand Down
9 changes: 7 additions & 2 deletions encoding/httpbinding/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion encoding/httpbinding/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpbinding

import (
"encoding/base64"
"math"
"math/big"
"net/http"
"strconv"
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion encoding/httpbinding/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package httpbinding

import (
"encoding/base64"
"math"
"math/big"
"net/url"
"strconv"
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion encoding/httpbinding/uri.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package httpbinding

import (
"math"
"math/big"
"strconv"
"strings"
Expand Down Expand Up @@ -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
Expand Down