-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support and mechanism for enum codegen
We will be added proper enum shapes in Smithy IDL 2.0, and we want code generators to explicitly pay attention to doing this codegen step. Adding a new required directive method later would be a breaking change unless se give it a default implementation, but a default implementation won't force implementers to notice enums need codegen. The solution in this commit is to support both string shape and enum shape codegen from the same directive. Generators need to ask the directive what kind of shape is being generated to know how to perform enum codegen. Generators that perform this transformation in advance can just assume they're only given enum shapes during code generation. Add Directive suffix to directive classes This helps to make their relationship and intent more clear, in particular classes like "Customize" that seemed overly ambiguous.
- Loading branch information
Showing
17 changed files
with
223 additions
and
73 deletions.
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
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
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
88 changes: 88 additions & 0 deletions
88
...ore/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateEnumDirective.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,88 @@ | ||
/* | ||
* 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.codegen.core.directed; | ||
|
||
import software.amazon.smithy.codegen.core.CodegenContext; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.traits.EnumTrait; | ||
|
||
/** | ||
* Directive used to generate an enum shape or enum string shape. | ||
* | ||
* @param <C> CodegenContext type. | ||
* @param <S> Codegen settings type. | ||
* @see DirectedCodegen#generateUnion | ||
*/ | ||
public final class GenerateEnumDirective<C extends CodegenContext<S, ?>, S> extends ShapeDirective<Shape, C, S> { | ||
|
||
GenerateEnumDirective(C context, ServiceShape service, Shape shape) { | ||
super(context, service, validateShape(shape)); | ||
} | ||
|
||
private static Shape validateShape(Shape shape) { | ||
if (!shape.isStringShape() || !shape.hasTrait(EnumTrait.class)) { | ||
throw new IllegalArgumentException("GenerateEnum requires a string shape with the enum trait"); | ||
} | ||
|
||
return shape; | ||
} | ||
|
||
/** | ||
* Represents the type of enum to generate. Smithy IDL 2.0 introduces actual | ||
* enum shapes. | ||
*/ | ||
public enum EnumType { | ||
/** A string shape marked with the enum trait is being generated. */ | ||
STRING, | ||
|
||
// TODO: idl-2.0 | ||
// ENUM | ||
} | ||
|
||
/** | ||
* Gets the type of enum being generated, whether it's a string shape marked with | ||
* the enum trait or, in Smithy IDL 2.0, an actual enum shape. | ||
* | ||
* <p>Note that Smithy IDL 2.0 generators can perform a pre-processing transform | ||
* to convert eligible string shape enums to proper enums, removing the need to | ||
* check this property. | ||
* | ||
* @return Gets the type of enum being generated. | ||
* @see #getEnumTrait() | ||
*/ | ||
public EnumType getEnumType() { | ||
// TODO: update when idl-2.0 is released. | ||
return EnumType.STRING; | ||
} | ||
|
||
/** | ||
* Gets the {@link EnumTrait} of the shape. | ||
* | ||
* @return Returns the enum trait. | ||
*/ | ||
public EnumTrait getEnumTrait() { | ||
return shape().expectTrait(EnumTrait.class); | ||
} | ||
|
||
/* | ||
TODO: Uncomment after IDL-2.0 is shipped. | ||
public EnumShape expectEnumShape() { | ||
return shape().asEnumShape().orElseThrow(() -> new ExpectationNotMetException( | ||
"Expected an enum shape, but found " + shape(), shape())); | ||
} | ||
*/ | ||
} |
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
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
Oops, something went wrong.