Skip to content

Commit 2668087

Browse files
committed
Fix TODOs, run ktlint
1 parent c4d9cee commit 2668087

18 files changed

+80
-39
lines changed

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/RustCodegenServerPlugin.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class RustCodegenServerPlugin : ServerDecoratableBuildPlugin() {
4343
*/
4444
override fun executeWithDecorator(
4545
context: PluginContext,
46-
vararg decorator: ServerCodegenDecorator
46+
vararg decorator: ServerCodegenDecorator,
4747
) {
4848
Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF
4949
val codegenDecorator =

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerRustSettings.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,13 @@ data class ServerCodegenConfig(
8383
override val debugMode: Boolean = defaultDebugMode,
8484
val publicConstrainedTypes: Boolean = defaultPublicConstrainedTypes,
8585
val ignoreUnsupportedConstraints: Boolean = defaultIgnoreUnsupportedConstraints,
86-
// TODO Docs
87-
val experimentalCustomValidationExceptionWithReasonPleaseDoNotUse: String? = defaultExperimentalCustomValidationExceptionWithReasonPleaseDoNotUse
86+
/**
87+
* A flag to enable _experimental_ support for custom validation exceptions via the
88+
* [CustomValidationExceptionWithReasonDecorator] decorator.
89+
* TODO(https://github.com/awslabs/smithy-rs/pull/2053): this will go away once we implement the RFC, when users will be
90+
* able to define the converters in their Rust application code.
91+
*/
92+
val experimentalCustomValidationExceptionWithReasonPleaseDoNotUse: String? = defaultExperimentalCustomValidationExceptionWithReasonPleaseDoNotUse,
8893
) : CoreCodegenConfig(
8994
formatTimeoutSeconds, debugMode,
9095
) {

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ValidateUnsupportedConstraints.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ data class ValidationResult(val shouldAbort: Boolean, val messages: List<LogMess
134134

135135
private val unsupportedConstraintsOnMemberShapes = allConstraintTraits - RequiredTrait::class.java
136136

137+
/**
138+
* Validate that all constrained operations have the shape [validationExceptionShapeId] shape attached to their errors.
139+
*/
137140
fun validateOperationsWithConstrainedInputHaveValidationExceptionAttached(
138141
model: Model,
139142
service: ServiceShape,
140-
// TODO Docs
141143
validationExceptionShapeId: ShapeId,
142144
): ValidationResult {
143145
// Traverse the model and error out if an operation uses constrained input, but it does not have

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/CustomValidationExceptionWithReasonDecorator.kt

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
package software.amazon.smithy.rust.codegen.server.smithy.customizations
27

38
import software.amazon.smithy.model.Model
@@ -23,18 +28,26 @@ import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegen
2328
import software.amazon.smithy.rust.codegen.server.smithy.generators.BlobLength
2429
import software.amazon.smithy.rust.codegen.server.smithy.generators.CollectionTraitInfo
2530
import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstraintViolation
26-
import software.amazon.smithy.rust.codegen.server.smithy.generators.ValidationExceptionConversionGenerator
2731
import software.amazon.smithy.rust.codegen.server.smithy.generators.Length
2832
import software.amazon.smithy.rust.codegen.server.smithy.generators.Pattern
2933
import software.amazon.smithy.rust.codegen.server.smithy.generators.Range
3034
import software.amazon.smithy.rust.codegen.server.smithy.generators.StringTraitInfo
31-
import software.amazon.smithy.rust.codegen.server.smithy.generators.TraitInfo
35+
import software.amazon.smithy.rust.codegen.server.smithy.generators.ValidationExceptionConversionGenerator
3236
import software.amazon.smithy.rust.codegen.server.smithy.generators.isKeyConstrained
3337
import software.amazon.smithy.rust.codegen.server.smithy.generators.isValueConstrained
3438
import software.amazon.smithy.rust.codegen.server.smithy.validationErrorMessage
3539

36-
// TODO Docs
37-
class CustomValidationExceptionWithReasonDecorator: ServerCodegenDecorator {
40+
/**
41+
* A decorator that adds code to convert from constraint violations to a custom `ValidationException` shape that is very
42+
* similar to `smithy.framework#ValidationException`, with an additional `reason` field.
43+
*
44+
* The shape definition is in [CustomValidationExceptionWithReasonDecoratorTest].
45+
*
46+
* This is just an example to showcase experimental support for custom validation exceptions.
47+
* TODO(https://github.com/awslabs/smithy-rs/pull/2053): this will go away once we implement the RFC, when users will be
48+
* able to define the converters in their Rust application code.
49+
*/
50+
class CustomValidationExceptionWithReasonDecorator : ServerCodegenDecorator {
3851
override val name: String
3952
get() = "CustomValidationExceptionWithReasonDecorator"
4053
override val order: Byte
@@ -49,8 +62,7 @@ class CustomValidationExceptionWithReasonDecorator: ServerCodegenDecorator {
4962
}
5063
}
5164

52-
// TODO Docs
53-
class ValidationExceptionWithReasonConversionGenerator(private val codegenContext: ServerCodegenContext):
65+
class ValidationExceptionWithReasonConversionGenerator(private val codegenContext: ServerCodegenContext) :
5466
ValidationExceptionConversionGenerator {
5567
override val shapeId: ShapeId =
5668
ShapeId.from(codegenContext.settings.codegenConfig.experimentalCustomValidationExceptionWithReasonPleaseDoNotUse)
@@ -243,7 +255,7 @@ class ValidationExceptionWithReasonConversionGenerator(private val codegenContex
243255

244256
override fun collectionShapeConstraintViolationImplBlock(
245257
collectionConstraintsInfo:
246-
Collection<CollectionTraitInfo>,
258+
Collection<CollectionTraitInfo>,
247259
isMemberConstrained: Boolean,
248260
) = writable {
249261
val validationExceptionFields = collectionConstraintsInfo.map {
@@ -271,7 +283,6 @@ class ValidationExceptionWithReasonConversionGenerator(private val codegenContex
271283
},
272284
""",
273285
)
274-
275286
}
276287
}
277288
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customizations/SmithyValidationExceptionDecorator.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
package software.amazon.smithy.rust.codegen.server.smithy.customizations
27

38
import software.amazon.smithy.model.Model
@@ -7,7 +12,6 @@ import software.amazon.smithy.model.shapes.ShapeId
712
import software.amazon.smithy.model.shapes.StringShape
813
import software.amazon.smithy.model.traits.EnumTrait
914
import software.amazon.smithy.model.traits.LengthTrait
10-
import software.amazon.smithy.model.traits.RangeTrait
1115
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
1216
import software.amazon.smithy.rust.codegen.core.rustlang.join
1317
import software.amazon.smithy.rust.codegen.core.rustlang.rust
@@ -25,15 +29,25 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.BlobLength
2529
import software.amazon.smithy.rust.codegen.server.smithy.generators.CollectionTraitInfo
2630
import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstraintViolation
2731
import software.amazon.smithy.rust.codegen.server.smithy.generators.Range
28-
import software.amazon.smithy.rust.codegen.server.smithy.generators.ValidationExceptionConversionGenerator
2932
import software.amazon.smithy.rust.codegen.server.smithy.generators.StringTraitInfo
3033
import software.amazon.smithy.rust.codegen.server.smithy.generators.TraitInfo
34+
import software.amazon.smithy.rust.codegen.server.smithy.generators.ValidationExceptionConversionGenerator
3135
import software.amazon.smithy.rust.codegen.server.smithy.generators.isKeyConstrained
3236
import software.amazon.smithy.rust.codegen.server.smithy.generators.isValueConstrained
3337
import software.amazon.smithy.rust.codegen.server.smithy.validationErrorMessage
3438

35-
// TODO Docs
36-
class SmithyValidationExceptionDecorator: ServerCodegenDecorator {
39+
/**
40+
* A decorator that adds code to convert from constraint violations to Smithy's `smithy.framework#ValidationException`,
41+
* defined in [0]. This is Smithy's recommended shape to return when validation fails.
42+
*
43+
* This decorator is always enabled when using the `rust-server-codegen` plugin.
44+
*
45+
* [0]: https://github.com/awslabs/smithy/tree/main/smithy-validation-model
46+
*
47+
* TODO(https://github.com/awslabs/smithy-rs/pull/2053): once the RFC is implemented, consider moving this back into the
48+
* generators.
49+
*/
50+
class SmithyValidationExceptionDecorator : ServerCodegenDecorator {
3751
override val name: String
3852
get() = "SmithyValidationExceptionDecorator"
3953
override val order: Byte
@@ -43,8 +57,7 @@ class SmithyValidationExceptionDecorator: ServerCodegenDecorator {
4357
SmithyValidationExceptionConversionGenerator(codegenContext)
4458
}
4559

46-
// TODO Docs
47-
class SmithyValidationExceptionConversionGenerator(private val codegenContext: ServerCodegenContext):
60+
class SmithyValidationExceptionConversionGenerator(private val codegenContext: ServerCodegenContext) :
4861
ValidationExceptionConversionGenerator {
4962

5063
// Define a companion object so that we can refer to this shape id globally.
@@ -197,7 +210,7 @@ class SmithyValidationExceptionConversionGenerator(private val codegenContext: S
197210

198211
override fun collectionShapeConstraintViolationImplBlock(
199212
collectionConstraintsInfo:
200-
Collection<CollectionTraitInfo>,
213+
Collection<CollectionTraitInfo>,
201214
isMemberConstrained: Boolean,
202215
) = writable {
203216
val validationExceptionFields = collectionConstraintsInfo.map { it.toTraitInfo().asValidationExceptionField }.toMutableList()

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/customize/ServerCodegenDecorator.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ typealias ServerProtocolMap = ProtocolMap<ServerProtocolGenerator, ServerCodegen
2222
*/
2323
interface ServerCodegenDecorator : CoreCodegenDecorator<ServerCodegenContext> {
2424
fun protocols(serviceId: ShapeId, currentProtocols: ServerProtocolMap): ServerProtocolMap = currentProtocols
25-
26-
// TODO Docs
2725
fun validationExceptionConversion(codegenContext: ServerCodegenContext): ValidationExceptionConversionGenerator? = null
2826
}
2927

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/CollectionConstraintViolationGenerator.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.Visibility
1111
import software.amazon.smithy.rust.codegen.core.rustlang.join
1212
import software.amazon.smithy.rust.codegen.core.rustlang.rust
1313
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
14-
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
1514
import software.amazon.smithy.rust.codegen.core.smithy.module
1615
import software.amazon.smithy.rust.codegen.server.smithy.PubCrateConstraintViolationSymbolProvider
1716
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
@@ -83,7 +82,7 @@ class CollectionConstraintViolationGenerator(
8382
#{CollectionShapeConstraintViolationImplBlock}
8483
}
8584
""",
86-
"CollectionShapeConstraintViolationImplBlock" to validationExceptionConversionGenerator.collectionShapeConstraintViolationImplBlock(collectionConstraintsInfo, isMemberConstrained)
85+
"CollectionShapeConstraintViolationImplBlock" to validationExceptionConversionGenerator.collectionShapeConstraintViolationImplBlock(collectionConstraintsInfo, isMemberConstrained),
8786
)
8887
}
8988
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedBlobGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class ConstrainedBlobGenerator(
133133
#{BlobShapeConstraintViolationImplBlock}
134134
}
135135
""",
136-
"BlobShapeConstraintViolationImplBlock" to validationExceptionConversionGenerator.blobShapeConstraintViolationImplBlock(blobConstraintsInfo)
136+
"BlobShapeConstraintViolationImplBlock" to validationExceptionConversionGenerator.blobShapeConstraintViolationImplBlock(blobConstraintsInfo),
137137
)
138138
}
139139
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedNumberGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class ConstrainedNumberGenerator(
149149
#{NumberShapeConstraintViolationImplBlock}
150150
}
151151
""",
152-
"NumberShapeConstraintViolationImplBlock" to validationExceptionConversionGenerator.numberShapeConstraintViolationImplBlock(rangeInfo)
152+
"NumberShapeConstraintViolationImplBlock" to validationExceptionConversionGenerator.numberShapeConstraintViolationImplBlock(rangeInfo),
153153
)
154154
}
155155
}

codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ConstrainedStringGenerator.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ class ConstrainedStringGenerator(
161161
#{StringShapeConstraintViolationImplBlock:W}
162162
}
163163
""",
164-
"StringShapeConstraintViolationImplBlock" to validationExceptionConversionGenerator.stringShapeConstraintViolationImplBlock(stringConstraintsInfo)
164+
"StringShapeConstraintViolationImplBlock" to validationExceptionConversionGenerator.stringShapeConstraintViolationImplBlock(stringConstraintsInfo),
165165
)
166166
}
167167
}

0 commit comments

Comments
 (0)