|
| 1 | +# Smithy Trait CodeGen |
| 2 | + |
| 3 | +Smithy build plugin that generates Java trait classes from customized Smithy trait defined with [`@trait`](https://smithy.io/2.0/spec/model.html#trait-trait). |
| 4 | + |
| 5 | +## Supported Trait Types |
| 6 | +- [**Simple Types**](https://smithy.io/2.0/spec/simple-types.html) (Excluding `Blob` and `Boolean`) |
| 7 | +- **Structure** |
| 8 | +- **List** |
| 9 | +- **Set** |
| 10 | +- **Map** |
| 11 | + |
| 12 | +> [!NOTE] |
| 13 | +> [Annotation Trait](https://smithy.io/2.0/spec/model.html#annotation-traits) is recommended rather than Boolean trait |
| 14 | +
|
| 15 | +## Supported Smithy Prelude Traits |
| 16 | +- [@required](https://smithy.io/2.0/spec/type-refinement-traits.html#required-trait) |
| 17 | +- [@enumValue](https://smithy.io/2.0/spec/type-refinement-traits.html#enumvalue-trait) |
| 18 | +- [@mixin](https://smithy.io/2.0/spec/type-refinement-traits.html#mixin-trait) |
| 19 | +- [@idRef](https://smithy.io/2.0/spec/constraint-traits.html#idref-trait) |
| 20 | +- [@uniqueItems](https://smithy.io/2.0/spec/constraint-traits.html#uniqueitems-trait) |
| 21 | +- [@deprecatedTrait](https://smithy.io/2.0/spec/documentation-traits.html#deprecated-trait) |
| 22 | +- [@documentation](https://smithy.io/2.0/spec/documentation-traits.html#documentation-trait) |
| 23 | +- [@externalDocumentation](https://smithy.io/2.0/spec/documentation-traits.html#externaldocumentation-trait) |
| 24 | +- [@timestampFormat](https://smithy.io/2.0/spec/protocol-traits.html#timestampformat-trait) |
| 25 | + |
| 26 | +## Configuration |
| 27 | + |
| 28 | +The generator supports the following configuration options: |
| 29 | + |
| 30 | +| Option | Description | Required | |
| 31 | +|--------|-------------|----------| |
| 32 | +| `package` | Java package name for generated classes | Yes | |
| 33 | +| `namespace` | Smithy namespace to search for traits | Yes | |
| 34 | +| `header` | Header lines to include in generated files | No | |
| 35 | +| `excludeTags` | Smithy tags to exclude from generation | No | |
| 36 | + |
| 37 | +An example for `smithy-build.json`: |
| 38 | + |
| 39 | +```json |
| 40 | +{ |
| 41 | + "version": "1.0", |
| 42 | + "plugins": { |
| 43 | + "trait-codegen": { |
| 44 | + "package": "com.example.traits", |
| 45 | + "namespace": "com.example.traits", |
| 46 | + "header": ["Copyright Example Corp"], |
| 47 | + "excludeTags": ["internal"] |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +## Getting Started |
| 54 | + |
| 55 | +First, create a gradle-based Smithy model project. You can also use Smithy CLI to generate a [complete custom trait example](https://github.com/smithy-lang/smithy-examples/tree/main/custom-trait-examples/custom-trait): `smithy init -o custom-trait -t custom-trait`. |
| 56 | + |
| 57 | +> [!NOTE] |
| 58 | +> The generator currently cannot be run with non-gradle-based projects. |
| 59 | +
|
| 60 | +In `build.gradle.kts`, add `id("software.amazon.smithy.gradle.smithy-trait-package").version("1.3.0")` to the `plugin` section. |
| 61 | +```kotlin |
| 62 | +plugins { |
| 63 | + id("software.amazon.smithy.gradle.smithy-trait-package").version("1.3.0") |
| 64 | +} |
| 65 | + |
| 66 | +dependencies { |
| 67 | + smithyCli("software.amazon.smithy:smithy-cli:1.61.0") |
| 68 | + implementation("software.amazon.smithy:smithy-model:1.61.0") |
| 69 | +} |
| 70 | + |
| 71 | +repositories { |
| 72 | + mavenLocal() |
| 73 | + mavenCentral() |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +Next, add the `trait-codegen` plugin to the |
| 78 | +[plugin configuration](https://smithy.io/2.0/guides/smithy-build-json.html) in `smithy-build.json`. |
| 79 | + |
| 80 | +```json |
| 81 | +"plugins": { |
| 82 | + "trait-codegen": { |
| 83 | + "package": "io.examples.traits", |
| 84 | + "namespace": "example.traits", |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Finally, build the model with Gradle: `gradle build` and you can find the the generated Java classes under `/build/smithyprojections/trait-codegen/` |
| 90 | + |
| 91 | +The generated Java class for `annotationTrait` from `custom-trait` template would be: |
| 92 | +```java |
| 93 | +@SmithyGenerated |
| 94 | +public final class AnnotationTrait extends AbstractTrait implements ToSmithyBuilder<AnnotationTrait> { |
| 95 | + public static final ShapeId ID = ShapeId.from("example.traits#annotationTrait"); |
| 96 | + |
| 97 | + private AnnotationTrait(Builder builder) { |
| 98 | + super(ID, builder.getSourceLocation()); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected Node createNode() { |
| 103 | + return Node.objectNodeBuilder() |
| 104 | + .sourceLocation(getSourceLocation()) |
| 105 | + .build(); |
| 106 | + } |
| 107 | + |
| 108 | + public static AnnotationTrait fromNode(Node node) { |
| 109 | + Builder builder = builder().sourceLocation(node); |
| 110 | + node.expectObjectNode(); |
| 111 | + return builder.build(); |
| 112 | + } |
| 113 | + |
| 114 | + // Builder Implementation |
| 115 | + |
| 116 | + // Service Provider Implementation |
| 117 | +} |
| 118 | +``` |
| 119 | +The generated Java class for `JsonNameTrait` from `custom-trait` template would be: |
| 120 | +```java |
| 121 | +@SmithyGenerated |
| 122 | +public final class JsonNameTrait extends StringTrait { |
| 123 | + public static final ShapeId ID = ShapeId.from("example.traits#jsonName"); |
| 124 | + |
| 125 | + public JsonNameTrait(String value) { |
| 126 | + super(ID, value, SourceLocation.NONE); |
| 127 | + } |
| 128 | + |
| 129 | + public JsonNameTrait(String value, FromSourceLocation sourceLocation) { |
| 130 | + super(ID, value, sourceLocation); |
| 131 | + } |
| 132 | + |
| 133 | + // Builder Implementation |
| 134 | + |
| 135 | + // Service Provider Implementation |
| 136 | +} |
| 137 | +``` |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +Licensed under the Apache 2.0 License. |
0 commit comments