-
Notifications
You must be signed in to change notification settings - Fork 251
Add trait to declare types for metadata #3078
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JordonPhillips
wants to merge
2
commits into
main
Choose a base branch
from
typed-metadata
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
.changes/next-release/feature-5c018acaef9147e7499e70c6d8919d6cf0b496c5.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "type": "feature", | ||
| "description": "Added a new `metadata` trait that allows model authors to declare types for metadata keys that will be automatically validated when building models.", | ||
| "pull_requests": [] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| # Typed Metadata | ||
|
|
||
| Metadata is a schema-less extensibility mechanism used to associate metadata to | ||
| an entire model. For example, metadata is used to define validators and | ||
| model-wide suppressions. | ||
|
|
||
| This document describes a way to define typing information for metadata that is | ||
| automatically validated by Smithy. | ||
|
|
||
| ## Motivation | ||
|
|
||
| The schema-less nature of metadata has allowed it to be used for any purpose | ||
| without much hassle. However, that has come at the cost of increased validation | ||
| complexity. Any tool, including Smithy itself, that uses metadata in a | ||
| structured way has to perform validation itself. By allowing opt-in validation, | ||
| we can centralize and deduplicate that effort. | ||
|
|
||
| ## Proposal | ||
|
|
||
| Model authors may globally declare the type of a metadata key by targeting a | ||
| shape with the `@metadata` trait. | ||
|
|
||
| ```smithy | ||
| /// Defines a type for a metadata key. | ||
| /// | ||
| /// If a matching key is defined in the model, its value will be validated | ||
| /// according to the targeted shape. | ||
| /// | ||
| /// The type for any metadata key MUST only be defined once. | ||
| @trait(selector: "dataType :not([trait|input]) :not([trait|output])") | ||
| structure metadata { | ||
| /// The metadata key to validate. Each key MUST only be defined once. | ||
| @required | ||
| @length(min: 1) | ||
| key: String | ||
| } | ||
| ``` | ||
|
|
||
| For example, the | ||
| [suppressions metadata](https://smithy.io/2.0/spec/model-validation.html#suppressions-metadata) | ||
| could be defined as: | ||
|
|
||
| ```smithy | ||
| $version: "2.0" | ||
|
|
||
| namespace smithy.api | ||
|
|
||
| @metadata(key: "suppressions") | ||
| list MetadataSuppressions { | ||
| member: MetadataSuppression | ||
| } | ||
|
|
||
| structure MetadataSuppression { | ||
| @required | ||
| id: String | ||
|
|
||
| @required | ||
| @pattern("^(\*|[_a-zA-Z]\w*(\.[_a-zA-Z]\w*)*)$") | ||
| namespace: String | ||
|
|
||
| reason: String | ||
| } | ||
| ``` | ||
|
|
||
| ### Validation | ||
|
|
||
| A metadata key MUST NOT be defined more than once with the `@metadata` trait. If | ||
| a metadata key is defined more than once with the `@metadata` trait, an | ||
| `ERROR`-level validation event will be emitted. | ||
|
|
||
| Validation of metadata values will behave no differently than validation | ||
| anywhere else, with severity levels being determined by each individual | ||
| validator. | ||
|
|
||
| Shapes targeted by the metadata shape and shapes transitively referenced by | ||
| those shapes will not trigger the unreferenced shapes validator. | ||
|
|
||
| Adding or removing this trait is considered backwards-compatible because | ||
| metadata does not inherently change any interface. It may cause a build to break | ||
| if the new validation rejects existing metadata, but that is intended behavior. | ||
|
|
||
| ## Alternatives | ||
|
|
||
| ### Inline definitions | ||
|
|
||
| The metadata trait globally defines the shape of a metadata key, but we could | ||
| allow local, inline definitions additionally or instead. This can be achieved by | ||
| using a special `$type` key in metadata's node value. | ||
|
|
||
| ```smithy | ||
| $version: "2.0" | ||
|
|
||
| metadata foo = { | ||
| "$type": "com.example#Foo" | ||
| "bar": "baz" | ||
| } | ||
|
|
||
| namespace com.example | ||
|
|
||
| structure Foo { | ||
| bar: String | ||
| } | ||
| ``` | ||
|
|
||
| The problem with this strategy is that it can only be applied to structure and | ||
| union shapes, because other shape types have nowhere to add the `$type` key or | ||
| it would potentially shadow a valid value key. | ||
|
|
||
| Other shapes could instead be defined via a `__type__` metadata key: | ||
|
|
||
| ```smithy | ||
| $version: "2.0" | ||
|
|
||
| metadata "__type__": [ | ||
| ["foo", "com.example#Foo"] | ||
| ] | ||
|
|
||
| metadata foo = "bar" | ||
|
|
||
| namespace com.example | ||
|
|
||
| string Foo | ||
|
|
||
| @metadata(key: "__type__") | ||
| list MetdataTypeDeclarations { | ||
| member: MetadataTypeDeclaration | ||
| } | ||
|
|
||
| @length(min: 2, max: 2) | ||
| list MetadataTypeDeclaration { | ||
| member: String | ||
| } | ||
| ``` | ||
|
|
||
| Unfortunately, this is inherently a global declaration that is not inline with | ||
| the value, defeating the intent of an inline definition. The metadata trait is | ||
| better suited to this purpose, as it is clearer, easier to validate, more easily | ||
| discoverable, and more easily trackable. | ||
|
|
||
| Smithy 2.1 could introduce new syntax to declare inline metadata types that isn't | ||
| tied to the value, but the AST could not support it without either exposing the | ||
| same problem or making a breaking change. | ||
|
|
||
| ## FAQ | ||
|
|
||
| ### Does this interact with metadata merging semantics? | ||
|
|
||
| When a given metadata key is defined more than once, it usually results in a | ||
| conflict that fails the build. If both definitions are arrays, however, they are | ||
| instead merged. If a metadata key is defined as a list, the merged array will be | ||
| validated. | ||
|
|
||
| This trait does not change merging semantics. | ||
|
|
||
| ### Will the existing defined metadata keys get metadata-trait-based definitions? | ||
|
|
||
| Smithy currently has definitions for three metadata keys: `severityOverrides`, | ||
| `suppressions`, and `validators`. All three could be defined with the metadata | ||
| trait. | ||
|
|
||
| However, these three keys are currently parsed and validated early on when | ||
| loading a model, before validation generally is run. They will need to be | ||
| special-cased to either run early or to be skipped in favor of existing | ||
| validation. | ||
|
|
||
| These keys will initially be reserved so they can't be defined. As the keys get | ||
| full definitions, their reserve list entries will be removed. | ||
|
|
||
| ### What happens if a defined metadata key is not used? | ||
|
|
||
| Nothing. Metadata keys are inherently optional. A `required` member may be added | ||
| later to enforce presence if there is a need. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
smithy-model/src/main/java/software/amazon/smithy/model/traits/MetadataTrait.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.model.traits; | ||
|
|
||
| import software.amazon.smithy.model.node.Node; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
| import software.amazon.smithy.utils.SmithyBuilder; | ||
| import software.amazon.smithy.utils.ToSmithyBuilder; | ||
|
|
||
| /** | ||
| * Globally declares the type of a metadata key. | ||
| * | ||
| * <p>When this trait is applied to a shape, any metadata entry in the model | ||
| * with the same key is validated against the targeted shape. A given | ||
| * metadata key may only have its type declared once across the entire model. | ||
| */ | ||
| public final class MetadataTrait extends AbstractTrait implements ToSmithyBuilder<MetadataTrait> { | ||
| public static final ShapeId ID = ShapeId.from("smithy.api#metadata"); | ||
|
|
||
| private final String key; | ||
|
|
||
| private MetadataTrait(Builder builder) { | ||
| super(ID, builder.getSourceLocation()); | ||
| this.key = SmithyBuilder.requiredState("key", builder.key); | ||
| } | ||
|
|
||
| /** | ||
| * @return Creates a builder for a {@link MetadataTrait}. | ||
| */ | ||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| /** | ||
| * @return The metadata key that this trait defines a type for. | ||
| */ | ||
| public String getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| @Override | ||
| public Builder toBuilder() { | ||
| return builder().sourceLocation(getSourceLocation()).key(key); | ||
| } | ||
|
|
||
| @Override | ||
| protected Node createNode() { | ||
| return Node.objectNodeBuilder() | ||
| .sourceLocation(getSourceLocation()) | ||
| .withMember("key", key) | ||
| .build(); | ||
| } | ||
|
|
||
| public static final class Provider extends AbstractTrait.Provider { | ||
| public Provider() { | ||
| super(ID); | ||
| } | ||
|
|
||
| @Override | ||
| public Trait createTrait(ShapeId target, Node value) { | ||
| Builder builder = builder().sourceLocation(value.getSourceLocation()); | ||
| builder.key(value.expectObjectNode().expectStringMember("key").getValue()); | ||
| MetadataTrait result = builder.build(); | ||
| result.setNodeCache(value); | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| public static final class Builder extends AbstractTraitBuilder<MetadataTrait, Builder> { | ||
| private String key; | ||
|
|
||
| private Builder() {} | ||
|
|
||
| /** | ||
| * Sets the metadata key that this trait defines a type for. | ||
| * | ||
| * @param key The metadata key. Required. | ||
| * @return Returns the builder. | ||
| */ | ||
| public Builder key(String key) { | ||
| this.key = key; | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public MetadataTrait build() { | ||
| return new MetadataTrait(this); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.