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

Make string enum to enum shape transform opt-in in CodegenDirector #1370

Merged
merged 1 commit into from
Aug 29, 2022
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 @@ -97,7 +97,6 @@ public static Model simplifyModelForServiceCodegen(Model model, ShapeId service,
ServiceShape serviceShape = model.expectShape(service, ServiceShape.class);
model = transformer.copyServiceErrorsToOperations(model, serviceShape);
model = transformer.flattenAndRemoveMixins(model);
model = transformer.changeStringEnumsToEnumShapes(model, true);
return model;
}

Expand Down Expand Up @@ -238,6 +237,19 @@ public void createDedicatedInputsAndOutputs(String inputSuffix, String outputSuf
});
}

/**
* Changes each compatible string shape with the enum trait to an enum shape.
*
* @param synthesizeEnumNames Whether enums without names should have names synthesized if possible.
* @see ModelTransformer#changeStringEnumsToEnumShapes(Model, boolean)
*/
public void changeStringEnumsToEnumShapes(boolean synthesizeEnumNames) {
transforms.add((model, transformer) -> {
LOGGER.finest("Creating dedicated input and output shapes for directed codegen");
return transformer.changeStringEnumsToEnumShapes(model, synthesizeEnumNames);
});
}

/**
* Sorts all members of the model prior to codegen.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,6 +38,8 @@ public class CodegenDirectorTest {

private static final class TestDirected implements DirectedCodegen<TestContext, TestSettings, TestIntegration> {
public final List<ShapeId> generatedShapes = new ArrayList<>();
public final List<ShapeId> generatedEnumTypeEnums = new ArrayList<>();
public final List<ShapeId> generatedStringTypeEnums = new ArrayList<>();

@Override
public SymbolProvider createSymbolProvider(CreateSymbolProviderDirective<TestSettings> directive) {
Expand Down Expand Up @@ -86,10 +89,15 @@ public void generateUnion(GenerateUnionDirective<TestContext, TestSettings> dire
public void generateEnumShape(GenerateEnumDirective<TestContext, TestSettings> directive) {
generatedShapes.add(directive.shape().getId());
GenerateEnumDirective.EnumType type = directive.getEnumType();
if (type == GenerateEnumDirective.EnumType.ENUM) {
directive.expectEnumShape();
} else {
throw new RuntimeException("Expected enum type to be enum shape");
switch (type) {
case ENUM:
generatedEnumTypeEnums.add(directive.expectEnumShape().getId());
break;
case STRING:
generatedStringTypeEnums.add(directive.shape().asStringShape().get().getId());
break;
default:
throw new IllegalStateException("Only ENUM and STRING types exist.");
}
}

Expand Down Expand Up @@ -164,6 +172,51 @@ public void performsCodegen() {
ShapeId.from("smithy.example#Instruction"),
ShapeId.from("smithy.api#Unit")
));

assertThat(testDirected.generatedStringTypeEnums, containsInAnyOrder(
ShapeId.from("smithy.example#Status")
));
assertThat(testDirected.generatedEnumTypeEnums, empty());
}

@Test
public void performsCodegenWithStringEnumsChangedToEnumShapes() {
TestDirected testDirected = new TestDirected();
CodegenDirector<TestWriter, TestIntegration, TestContext, TestSettings> runner
= new CodegenDirector<>();
FileManifest manifest = new MockManifest();
Model model = Model.assembler()
.addImport(getClass().getResource("directed-model.smithy"))
.assemble()
.unwrap();

runner.settings(new TestSettings());
runner.directedCodegen(testDirected);
runner.fileManifest(manifest);
runner.service(ShapeId.from("smithy.example#Foo"));
runner.model(model);
runner.integrationClass(TestIntegration.class);
runner.performDefaultCodegenTransforms();
runner.changeStringEnumsToEnumShapes(true);
runner.createDedicatedInputsAndOutputs();
runner.sortMembers();
runner.run();

// asserts that mixin smithy.example#Paginated is not generated
assertThat(testDirected.generatedShapes, containsInAnyOrder(
ShapeId.from("smithy.example#Foo"),
ShapeId.from("smithy.example#TheFoo"),
ShapeId.from("smithy.example#ListFooInput"),
ShapeId.from("smithy.example#ListFooOutput"),
ShapeId.from("smithy.example#Status"),
ShapeId.from("smithy.example#Instruction"),
ShapeId.from("smithy.api#Unit")
));

assertThat(testDirected.generatedEnumTypeEnums, containsInAnyOrder(
ShapeId.from("smithy.example#Status")
));
assertThat(testDirected.generatedStringTypeEnums, empty());
}

@Test
Expand Down