Skip to content

Commit

Permalink
Add maps and lists to directed codegen
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JordonPhillips committed May 6, 2024
1 parent a6f1d1c commit 0eaafee
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ default void generateOperation(GenerateOperationDirective<C, S> directive) {
*/
void generateUnion(GenerateUnionDirective<C, S> directive);

/**
* Generates any code needed for a list shape.
*
* @param directive Directive to perform.
*/
default void generateList(GenerateListDirective<C, S> directive) {}

/**
* Generates any code needed for a map shape.
*
* @param directive Directive to perform.
*/
default void generateMap(GenerateMapDirective<C, S> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <C> CodegenContext type.
* @param <S> Codegen settings type.
* @see DirectedCodegen#generateList
*/
public class GenerateListDirective<C extends CodegenContext<S, ?, ?>, S>
extends ShapeDirective<ListShape, C, S> {
GenerateListDirective(C context, ServiceShape service, ListShape shape) {
super(context, service, shape);
}
}
Original file line number Diff line number Diff line change
@@ -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 <C> CodegenContext type.
* @param <S> Codegen settings type.
* @see DirectedCodegen#generateMap
*/
public class GenerateMapDirective<C extends CodegenContext<S, ?, ?>, S>
extends ShapeDirective<MapShape, C, S> {
GenerateMapDirective(C context, ServiceShape service, MapShape shape) {
super(context, service, shape);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public void generateUnion(GenerateUnionDirective<TestContext, TestSettings> dire
generatedShapes.add(directive.shape().getId());
}

@Override
public void generateList(GenerateListDirective<TestContext, TestSettings> directive) {
generatedShapes.add(directive.shape().getId());
}

@Override
public void generateMap(GenerateMapDirective<TestContext, TestSettings> directive) {
generatedShapes.add(directive.shape().getId());
}

@Override
public void generateEnumShape(GenerateEnumDirective<TestContext, TestSettings> directive) {
generatedShapes.add(directive.shape().getId());
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down

0 comments on commit 0eaafee

Please sign in to comment.