From f05439ae26d41d63762cda62c795655f45b1de7b Mon Sep 17 00:00:00 2001 From: JordonPhillips Date: Fri, 3 May 2024 19:00:32 +0200 Subject: [PATCH] Add maps and lists to directed codegen This adds lists and maps to directed codegen. This is needed because when generating Python code there needs to be some data generated for lists and maps that must be placed in topological ordering as much as is possible. --- .../core/directed/CodegenDirector.java | 16 +++++++++++++ .../core/directed/DirectedCodegen.java | 14 +++++++++++ .../core/directed/GenerateListDirective.java | 24 +++++++++++++++++++ .../core/directed/GenerateMapDirective.java | 24 +++++++++++++++++++ .../core/directed/CodegenDirectorTest.java | 16 +++++++++++++ .../core/directed/directed-model.smithy | 16 ++++++++++--- 6 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateListDirective.java create mode 100644 smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateMapDirective.java diff --git a/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/CodegenDirector.java b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/CodegenDirector.java index 81e024138d5..3b36479138d 100644 --- a/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/CodegenDirector.java +++ b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/CodegenDirector.java @@ -39,6 +39,8 @@ import software.amazon.smithy.model.node.ObjectNode; import software.amazon.smithy.model.shapes.EnumShape; import software.amazon.smithy.model.shapes.IntEnumShape; +import software.amazon.smithy.model.shapes.ListShape; +import software.amazon.smithy.model.shapes.MapShape; import software.amazon.smithy.model.shapes.OperationShape; import software.amazon.smithy.model.shapes.ResourceShape; import software.amazon.smithy.model.shapes.ServiceShape; @@ -556,6 +558,20 @@ public Void unionShape(UnionShape shape) { return null; } + @Override + public Void listShape(ListShape shape) { + LOGGER.finest(() -> "Generating list " + shape.getId()); + directedCodegen.generateList(new GenerateListDirective<>(context, serviceShape, shape)); + return null; + } + + @Override + public Void mapShape(MapShape shape) { + LOGGER.finest(() -> "Generating map " + shape.getId()); + directedCodegen.generateMap(new GenerateMapDirective<>(context, serviceShape, shape)); + return null; + } + @Override public Void stringShape(StringShape shape) { if (shape.hasTrait(EnumTrait.class)) { diff --git a/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/DirectedCodegen.java b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/DirectedCodegen.java index e321473e71f..469c81bfa56 100644 --- a/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/DirectedCodegen.java +++ b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/DirectedCodegen.java @@ -99,6 +99,20 @@ default void generateOperation(GenerateOperationDirective directive) { */ void generateUnion(GenerateUnionDirective directive); + /** + * Generates any code needed for a list shape. + * + * @param directive Directive to perform. + */ + default void generateList(GenerateListDirective directive) {} + + /** + * Generates any code needed for a map shape. + * + * @param directive Directive to perform. + */ + default void generateMap(GenerateMapDirective directive) {} + /** * Generates the code needed for an enum shape, whether it's a string shape * marked with the enum trait, or a proper enum shape introduced in Smithy diff --git a/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateListDirective.java b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateListDirective.java new file mode 100644 index 00000000000..e3bbc76b469 --- /dev/null +++ b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateListDirective.java @@ -0,0 +1,24 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.codegen.core.directed; + +import software.amazon.smithy.codegen.core.CodegenContext; +import software.amazon.smithy.model.shapes.ListShape; +import software.amazon.smithy.model.shapes.ServiceShape; + +/** + * Directive used to generate a list. + * + * @param CodegenContext type. + * @param Codegen settings type. + * @see DirectedCodegen#generateList + */ +public class GenerateListDirective, S> + extends ShapeDirective { + GenerateListDirective(C context, ServiceShape service, ListShape shape) { + super(context, service, shape); + } +} diff --git a/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateMapDirective.java b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateMapDirective.java new file mode 100644 index 00000000000..1c2f16abe43 --- /dev/null +++ b/smithy-codegen-core/src/main/java/software/amazon/smithy/codegen/core/directed/GenerateMapDirective.java @@ -0,0 +1,24 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package software.amazon.smithy.codegen.core.directed; + +import software.amazon.smithy.codegen.core.CodegenContext; +import software.amazon.smithy.model.shapes.MapShape; +import software.amazon.smithy.model.shapes.ServiceShape; + +/** + * Directive used to generate a map. + * + * @param CodegenContext type. + * @param Codegen settings type. + * @see DirectedCodegen#generateMap + */ +public class GenerateMapDirective, S> + extends ShapeDirective { + GenerateMapDirective(C context, ServiceShape service, MapShape shape) { + super(context, service, shape); + } +} diff --git a/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/directed/CodegenDirectorTest.java b/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/directed/CodegenDirectorTest.java index df320005b18..f4a8bbc12a8 100644 --- a/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/directed/CodegenDirectorTest.java +++ b/smithy-codegen-core/src/test/java/software/amazon/smithy/codegen/core/directed/CodegenDirectorTest.java @@ -99,6 +99,16 @@ public void generateUnion(GenerateUnionDirective dire generatedShapes.add(directive.shape().getId()); } + @Override + public void generateList(GenerateListDirective directive) { + generatedShapes.add(directive.shape().getId()); + } + + @Override + public void generateMap(GenerateMapDirective directive) { + generatedShapes.add(directive.shape().getId()); + } + @Override public void generateEnumShape(GenerateEnumDirective directive) { generatedShapes.add(directive.shape().getId()); @@ -187,6 +197,9 @@ public void performsCodegen() { ShapeId.from("smithy.example#TheFoo"), ShapeId.from("smithy.example#ListFooInput"), ShapeId.from("smithy.example#ListFooOutput"), + ShapeId.from("smithy.example#FooStructure"), + ShapeId.from("smithy.example#FooList"), + ShapeId.from("smithy.example#StringMap"), ShapeId.from("smithy.example#Status"), ShapeId.from("smithy.example#FaceCard"), ShapeId.from("smithy.example#Instruction"), @@ -229,6 +242,9 @@ public void performsCodegenWithStringEnumsChangedToEnumShapes() { ShapeId.from("smithy.example#TheFoo"), ShapeId.from("smithy.example#ListFooInput"), ShapeId.from("smithy.example#ListFooOutput"), + ShapeId.from("smithy.example#FooStructure"), + ShapeId.from("smithy.example#FooList"), + ShapeId.from("smithy.example#StringMap"), ShapeId.from("smithy.example#Status"), ShapeId.from("smithy.example#FaceCard"), ShapeId.from("smithy.example#Instruction"), diff --git a/smithy-codegen-core/src/test/resources/software/amazon/smithy/codegen/core/directed/directed-model.smithy b/smithy-codegen-core/src/test/resources/software/amazon/smithy/codegen/core/directed/directed-model.smithy index 48d6c51fc39..686af4988c0 100644 --- a/smithy-codegen-core/src/test/resources/software/amazon/smithy/codegen/core/directed/directed-model.smithy +++ b/smithy-codegen-core/src/test/resources/software/amazon/smithy/codegen/core/directed/directed-model.smithy @@ -21,14 +21,24 @@ operation ListFoo { } output:= with [Paginated] { status: Status - items: StringList + items: FooList instruction: Instruction facecard: FaceCard } } -list StringList { - member: String +structure FooStructure { + id: String + tags: StringMap +} + +map StringMap { + key: String + value: String +} + +list FooList { + member: FooStructure } @enum([