Skip to content

Commit 798bfea

Browse files
Enable shapes-only codegen
This adds an alternative generation mode that only generates "data shapes".
1 parent 8297c56 commit 798bfea

26 files changed

+1799
-206
lines changed

README.md

Lines changed: 152 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ This repository does *not* contain any generated clients, such as for S3 or othe
2828
AWS services. Rather, these are the tools that facilitate the generation of those
2929
clients and non-AWS Smithy clients.
3030

31-
### How do I use this?
31+
### How do I use this to create a client?
3232

3333
The first step is to create a Smithy package. If this is your first time working
3434
with Smithy, follow [this quickstart guide](https://smithy.io/2.0/quickstart.html)
@@ -82,9 +82,9 @@ this file, see the
8282
"sources": ["model"],
8383
"maven": {
8484
"dependencies": [
85-
"software.amazon.smithy:smithy-model:[1.34.0,2.0)",
86-
"software.amazon.smithy:smithy-aws-traits:[1.34.0,2.0)",
87-
"software.amazon.smithy.python:smithy-python-codegen:0.1.0"
85+
"software.amazon.smithy:smithy-model:[1.54.0,2.0)",
86+
"software.amazon.smithy:smithy-aws-traits:[1.54.0,2.0)",
87+
"software.amazon.smithy.python.codegen:core:0.0.1"
8888
]
8989
},
9090
"projections": {
@@ -175,6 +175,154 @@ Only for now. Once the generator has been published, the Smithy CLI will be able
175175
to run it without a separate Java installation. Similarly, once the python
176176
helper libraries have been published you won't need to install them locally.
177177

178+
### How do I generate types for shapes without a client?
179+
180+
If all you want are concrete Python classes for the shapes in your Smithy model,
181+
all you need to do is replace `python-client-codegen` with
182+
`python-type-codegen` when following the steps above. Your `smithy-build.json`
183+
would now look like:
184+
185+
```json
186+
{
187+
"version": "1.0",
188+
"sources": ["model"],
189+
"maven": {
190+
"dependencies": [
191+
"software.amazon.smithy:smithy-model:[1.54.0,2.0)",
192+
"software.amazon.smithy.python.codegen.plugins:types:0.0.1"
193+
]
194+
},
195+
"projections": {
196+
"shapes": {
197+
"plugins": {
198+
"python-type-codegen": {
199+
"service": "com.example#EchoService",
200+
"module": "echo",
201+
"moduleVersion": "0.0.1"
202+
}
203+
}
204+
}
205+
}
206+
}
207+
```
208+
209+
The module with the generated shape classes can be found in
210+
`build/smithy/client/python-type-codegen` after you run `smithy-build`.
211+
212+
Unlike when generating a client, a service shape is not required for shape
213+
generation. If a service is not provided then every shape found in the model
214+
will be generated. Any naming conflicts may be resolved by using the
215+
[`renameShapes` transform](https://smithy.io/2.0/guides/smithy-build-json.html#renameshapes)
216+
(or renaming the shapes in the model of course).
217+
218+
The set of shapes generated can also be constrained by using the
219+
[`includeShapesBySelector` transform](https://smithy.io/2.0/guides/smithy-build-json.html#includeshapesbyselector).
220+
For example, to generate only shapes within the `com.example` namespace:
221+
222+
```json
223+
{
224+
"version": "1.0",
225+
"sources": ["model"],
226+
"maven": {
227+
"dependencies": [
228+
"software.amazon.smithy:smithy-model:[1.54.0,2.0)",
229+
"software.amazon.smithy.python.codegen.plugins:types:0.0.1"
230+
]
231+
},
232+
"projections": {
233+
"shapes": {
234+
"transforms": [
235+
{
236+
"name": "includeShapesBySelector",
237+
"args": {
238+
"selector": "[id|namespace = 'com.example']"
239+
}
240+
}
241+
],
242+
"plugins": {
243+
"python-type-codegen": {
244+
"module": "echo",
245+
"moduleVersion": "0.0.1"
246+
}
247+
}
248+
}
249+
}
250+
}
251+
```
252+
253+
Input and output shapes (shapes with the `@input` or `@output` traits and
254+
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):
258+
259+
```json
260+
{
261+
"version": "1.0",
262+
"sources": ["model"],
263+
"maven": {
264+
"dependencies": [
265+
"software.amazon.smithy:smithy-model:[1.54.0,2.0)",
266+
"software.amazon.smithy.python.codegen.plugins:types:0.0.1"
267+
]
268+
},
269+
"projections": {
270+
"shapes": {
271+
"transforms": [
272+
{
273+
"name": "excludeTraits",
274+
"args": {
275+
"traits": ["input", "output"]
276+
}
277+
}
278+
],
279+
"plugins": {
280+
"python-type-codegen": {
281+
"module": "echo",
282+
"moduleVersion": "0.0.1"
283+
}
284+
}
285+
}
286+
}
287+
}
288+
```
289+
290+
You can also generate both a client package and a shape package in one build,
291+
but they won't depend on each other. To do this, just add both plugins in the
292+
projection, or create a projection for each plugin. Below is an example showing
293+
both plugins in one projection:
294+
295+
```json
296+
{
297+
"version": "1.0",
298+
"sources": ["model"],
299+
"maven": {
300+
"dependencies": [
301+
"software.amazon.smithy:smithy-model:[1.54.0,2.0)",
302+
"software.amazon.smithy:smithy-aws-traits:[1.54.0,2.0)",
303+
"software.amazon.smithy.python.codegen:core:0.0.1",
304+
"software.amazon.smithy.python.codegen.plugins:types:0.0.1"
305+
]
306+
},
307+
"projections": {
308+
"client": {
309+
"plugins": {
310+
"python-client-codegen": {
311+
"service": "com.example#EchoService",
312+
"module": "echo",
313+
"moduleVersion": "0.0.1"
314+
},
315+
"python-type-codegen": {
316+
"service": "com.example#EchoService",
317+
"module": "echo",
318+
"moduleVersion": "0.0.1"
319+
}
320+
}
321+
}
322+
}
323+
}
324+
```
325+
178326
### Core Modules and Interfaces
179327

180328
* `smithy-core` provides transport-agnostic core modules and interfaces

codegen/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
allprojects {
1717
group = "software.amazon.smithy.python"
18-
version = "0.1.0"
18+
version = "0.0.1"
1919
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import org.gradle.api.Project
2+
3+
plugins {
4+
id("smithy-python.module-conventions")
5+
id("smithy-python.integ-test-conventions")
6+
}
7+
8+
// Workaround per: https://github.com/gradle/gradle/issues/15383
9+
val Project.libs get() = the<org.gradle.accessors.dm.LibrariesForLibs>()
10+
11+
group = "software.amazon.smithy.python.codegen.plugins"
12+
13+
dependencies {
14+
implementation(libs.smithy.codegen)
15+
implementation(project(":core"))
16+
17+
// Avoid circular dependency in codegen core
18+
if (project.name != "core") {
19+
api(project(":core"))
20+
}
21+
}
22+
23+
val generatedSrcDir = layout.buildDirectory.dir("generated-src").get()
24+
25+
// Add generated sources to integration test sources
26+
sourceSets {
27+
named("it") {
28+
java {
29+
srcDir(generatedSrcDir)
30+
}
31+
}
32+
}
33+
34+
// Ensure integ tests are executed as part of test suite
35+
tasks["test"].finalizedBy("integ")

0 commit comments

Comments
 (0)