Skip to content

Commit 4ac79e6

Browse files
authored
Add custom validation exception traits in new codegen-traits package (#4321)
## Motivation and Context <!--- Why is this change required? What problem does it solve? --> <!--- If it fixes an open issue, please link to the issue here --> Extension of #4317. This PR will add the traits and the #4317 will be used to add the validation and codegen. Deferring moving existing traits to this new package to another PR to avoid clashing refactors with features. ## Description <!--- Describe your changes in detail --> ## Testing <!--- Please describe in detail how you tested your changes --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Checklist <!--- If a checkbox below is not applicable, then please DELETE it rather than leaving it unchecked --> - [x] For changes to the smithy-rs codegen or runtime crates, I have created a changelog entry Markdown file in the `.changelog` directory, specifying "client," "server," or both in the `applies_to` key. - [x] For changes to the AWS SDK, generated SDK code, or SDK runtime crates, I have created a changelog entry Markdown file in the `.changelog` directory, specifying "aws-sdk-rust" in the `applies_to` key. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 8b844a1 commit 4ac79e6

18 files changed

+345
-1
lines changed

.changelog/1759416345.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
applies_to: ["server"]
3+
authors: ["jasgin"]
4+
references: ["smithy-rs#4321"]
5+
breaking: false
6+
new_feature: true
7+
bug_fix: false
8+
---
9+
10+
Adds the custom traits `@validationException`, `@validationMessage`, `@validationFieldList`, `@validationFieldName`, and `@validationFieldMessage`
11+
for defining custom validation exceptions.

codegen-server/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ extra["moduleName"] = "software.amazon.smithy.rust.codegen.server"
1616

1717
dependencies {
1818
implementation(project(":codegen-core"))
19+
implementation(project(":codegen-traits"))
1920
implementation(libs.smithy.aws.traits)
2021
implementation(libs.smithy.protocol.test.traits)
2122
implementation(libs.smithy.protocol.traits)

codegen-traits/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
plugins {
7+
id("smithy-rs.kotlin-conventions")
8+
id("smithy-rs.publishing-conventions")
9+
}
10+
11+
description = "Smithy traits for Rust code generation"
12+
extra["displayName"] = "Smithy :: Rust :: Codegen :: Traits"
13+
extra["moduleName"] = "software.amazon.smithy.rust.codegen.traits"
14+
15+
dependencies {
16+
implementation(libs.smithy.model)
17+
testImplementation(libs.junit.jupiter)
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.traits
7+
8+
import software.amazon.smithy.model.SourceLocation
9+
import software.amazon.smithy.model.node.Node
10+
import software.amazon.smithy.model.shapes.ShapeId
11+
import software.amazon.smithy.model.traits.AbstractTrait
12+
import software.amazon.smithy.model.traits.Trait
13+
14+
class ValidationExceptionTrait(
15+
sourceLocation: SourceLocation,
16+
) : AbstractTrait(ID, sourceLocation) {
17+
companion object {
18+
val ID: ShapeId = ShapeId.from("smithy.rust.codegen.traits#validationException")
19+
}
20+
21+
override fun createNode(): Node = Node.objectNode()
22+
23+
class Provider : AbstractTrait.Provider(ID) {
24+
override fun createTrait(
25+
target: ShapeId,
26+
value: Node,
27+
): Trait {
28+
val result = ValidationExceptionTrait(value.sourceLocation)
29+
result.setNodeCache(value)
30+
return result
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.traits
7+
8+
import software.amazon.smithy.model.SourceLocation
9+
import software.amazon.smithy.model.node.Node
10+
import software.amazon.smithy.model.shapes.ShapeId
11+
import software.amazon.smithy.model.traits.AbstractTrait
12+
import software.amazon.smithy.model.traits.Trait
13+
14+
class ValidationFieldListTrait(
15+
sourceLocation: SourceLocation,
16+
) : AbstractTrait(ID, sourceLocation) {
17+
companion object {
18+
val ID: ShapeId = ShapeId.from("smithy.rust.codegen.traits#validationFieldList")
19+
}
20+
21+
override fun createNode(): Node = Node.objectNode()
22+
23+
class Provider : AbstractTrait.Provider(ID) {
24+
override fun createTrait(
25+
target: ShapeId,
26+
value: Node,
27+
): Trait {
28+
val result = ValidationFieldListTrait(value.sourceLocation)
29+
result.setNodeCache(value)
30+
return result
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.traits
7+
8+
import software.amazon.smithy.model.SourceLocation
9+
import software.amazon.smithy.model.node.Node
10+
import software.amazon.smithy.model.shapes.ShapeId
11+
import software.amazon.smithy.model.traits.AbstractTrait
12+
import software.amazon.smithy.model.traits.Trait
13+
14+
class ValidationFieldMessageTrait(
15+
sourceLocation: SourceLocation,
16+
) : AbstractTrait(ID, sourceLocation) {
17+
companion object {
18+
val ID: ShapeId = ShapeId.from("smithy.rust.codegen.traits#validationFieldMessage")
19+
}
20+
21+
override fun createNode(): Node = Node.objectNode()
22+
23+
class Provider : AbstractTrait.Provider(ID) {
24+
override fun createTrait(
25+
target: ShapeId,
26+
value: Node,
27+
): Trait {
28+
val result = ValidationFieldMessageTrait(value.sourceLocation)
29+
result.setNodeCache(value)
30+
return result
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.traits
7+
8+
import software.amazon.smithy.model.SourceLocation
9+
import software.amazon.smithy.model.node.Node
10+
import software.amazon.smithy.model.shapes.ShapeId
11+
import software.amazon.smithy.model.traits.AbstractTrait
12+
import software.amazon.smithy.model.traits.Trait
13+
14+
class ValidationFieldNameTrait(
15+
sourceLocation: SourceLocation,
16+
) : AbstractTrait(ID, sourceLocation) {
17+
companion object {
18+
val ID: ShapeId = ShapeId.from("smithy.rust.codegen.traits#validationFieldName")
19+
}
20+
21+
override fun createNode(): Node = Node.objectNode()
22+
23+
class Provider : AbstractTrait.Provider(ID) {
24+
override fun createTrait(
25+
target: ShapeId,
26+
value: Node,
27+
): Trait {
28+
val result = ValidationFieldNameTrait(value.sourceLocation)
29+
result.setNodeCache(value)
30+
return result
31+
}
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package software.amazon.smithy.rust.codegen.traits
7+
8+
import software.amazon.smithy.model.SourceLocation
9+
import software.amazon.smithy.model.node.Node
10+
import software.amazon.smithy.model.shapes.ShapeId
11+
import software.amazon.smithy.model.traits.AbstractTrait
12+
import software.amazon.smithy.model.traits.Trait
13+
14+
class ValidationMessageTrait(
15+
sourceLocation: SourceLocation,
16+
) : AbstractTrait(ID, sourceLocation) {
17+
companion object {
18+
val ID: ShapeId = ShapeId.from("smithy.rust.codegen.traits#validationMessage")
19+
}
20+
21+
override fun createNode(): Node = Node.objectNode()
22+
23+
class Provider : AbstractTrait.Provider(ID) {
24+
override fun createTrait(
25+
target: ShapeId,
26+
value: Node,
27+
): Trait {
28+
val result = ValidationMessageTrait(value.sourceLocation)
29+
result.setNodeCache(value)
30+
return result
31+
}
32+
}
33+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
validation-exception.smithy
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
software.amazon.smithy.rust.codegen.server.smithy.traits.ValidationExceptionTrait$Provider
2+
software.amazon.smithy.rust.codegen.server.smithy.traits.ValidationMessageTrait$Provider
3+
software.amazon.smithy.rust.codegen.server.smithy.traits.ValidationFieldListTrait$Provider
4+
software.amazon.smithy.rust.codegen.server.smithy.traits.ValidationFieldNameTrait$Provider
5+
software.amazon.smithy.rust.codegen.server.smithy.traits.ValidationFieldMessageTrait$Provider

0 commit comments

Comments
 (0)