Skip to content

Commit b3242a9

Browse files
Add IO generation toggle to type codegen
1 parent 798bfea commit b3242a9

File tree

7 files changed

+216
-211
lines changed

7 files changed

+216
-211
lines changed

README.md

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,8 @@ For example, to generate only shapes within the `com.example` namespace:
252252

253253
Input and output shapes (shapes with the `@input` or `@output` traits and
254254
operation inputs / outputs created as part of an operation definition) are not
255-
generated by default. To generate these shapes anyway, remove the traits with
256-
the
257-
[`excludeTraits` transform](https://smithy.io/2.0/guides/smithy-build-json.html#excludetraits):
255+
generated by default. To generate these shapes anyway, set the
256+
`generateInputsAndOutputs` property to `true`.
258257

259258
```json
260259
{
@@ -268,18 +267,11 @@ the
268267
},
269268
"projections": {
270269
"shapes": {
271-
"transforms": [
272-
{
273-
"name": "excludeTraits",
274-
"args": {
275-
"traits": ["input", "output"]
276-
}
277-
}
278-
],
279270
"plugins": {
280271
"python-type-codegen": {
281272
"module": "echo",
282-
"moduleVersion": "0.0.1"
273+
"moduleVersion": "0.0.1",
274+
"generateInputsAndOutputs": true
283275
}
284276
}
285277
}

codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java

Lines changed: 180 additions & 179 deletions
Large diffs are not rendered by default.

codegen/core/src/main/java/software/amazon/smithy/python/codegen/DirectedPythonClientCodegen.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import software.amazon.smithy.python.codegen.generators.StructureGenerator;
4141
import software.amazon.smithy.python.codegen.generators.UnionGenerator;
4242
import software.amazon.smithy.python.codegen.integrations.PythonIntegration;
43-
import software.amazon.smithy.python.codegen.integrations.RuntimeClientPlugin;
4443
import software.amazon.smithy.python.codegen.writer.PythonDelegator;
4544
import software.amazon.smithy.utils.SmithyUnstableApi;
4645

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/InitGenerator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import java.nio.file.Path;
88
import java.nio.file.Paths;
99
import java.util.stream.Collectors;
10-
import software.amazon.smithy.codegen.core.directed.CustomizeDirective;
1110
import software.amazon.smithy.python.codegen.GenerationContext;
12-
import software.amazon.smithy.python.codegen.PythonSettings;
1311
import software.amazon.smithy.utils.SmithyInternalApi;
1412

1513
/**
@@ -42,9 +40,9 @@ public void run() {
4240
"# Code generated by smithy-python-codegen DO NOT EDIT.\n");
4341
}
4442
}
45-
context.writerDelegator().useFileWriter(
46-
"%s/__init__.py".formatted(context.settings().moduleName()),
47-
w -> w.write("__version__: str = '$L'", context.settings().moduleVersion())
48-
);
43+
context.writerDelegator()
44+
.useFileWriter(
45+
"%s/__init__.py".formatted(context.settings().moduleName()),
46+
w -> w.write("__version__: str = '$L'", context.settings().moduleVersion()));
4947
}
5048
}

codegen/core/src/main/java/software/amazon/smithy/python/codegen/integrations/PythonIntegration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ default List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) {
3939
return Collections.emptyList();
4040
}
4141

42-
4342
/**
4443
* Writes out all extra files required by runtime plugins.
4544
*/

codegen/plugins/types/src/main/java/software/amazon/smithy/python/codegen/types/PythonTypeCodegenPlugin.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void execute(PluginContext context) {
5454

5555
var model = context.getModel();
5656
if (typeSettings.service().isEmpty()) {
57-
model = addSyntheticService(model, typeSettings.selector().select(model));
57+
model = addSyntheticService(model, typeSettings.selector().select(model), typeSettings);
5858
}
5959

6060
runner.settings(pythonSettings);
@@ -67,7 +67,7 @@ public void execute(PluginContext context) {
6767
runner.run();
6868
}
6969

70-
private Model addSyntheticService(Model model, Collection<Shape> shapes) {
70+
private Model addSyntheticService(Model model, Collection<Shape> shapes, PythonTypeCodegenSettings settings) {
7171
StructureShape.Builder inputBuilder = StructureShape.builder()
7272
.id(SYNTHETIC_INPUT_ID)
7373
.addTrait(new InputTrait());
@@ -76,16 +76,22 @@ private Model addSyntheticService(Model model, Collection<Shape> shapes) {
7676
.id(SYNTHETIC_OPERATION_ID)
7777
.input(SYNTHETIC_INPUT_ID);
7878

79+
ServiceShape.Builder serviceBuilder = ServiceShape.builder()
80+
.id(SYNTHETIC_SERVICE_ID);
81+
7982
var index = 0;
8083
for (Shape shape : shapes) {
8184
if (!GENERATED_TYPES.contains(shape.getType())
82-
|| shape.hasTrait(InputTrait.class)
83-
|| shape.hasTrait(OutputTrait.class)
8485
|| shape.hasTrait(MixinTrait.class)
8586
|| Prelude.isPreludeShape(shape)) {
8687
continue;
8788
}
8889

90+
if (!settings.generateInputsAndOutputs()
91+
&& (shape.hasTrait(InputTrait.class) || shape.hasTrait(OutputTrait.class))) {
92+
continue;
93+
}
94+
8995
if (shape.hasTrait(ErrorTrait.class)) {
9096
operationBuilder.addError(shape.getId());
9197
} else {
@@ -94,11 +100,7 @@ private Model addSyntheticService(Model model, Collection<Shape> shapes) {
94100
}
95101
}
96102

97-
ServiceShape service = ServiceShape.builder()
98-
.id(SYNTHETIC_SERVICE_ID)
99-
.addOperation(SYNTHETIC_OPERATION_ID)
100-
.build();
101-
103+
var service = serviceBuilder.addOperation(SYNTHETIC_OPERATION_ID).build();
102104
ModelTransformer transformer = ModelTransformer.create();
103105
return transformer.replaceShapes(model, Set.of(inputBuilder.build(), operationBuilder.build(), service));
104106
}

codegen/plugins/types/src/main/java/software/amazon/smithy/python/codegen/types/PythonTypeCodegenSettings.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.Arrays;
88
import java.util.Optional;
9+
import software.amazon.smithy.model.node.BooleanNode;
910
import software.amazon.smithy.model.node.ObjectNode;
1011
import software.amazon.smithy.model.node.StringNode;
1112
import software.amazon.smithy.model.selector.Selector;
@@ -30,21 +31,24 @@ public record PythonTypeCodegenSettings(
3031
String moduleName,
3132
String moduleVersion,
3233
String moduleDescription,
33-
Selector selector) implements ToSmithyBuilder<PythonTypeCodegenSettings> {
34+
Selector selector,
35+
Boolean generateInputsAndOutputs) implements ToSmithyBuilder<PythonTypeCodegenSettings> {
3436

3537
private static final String SERVICE = "service";
3638
private static final String MODULE_NAME = "module";
3739
private static final String MODULE_DESCRIPTION = "moduleDescription";
3840
private static final String MODULE_VERSION = "moduleVersion";
3941
private static final String SELECTOR = "selector";
42+
private static final String GENERATE_INPUTS_AND_OUTPUTS = "generateInputsAndOutputs";
4043

4144
private PythonTypeCodegenSettings(Builder builder) {
4245
this(
4346
Optional.ofNullable(builder.service),
4447
builder.moduleName,
4548
builder.moduleVersion,
4649
builder.moduleDescription,
47-
builder.selector);
50+
builder.selector,
51+
builder.generateInputsAndOutputs);
4852
}
4953

5054
@Override
@@ -53,7 +57,8 @@ public Builder toBuilder() {
5357
.moduleName(moduleName)
5458
.moduleVersion(moduleVersion)
5559
.moduleDescription(moduleDescription)
56-
.selector(selector);
60+
.selector(selector)
61+
.generateInputsAndOutputs(generateInputsAndOutputs);
5762
service.ifPresent(builder::service);
5863
return builder;
5964
}
@@ -88,6 +93,9 @@ public static PythonTypeCodegenSettings fromNode(ObjectNode config) {
8893
config.getStringMember(SERVICE).map(StringNode::expectShapeId).ifPresent(builder::service);
8994
config.getStringMember(MODULE_DESCRIPTION).map(StringNode::getValue).ifPresent(builder::moduleDescription);
9095
config.getStringMember(SELECTOR).map(node -> Selector.parse(node.getValue())).ifPresent(builder::selector);
96+
config.getBooleanMember(GENERATE_INPUTS_AND_OUTPUTS)
97+
.map(BooleanNode::getValue)
98+
.ifPresent(builder::generateInputsAndOutputs);
9199
return builder.build();
92100
}
93101

@@ -102,6 +110,7 @@ public static class Builder implements SmithyBuilder<PythonTypeCodegenSettings>
102110
private String moduleVersion;
103111
private String moduleDescription;
104112
private Selector selector = Selector.IDENTITY;
113+
private Boolean generateInputsAndOutputs = false;
105114

106115
@Override
107116
public PythonTypeCodegenSettings build() {
@@ -134,5 +143,10 @@ public Builder selector(Selector selector) {
134143
this.selector = selector == null ? Selector.IDENTITY : selector;
135144
return this;
136145
}
146+
147+
public Builder generateInputsAndOutputs(boolean generateInputsAndOutputs) {
148+
this.generateInputsAndOutputs = generateInputsAndOutputs;
149+
return this;
150+
}
137151
}
138152
}

0 commit comments

Comments
 (0)