-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Initial POJO Schema generation (#24)
Add initial implementation of Schema generation.
- Loading branch information
Showing
10 changed files
with
362 additions
and
6 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
61 changes: 61 additions & 0 deletions
61
codegen/src/main/java/software/amazon/smithy/java/codegen/SchemaUtils.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,61 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.java.codegen; | ||
|
||
import java.util.Locale; | ||
import software.amazon.smithy.java.codegen.writer.JavaWriter; | ||
import software.amazon.smithy.java.runtime.core.schema.PreludeSchemas; | ||
import software.amazon.smithy.model.loader.Prelude; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
import software.amazon.smithy.model.shapes.ToShapeId; | ||
import software.amazon.smithy.utils.CaseUtils; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Provides various utility functions around SDK schemas | ||
*/ | ||
@SmithyInternalApi | ||
public final class SchemaUtils { | ||
|
||
/** | ||
* Determines the name to use for the Schema constant for a member. | ||
* | ||
* @param memberName Member shape to generate schema name from | ||
* @return name to use for static schema property | ||
*/ | ||
public static String toMemberSchemaName(String memberName) { | ||
return "SCHEMA_" + CaseUtils.toSnakeCase(memberName).toUpperCase(Locale.ENGLISH); | ||
} | ||
|
||
/** | ||
* Determines the name to use for shape Schemas. | ||
* | ||
* @param toShapeId shape to generate name for | ||
* @return name to use for static schema property | ||
*/ | ||
public static String toSchemaName(ToShapeId toShapeId) { | ||
return CaseUtils.toSnakeCase(toShapeId.toShapeId().getName()).toUpperCase(Locale.ENGLISH); | ||
} | ||
|
||
/** | ||
* Writes the schema property to use for a given shape. | ||
* | ||
* <p>If a shape is a prelude shape then it will use a property from {@link PreludeSchemas}. | ||
* Otherwise, the shape will use the generated {@code SharedSchemas} utility class. | ||
* | ||
* @param writer Writer to use for writing the Schema type. | ||
* @param shape shape to write Schema type for. | ||
*/ | ||
public static void writeSchemaType(JavaWriter writer, Shape shape) { | ||
if (Prelude.isPreludeShape(shape)) { | ||
writer.write("$T.$L", PreludeSchemas.class, shape.getType().name()); | ||
} else { | ||
writer.write("SharedSchemas.$L", toSchemaName(shape)); | ||
} | ||
} | ||
|
||
private SchemaUtils() {} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
.../src/main/java/software/amazon/smithy/java/codegen/generators/SharedSchemasGenerator.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,79 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.java.codegen.generators; | ||
|
||
import java.util.EnumSet; | ||
import java.util.function.Consumer; | ||
import software.amazon.smithy.codegen.core.directed.CustomizeDirective; | ||
import software.amazon.smithy.java.codegen.CodeGenerationContext; | ||
import software.amazon.smithy.java.codegen.JavaCodegenSettings; | ||
import software.amazon.smithy.java.codegen.writer.JavaWriter; | ||
import software.amazon.smithy.model.loader.Prelude; | ||
import software.amazon.smithy.model.shapes.ShapeType; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Generates a {@code SharedSchemas} utility class that contains all unattached schemas for the model. | ||
*/ | ||
@SmithyInternalApi | ||
public final class SharedSchemasGenerator | ||
implements Consumer<CustomizeDirective<CodeGenerationContext, JavaCodegenSettings>> { | ||
|
||
// Types that generate their own schemas | ||
private static final EnumSet<ShapeType> EXCLUDED_TYPES = EnumSet.of( | ||
ShapeType.RESOURCE, | ||
ShapeType.SERVICE, | ||
ShapeType.UNION, | ||
ShapeType.ENUM, | ||
ShapeType.INT_ENUM, | ||
ShapeType.STRUCTURE, | ||
ShapeType.MEMBER, | ||
ShapeType.OPERATION | ||
); | ||
|
||
@Override | ||
public void accept(CustomizeDirective<CodeGenerationContext, JavaCodegenSettings> directive) { | ||
directive.context() | ||
.writerDelegator() | ||
.useFileWriter( | ||
getFilename(directive.settings()), | ||
directive.settings().packageNamespace() + ".model", | ||
writer -> { | ||
writer.write( | ||
""" | ||
/** | ||
* Defines shared shapes across the model package that are not part of another code-generated type. | ||
*/ | ||
final class SharedSchemas { | ||
${C|} | ||
private SharedSchemas() {} | ||
} | ||
""", | ||
writer.consumer(w -> this.generatedSchemas(w, directive)) | ||
); | ||
} | ||
); | ||
} | ||
|
||
private void generatedSchemas( | ||
JavaWriter writer, | ||
CustomizeDirective<CodeGenerationContext, JavaCodegenSettings> directive | ||
) { | ||
// Loop through service closure and find all shapes that will not generate their own schemas | ||
directive.connectedShapes() | ||
.values() | ||
.stream() | ||
.filter(s -> !EXCLUDED_TYPES.contains(s.getType())) | ||
.filter(s -> !Prelude.isPreludeShape(s)) | ||
.forEach(s -> new SchemaGenerator(writer, s, directive.symbolProvider(), directive.model()).run()); | ||
} | ||
|
||
private static String getFilename(JavaCodegenSettings settings) { | ||
return String.format("./%s/model/SharedSchemas.java", settings.packageNamespace().replace(".", "/")); | ||
} | ||
} |
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.