1
1
# 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.
3
4
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;
5
6
shapes provide the structure and layout of an API, while traits provide refinement and style.
6
7
7
-
8
8
### Custom trait packages
9
9
A custom trait package can be created to distribute your trait definition for use in both Smithy models and in code generators.
10
10
@@ -26,102 +26,55 @@ a nested `Provider` class that implements `software.amazon.smithy.model.traits.T
26
26
then be added to a resource file ` META-INF/services/software.amazon.smithy.model.traits.TraitService ` in order for the SPI to
27
27
discover the custom trait implementation. See the examples in this directory for example implementations.
28
28
29
-
30
29
For more information on defining traits see the [ Smithy specification] ( https://smithy.io/2.0/spec/model.html#defining-traits )
31
30
32
31
33
32
## 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 )
37
36
38
37
---
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.
66
41
67
42
### Use as a template
68
43
To use this example as a template run the following command.
69
44
70
45
``` console
71
- smithy init -t custom-annotation- trait
46
+ smithy init -t custom-trait
72
47
```
73
48
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.
89
52
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.
92
58
93
59
### Use as a template
94
60
To use this example as a template run the following command.
95
61
96
62
``` console
97
- smithy init -t custom-string- trait
63
+ smithy init -t custom-trait-with-java-validator
98
64
```
99
65
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.
118
71
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 .
121
74
122
75
### Use as a template
123
76
To use this example as a template run the following command.
124
77
125
78
``` console
126
- smithy init -t custom-structure- trait
79
+ smithy init -t custom-trait-handwritten
127
80
```
0 commit comments