Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,16 @@ After the `addMembers` transformer runs, `MyStruct` has the original `original`

## `removeTraits`

Unlike the others, this one isn't driven by a trait — it's model-wide, so there's no shape to attach it to. It's configured by the `removeTraits` metadata key, whose type is declared with Smithy's [`@metadata` trait](https://smithy.io/2.0/spec/model.html#metadata-trait), so the value is validated as part of loading the model.
Unlike the others, this one isn't driven by a trait — it's model-wide, so there's no shape to attach it to. It's configured by the `smithytransformations#removeTraits` metadata key, whose type is declared with Smithy's [`@metadata` trait](https://smithy.io/2.0/spec/model.html#metadata-trait), so the value is validated as part of loading the model.

Metadata keys are plain strings — Smithy attaches no namespace meaning to the `#`. It's qualified here purely to avoid collisions: metadata is merged across every model loaded together, so a bare `removeTraits` would clash with any other library that picked the same word. (The transformer's *name*, used in `smithy4sModelTransformers` and `smithy-build.json`, is just `removeTraits`.)

Each entry is a [selector](https://smithy.io/2.0/spec/selectors.html) matching the **trait definition shapes** to strip:

```smithy
$version: "2"

metadata "removeTraits" = [
metadata "smithytransformations#removeTraits" = [
"[trait|trait][id|namespace = 'smithy.rules']" // every trait in the namespace
"[id = 'smithy.api#deprecated']" // just this one trait
]
Expand Down
2 changes: 1 addition & 1 deletion smithy4sExample/src/main/smithy/example.smithy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$version: "2"

// every trait in the `custom` namespace is stripped from the model before codegen runs
metadata "removeTraits" = ["[trait|trait][id|namespace = 'custom']"]
metadata "smithytransformations#removeTraits" = ["[trait|trait][id|namespace = 'custom']"]

namespace example

Expand Down
30 changes: 15 additions & 15 deletions tests/src/test/scala/smithytransformations/RemoveTraitsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class RemoveTraitsTest extends FunSuite {
customTraits,
"""|$version: "2"
|
|metadata "removeTraits" = ["[trait|trait][id|namespace = 'custom']"]
|metadata "smithytransformations#removeTraits" = ["[trait|trait][id|namespace = 'custom']"]
|
|namespace example
|
Expand All @@ -52,7 +52,7 @@ class RemoveTraitsTest extends FunSuite {
customTraits,
"""|$version: "2"
|
|metadata "removeTraits" = ["[trait|trait][id|namespace = 'custom']"]
|metadata "smithytransformations#removeTraits" = ["[trait|trait][id|namespace = 'custom']"]
|
|namespace example
|
Expand All @@ -71,7 +71,7 @@ class RemoveTraitsTest extends FunSuite {
customTraits,
"""|$version: "2"
|
|metadata "removeTraits" = ["[id = 'custom#alpha']"]
|metadata "smithytransformations#removeTraits" = ["[id = 'custom#alpha']"]
|
|namespace example
|
Expand All @@ -89,7 +89,7 @@ class RemoveTraitsTest extends FunSuite {
customTraits,
"""|$version: "2"
|
|metadata "removeTraits" = ["[id = 'custom#alpha']"]
|metadata "smithytransformations#removeTraits" = ["[id = 'custom#alpha']"]
|
|namespace example
|
Expand All @@ -111,7 +111,7 @@ class RemoveTraitsTest extends FunSuite {
otherTraits,
"""|$version: "2"
|
|metadata "removeTraits" = ["[trait|trait][id|namespace = 'custom']", "[id = 'other#gamma']"]
|metadata "smithytransformations#removeTraits" = ["[trait|trait][id|namespace = 'custom']", "[id = 'other#gamma']"]
|
|namespace example
|
Expand All @@ -133,7 +133,7 @@ class RemoveTraitsTest extends FunSuite {
otherTraits,
"""|$version: "2"
|
|metadata "removeTraits" = ["[trait|trait][id|namespace = 'custom']", "[id = 'other#gamma']"]
|metadata "smithytransformations#removeTraits" = ["[trait|trait][id|namespace = 'custom']", "[id = 'other#gamma']"]
|
|namespace example
|
Expand Down Expand Up @@ -164,7 +164,7 @@ class RemoveTraitsTest extends FunSuite {
val model =
"""|$version: "2"
|
|metadata "removeTraits" = []
|metadata "smithytransformations#removeTraits" = []
|
|namespace example
|
Expand All @@ -180,7 +180,7 @@ class RemoveTraitsTest extends FunSuite {
val model =
"""|$version: "2"
|
|metadata "removeTraits" = ["[trait|trait][id|namespace = 'nope']"]
|metadata "smithytransformations#removeTraits" = ["[trait|trait][id|namespace = 'nope']"]
|
|namespace example
|
Expand All @@ -196,15 +196,15 @@ class RemoveTraitsTest extends FunSuite {
val errors = validationErrorsFor(
"""|$version: "2"
|
|metadata "removeTraits" = "custom"
|metadata "smithytransformations#removeTraits" = "custom"
|
|namespace example
|
|structure MyStruct {}
|""".stripMargin
)
assert(
errors.exists(_.contains("removeTraits")),
errors.exists(_.contains("smithytransformations#removeTraits")),
errors.mkString("\n"),
)
}
Expand All @@ -213,15 +213,15 @@ class RemoveTraitsTest extends FunSuite {
val errors = validationErrorsFor(
"""|$version: "2"
|
|metadata "removeTraits" = [""]
|metadata "smithytransformations#removeTraits" = [""]
|
|namespace example
|
|structure MyStruct {}
|""".stripMargin
)
assert(
errors.exists(_.contains("removeTraits")),
errors.exists(_.contains("smithytransformations#removeTraits")),
errors.mkString("\n"),
)
}
Expand All @@ -242,7 +242,7 @@ class RemoveTraitsTest extends FunSuite {
|""".stripMargin,
"""|$version: "2"
|
|metadata "removeTraits" = ["[trait|trait][trait|smithy.api#unstable]"]
|metadata "smithytransformations#removeTraits" = ["[trait|trait][trait|smithy.api#unstable]"]
|
|namespace example
|
Expand Down Expand Up @@ -270,7 +270,7 @@ class RemoveTraitsTest extends FunSuite {
|""".stripMargin,
"""|$version: "2"
|
|metadata "removeTraits" = ["[trait|trait][trait|smithy.api#unstable]"]
|metadata "smithytransformations#removeTraits" = ["[trait|trait][trait|smithy.api#unstable]"]
|
|namespace example
|
Expand All @@ -289,7 +289,7 @@ class RemoveTraitsTest extends FunSuite {
val model = loadModel(
"""|$version: "2"
|
|metadata "removeTraits" = ["[[[not a selector"]
|metadata "smithytransformations#removeTraits" = ["[[[not a selector"]
|
|namespace example
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/**
* Strips traits off every shape in the model.
*
* <p>Configured by the {@code removeTraits} metadata key, whose shape (and therefore whose
* <p>Configured by the {@code smithytransformations#removeTraits} metadata key, whose shape (and therefore whose
* validation) is declared with {@code @metadata} in {@code smithytransformations.smithy}. Metadata
* rather than a trait, because the transformation is model-wide and has no shape to attach itself
* to.
Expand All @@ -45,7 +45,7 @@
public final class RemoveTraits implements ProjectionTransformer {

/** Metadata key holding the selectors. Kept in sync with the {@code @metadata} declaration. */
static final String METADATA_KEY = "removeTraits";
static final String METADATA_KEY = "smithytransformations#removeTraits";

@Override
public String getName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ map TraitMap {

/// Selectors matching the trait definition shapes whose traits should be stripped off
/// every shape in the model, e.g. `[trait|trait][id|namespace = 'smithy.rules']`.
@metadata(key: "removeTraits")
@metadata(key: "smithytransformations#removeTraits")
list RemoveTraits {
member: RemoveTraitsSelector
}
Expand Down
Loading