From a41ec935dcf5db25c445ae2587401ebdc5c91b5c Mon Sep 17 00:00:00 2001 From: Steven Yuan Date: Fri, 11 Nov 2022 19:50:09 +0000 Subject: [PATCH] Update intEnum generation to use type alias Due to type definitions requiring strict casting and implementation overhead in protocol generation, this commit uses type aliases to generate intEnums as an intermediate step until migrating back to type definitions. This change is backwards compatible except for the removed `Values()` method that was on intEnum type definitions, but not definable on type aliases since defining methods on base types (`int32`) is not permitted. --- .../smithy/go/codegen/CodegenUtils.java | 1 + .../smithy/go/codegen/IntEnumGenerator.java | 8 ++++++- .../go/codegen/ShapeValueGenerator.java | 2 ++ .../HttpBindingProtocolGenerator.java | 2 ++ .../expected/types/enums.go | 23 +------------------ 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenUtils.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenUtils.java index 626a7eb57..cdd4ccd4e 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenUtils.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/CodegenUtils.java @@ -419,6 +419,7 @@ public static boolean isNumber(Shape shape) { case BYTE: case SHORT: case INTEGER: + case INT_ENUM: case LONG: case FLOAT: case DOUBLE: diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/IntEnumGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/IntEnumGenerator.java index 7b101c492..cf4059183 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/IntEnumGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/IntEnumGenerator.java @@ -48,7 +48,9 @@ final class IntEnumGenerator implements Runnable { public void run() { Symbol symbol = symbolProvider.toSymbol(shape); - writer.write("type $L int32", symbol.getName()).write(""); + // TODO(smithy): Use type alias instead of type definition until refactoring + // protocol generators is prioritized. + writer.write("type $L = int32", symbol.getName()).write(""); writer.writeDocs(String.format("Enum values for %s", symbol.getName())); Set constants = new LinkedHashSet<>(); @@ -84,6 +86,9 @@ public void run() { } }).write(""); + // TODO(smithy): type aliases don't allow defining methods on base types (e.g. int32). + // Uncomment generating the Value() method when the type alias is migrated to type definition. + /* writer.writeDocs(String.format("Values returns all known values for %s. Note that this can be expanded in the " + "future, and so it is only as up to date as the client.%n%nThe ordering of this slice is not " + "guaranteed to be stable across updates.", symbol.getName())); @@ -94,5 +99,6 @@ public void run() { } }); }); + */ } } diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ShapeValueGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ShapeValueGenerator.java index 661554719..82b79da35 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ShapeValueGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/ShapeValueGenerator.java @@ -304,6 +304,7 @@ protected void writeScalarPointerInline(GoWriter writer, MemberShape member, Nod funcName = "Int16"; break; case INTEGER: + case INT_ENUM: funcName = "Int32"; break; case LONG: @@ -669,6 +670,7 @@ public Void numberNode(NumberNode node) { case BYTE: case SHORT: case INTEGER: + case INT_ENUM: case LONG: case FLOAT: case DOUBLE: diff --git a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpBindingProtocolGenerator.java b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpBindingProtocolGenerator.java index b04e1442d..7e6139e5d 100644 --- a/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpBindingProtocolGenerator.java +++ b/codegen/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/integration/HttpBindingProtocolGenerator.java @@ -767,6 +767,7 @@ private void writeHttpBindingSetter( locationEncoder.accept(writer, "Short(" + operand + ")"); break; case INTEGER: + case INT_ENUM: locationEncoder.accept(writer, "Integer(" + operand + ")"); break; case LONG: @@ -1211,6 +1212,7 @@ private String generateHttpHeaderValue( writer.write("if err != nil { return err }"); return "int16(vv)"; case INTEGER: + case INT_ENUM: writer.addUseImports(SmithyGoDependency.STRCONV); writer.write("vv, err := strconv.ParseInt($L, 0, 32)", operand); writer.write("if err != nil { return err }"); diff --git a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/smithy-tests/int-enum-shape-test/expected/types/enums.go b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/smithy-tests/int-enum-shape-test/expected/types/enums.go index 038302c35..ea165773b 100644 --- a/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/smithy-tests/int-enum-shape-test/expected/types/enums.go +++ b/codegen/smithy-go-codegen/src/main/resources/software/amazon/smithy/go/codegen/smithy-tests/int-enum-shape-test/expected/types/enums.go @@ -3,7 +3,7 @@ package types -type Number int32 +type Number = int32 // Enum values for Number const ( @@ -22,27 +22,6 @@ const ( NumberKing Number = 13 ) -// Values returns all known values for Number. Note that this can be expanded in -// the future, and so it is only as up to date as the client. The ordering of this -// slice is not guaranteed to be stable across updates. -func (Number) Values() []Number { - return []Number{ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - } -} - type Suit string // Enum values for Suit