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

Add maps and lists to directed codegen #2273

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 @@ -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
Loading