-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for intEnum code generation (#383)
- Update smithy-go-codegen-test with intEnum test - Add unit test for intEnum shape
- Loading branch information
Steven Yuan
authored
Sep 2, 2022
1 parent
fef294e
commit 9161575
Showing
10 changed files
with
325 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...n/smithy-go-codegen/src/main/java/software/amazon/smithy/go/codegen/IntEnumGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2022 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; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.logging.Logger; | ||
import software.amazon.smithy.codegen.core.Symbol; | ||
import software.amazon.smithy.codegen.core.SymbolProvider; | ||
import software.amazon.smithy.model.shapes.IntEnumShape; | ||
import software.amazon.smithy.model.shapes.MemberShape; | ||
import software.amazon.smithy.model.traits.DocumentationTrait; | ||
import software.amazon.smithy.model.traits.EnumValueTrait; | ||
import software.amazon.smithy.utils.StringUtils; | ||
|
||
/** | ||
* Renders intEnums and their constants. | ||
*/ | ||
final class IntEnumGenerator implements Runnable { | ||
private static final Logger LOGGER = Logger.getLogger(IntEnumGenerator.class.getName()); | ||
|
||
private final SymbolProvider symbolProvider; | ||
private final GoWriter writer; | ||
private final IntEnumShape shape; | ||
|
||
IntEnumGenerator(SymbolProvider symbolProvider, GoWriter writer, IntEnumShape shape) { | ||
this.symbolProvider = symbolProvider; | ||
this.writer = writer; | ||
this.shape = shape; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
Symbol symbol = symbolProvider.toSymbol(shape); | ||
|
||
writer.write("type $L int32", symbol.getName()).write(""); | ||
|
||
writer.writeDocs(String.format("Enum values for %s", symbol.getName())); | ||
Set<String> constants = new LinkedHashSet<>(); | ||
writer.openBlock("const (", ")", () -> { | ||
for (Map.Entry<String, MemberShape> entry : shape.getAllMembers().entrySet()) { | ||
StringBuilder labelBuilder = new StringBuilder(symbol.getName()); | ||
String name = entry.getKey(); | ||
|
||
for (String part : name.split("(?U)[\\W_]")) { | ||
if (part.matches(".*[a-z].*") && part.matches(".*[A-Z].*")) { | ||
// Mixed case names should not be changed other than first letter capitalized. | ||
labelBuilder.append(StringUtils.capitalize(part)); | ||
} else { | ||
// For all non-mixed case parts title case first letter, followed by all other lower cased. | ||
labelBuilder.append(StringUtils.capitalize(part.toLowerCase(Locale.US))); | ||
} | ||
} | ||
String label = labelBuilder.toString(); | ||
|
||
// If camel-casing would cause a conflict, don't camel-case this enum value. | ||
if (constants.contains(label)) { | ||
LOGGER.warning(String.format( | ||
"Multiple enums resolved to the same name, `%s`, using unaltered value for: %s", | ||
label, name)); | ||
label = name; | ||
} | ||
constants.add(label); | ||
|
||
entry.getValue().getTrait(DocumentationTrait.class) | ||
.ifPresent(trait -> writer.writeDocs(trait.getValue())); | ||
writer.write("$L $L = $L", label, symbol.getName(), | ||
entry.getValue().expectTrait(EnumValueTrait.class).expectIntValue()); | ||
} | ||
}).write(""); | ||
|
||
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())); | ||
writer.openBlock("func ($L) Values() []$L {", "}", symbol.getName(), symbol.getName(), () -> { | ||
writer.openBlock("return []$L{", "}", symbol.getName(), () -> { | ||
for (Map.Entry<String, Integer> entry : shape.getEnumValues().entrySet()) { | ||
writer.write("$L,", entry.getValue()); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...zon/smithy/go/codegen/smithy-tests/int-enum-shape-test/expected/changeCardInput.go.struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
type ChangeCardInput struct { | ||
|
||
Number types.Number | ||
|
||
Suit types.Suit | ||
|
||
noSmithyDocumentSerde | ||
} |
11 changes: 11 additions & 0 deletions
11
...on/smithy/go/codegen/smithy-tests/int-enum-shape-test/expected/changeCardOutput.go.struct
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
type ChangeCardOutput struct { | ||
|
||
Number types.Number | ||
|
||
Suit types.Suit | ||
|
||
// Metadata pertaining to the operation's result. | ||
ResultMetadata middleware.Metadata | ||
|
||
noSmithyDocumentSerde | ||
} |
66 changes: 66 additions & 0 deletions
66
...oftware/amazon/smithy/go/codegen/smithy-tests/int-enum-shape-test/expected/types/enums.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
...ware/amazon/smithy/go/codegen/smithy-tests/int-enum-shape-test/int-enum-shape-test.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.example | ||
|
||
service Example { | ||
version: "1.0.0" | ||
operations: [ | ||
ChangeCard | ||
] | ||
} | ||
|
||
operation ChangeCard { | ||
input: Card | ||
output: Card | ||
} | ||
|
||
structure Card { | ||
suit: Suit | ||
number: Number | ||
} | ||
|
||
enum Suit { | ||
DIAMOND | ||
CLUB | ||
HEART | ||
SPADE | ||
} | ||
|
||
intEnum Number { | ||
ACE = 1 | ||
TWO = 2 | ||
THREE = 3 | ||
FOUR = 4 | ||
FIVE = 5 | ||
SIX = 6 | ||
SEVEN = 7 | ||
EIGHT = 8 | ||
NINE = 9 | ||
TEN = 10 | ||
JACK = 11 | ||
QUEEN = 12 | ||
KING = 13 | ||
} |
75 changes: 75 additions & 0 deletions
75
...go-codegen/src/test/java/software/amazon/smithy/go/codegen/IntEnumShapeGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2022 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; | ||
|
||
import java.util.logging.Logger; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import software.amazon.smithy.build.MockManifest; | ||
import software.amazon.smithy.build.PluginContext; | ||
import software.amazon.smithy.go.codegen.GoCodegenPlugin; | ||
import software.amazon.smithy.model.Model; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
|
||
import static software.amazon.smithy.go.codegen.TestUtils.buildMockPluginContext; | ||
import static software.amazon.smithy.go.codegen.TestUtils.loadSmithyModelFromResource; | ||
import static software.amazon.smithy.go.codegen.TestUtils.loadExpectedFileStringFromResource; | ||
|
||
|
||
public class IntEnumShapeGeneratorTest { | ||
private static final Logger LOGGER = Logger.getLogger(IntEnumShapeGeneratorTest.class.getName()); | ||
|
||
@Test | ||
public void testIntEnumShapeTest() { | ||
|
||
// Arrange | ||
Model model = | ||
loadSmithyModelFromResource("int-enum-shape-test"); | ||
MockManifest manifest = | ||
new MockManifest(); | ||
PluginContext context = | ||
buildMockPluginContext(model, manifest, "smithy.example#Example"); | ||
|
||
// Act | ||
(new GoCodegenPlugin()).execute(context); | ||
|
||
// Assert | ||
String actualEnumShapeCode = | ||
manifest.getFileString("types/enums.go").get(); | ||
String expectedEnumShapeCode = | ||
loadExpectedFileStringFromResource("int-enum-shape-test", "types/enums.go"); | ||
assertThat("intEnum shape actual generated code is equal to the expected generated code", | ||
actualEnumShapeCode, | ||
is(expectedEnumShapeCode)); | ||
String actualChangeCardOperationCode = | ||
manifest.getFileString("api_op_ChangeCard.go").get(); | ||
String expectedChangeCardInputCode = | ||
loadExpectedFileStringFromResource("int-enum-shape-test", "changeCardInput.go.struct"); | ||
assertThat("intEnum shape properly referenced in generated input structure code", | ||
actualChangeCardOperationCode, | ||
containsString(expectedChangeCardInputCode)); | ||
String expectedChangeCardOutputCode = | ||
loadExpectedFileStringFromResource("int-enum-shape-test", "changeCardOutput.go.struct"); | ||
assertThat("intEnum shape properly referenced in generated output structure code", | ||
actualChangeCardOperationCode, | ||
containsString(expectedChangeCardOutputCode)); | ||
|
||
} | ||
|
||
} |