Skip to content

Commit fdfface

Browse files
authored
Update trait examples to use smithy-trait-package plugin (#103)
Update trait examples to use `smithy-trait-package` gradle plugin.
1 parent fd7d1b4 commit fdfface

File tree

69 files changed

+705
-803
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+705
-803
lines changed

custom-trait-examples/README.md

+27-74
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Custom Traits
2-
The examples in this directory demonstrate how to create a custom trait package for Smithy.
2+
The examples in this directory demonstrate how to create your own [traits](https://smithy.io/2.0/spec/model.html#traits)
3+
in Smithy.
34

4-
Traits are model components that can be attached to shapes to describe additional information about the shape;
5+
Traits are model components that can be attached to shapes to describe additional information about the shape;
56
shapes provide the structure and layout of an API, while traits provide refinement and style.
67

7-
88
### Custom trait packages
99
A custom trait package can be created to distribute your trait definition for use in both Smithy models and in code generators.
1010

@@ -26,102 +26,55 @@ a nested `Provider` class that implements `software.amazon.smithy.model.traits.T
2626
then be added to a resource file `META-INF/services/software.amazon.smithy.model.traits.TraitService` in order for the SPI to
2727
discover the custom trait implementation. See the examples in this directory for example implementations.
2828

29-
3029
For more information on defining traits see the [Smithy specification](https://smithy.io/2.0/spec/model.html#defining-traits)
3130

3231

3332
## Examples
34-
- [Custom Annotation Trait](#custom-annotation-trait)
35-
- [Custom String Trait](#custom-string-trait)
36-
- [Custom Structure Trait](#custom-structure-trait)
33+
- [Custom trait](#custom-trait)
34+
- [Custom trait with Java validator](#custom-trait-with-java-validator)
35+
- [Custom, handwritten trait](#custom-handwritten-trait)
3736

3837
---
39-
## Custom Annotation Trait
40-
This example demonstrates how to create a custom [Annotation trait](https://smithy.io/2.0/spec/model.html?highlight=annotation#annotation-traits)
41-
package. Annotation traits are structure traits with no members.
42-
43-
For example, you might use an annotation trait `smithy.example#beta` to mark an operation as still in `beta`:
44-
```smithy
45-
use smithy.example#beta
46-
47-
@beta
48-
operation MyOperation {
49-
input: MyOperationInput
50-
output: MyOperationOutput
51-
}
52-
53-
```
54-
55-
Annotation traits can be defined as Smithy shapes without code. For example:
56-
```smithy
57-
$version: "2"
58-
namespace smithy.example
59-
60-
@trait
61-
structure foo {}
62-
```
63-
64-
However, sometimes defining the trait as Java class in addition to a smithy model makes it easier for the trait to be used by
65-
other Java code such as code generators.
38+
## Custom Trait
39+
This example demonstrates how to create a package for custom traits using the `smithy-trait-package` Gradle plugin
40+
and Smithy's `trait-codegen` build plugin.
6641

6742
### Use as a template
6843
To use this example as a template run the following command.
6944

7045
```console
71-
smithy init -t custom-annotation-trait
46+
smithy init -t custom-trait
7247
```
7348

74-
## Custom String Trait
75-
This example demonstrates how to create a custom String trait package. String traits are string shapes with the `@trait`
76-
trait applied, and they accept a single string argument.
77-
78-
For example, you might use a string trait `smithy.example#label` to add a label to a structure:
79-
```smithy
80-
use smithy.example#label
81-
82-
@label("myLabel")
83-
structure MyStructure {
84-
value: String
85-
}
86-
```
87-
Many custom traits require additional validations to ensure they are used correctly. This example also adds a validator
88-
for the string trait that performs additional checks on the trait's usage.
49+
## Custom trait with Java validator
50+
This example demonstrates how to create a package for custom traits that includes a custom Java validator. This example
51+
uses the `smithy-trait-package` Gradle plugin and Smithy's `trait-codegen` build plugin to configure the package and generate traits.
8952

90-
*Note*: the validator defined in this package will be used by packages that declare this custom trait package as a
91-
dependency without being added to the `metadata validators` metadata key.
53+
Many custom traits require additional validations to ensure they are used correctly. Customers can use
54+
[trait validators](https://smithy.io/2.0/spec/model-validation.html#smithy-api-traitvalidators-trait)
55+
to apply most validation. However, sometimes advanced validation that cannot be easily expressed with Smithy selectors
56+
is required. [Custom Java validators](https://smithy.io/2.0/guides/model-linters.html#writing-custom-validators) can
57+
be used apply advanced model validation using the Java programming language.
9258

9359
### Use as a template
9460
To use this example as a template run the following command.
9561

9662
```console
97-
smithy init -t custom-string-trait
63+
smithy init -t custom-trait-with-java-validator
9864
```
9965

100-
## Custom Structure Trait
101-
This example demonstrates how to create a custom complex trait package. This complex trait has multiple inputs.
102-
103-
Complex structure traits can be useful for adding complex metadata to a shape for applications
104-
such as code generation.
105-
106-
For example, the [`smithy.api#http`](https://smithy.io/2.0/spec/http-bindings.html#smithy-api-http-trait) trait defines multiple
107-
inputs that are used by client and server code generators for generating http route handlers.
108-
109-
```smithy
110-
@idempotent
111-
@http(method: "PUT", uri: "/{bucketName}/{key}", code: 200)
112-
operation PutObject {
113-
input: PutObjectInput
114-
}
115-
```
116-
Many custom traits require these additional validations to ensure they are used correctly. This example also adds a validator
117-
for the custom trait that performs additional checks on the usage of the trait.
66+
## Custom, handwritten trait
67+
Trait codegen can be used in most cases when defining a custom trait, but some customers may wish to customize the Java
68+
definitions of their traits to support additional behavior such as parsing a string into a URL. To support such use cases
69+
customer can opt to handwrite their trait definitions. Such handwritten trait definitions can be included in a package alongside
70+
automatically generated Java trait definitions.
11871

119-
*Note*: the validator defined in this package will be used by packages that declare this custom trait package as a
120-
dependency without being added to the `metadata validators` metadata key.
72+
This package demonstrates how to include a handwritten Java trait alongside traits generated by the `trait-codegen`
73+
plugin.
12174

12275
### Use as a template
12376
To use this example as a template run the following command.
12477

12578
```console
126-
smithy init -t custom-structure-trait
79+
smithy init -t custom-trait-handwritten
12780
```

custom-trait-examples/custom-annotation-trait/README.md

-44
This file was deleted.

custom-trait-examples/custom-annotation-trait/build.gradle.kts

-65
This file was deleted.

custom-trait-examples/custom-annotation-trait/model/custom-trait.smithy

-6
This file was deleted.

custom-trait-examples/custom-annotation-trait/smithy-build.json

-3
This file was deleted.

custom-trait-examples/custom-annotation-trait/src/main/java/io/smithy/examples/traits/SpecialTrait.java

-30
This file was deleted.

custom-trait-examples/custom-annotation-trait/src/main/resources/META-INF/services/software.amazon.smithy.model.traits.TraitService

-1
This file was deleted.

custom-trait-examples/custom-string-trait/README.md

-49
This file was deleted.

0 commit comments

Comments
 (0)