Skip to content

Commit c4d9cee

Browse files
committed
Server codegen integration test works
1 parent 01112a4 commit c4d9cee

26 files changed

+654
-181
lines changed

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/RustClientCodegenPlugin.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customize.NoOpEventStre
1616
import software.amazon.smithy.rust.codegen.client.smithy.customize.RequiredCustomizations
1717
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointsDecorator
1818
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.FluentClientDecorator
19-
import software.amazon.smithy.rust.codegen.client.testutil.DecoratableBuildPlugin
19+
import software.amazon.smithy.rust.codegen.client.testutil.ClientDecoratableBuildPlugin
2020
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.NonExhaustive
2121
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWordSymbolProvider
2222
import software.amazon.smithy.rust.codegen.core.smithy.BaseSymbolMetadataProvider
@@ -36,7 +36,7 @@ import java.util.logging.Logger
3636
* `resources/META-INF.services/software.amazon.smithy.build.SmithyBuildPlugin` refers to this class by name which
3737
* enables the smithy-build plugin to invoke `execute` with all Smithy plugin context + models.
3838
*/
39-
class RustClientCodegenPlugin : DecoratableBuildPlugin() {
39+
class RustClientCodegenPlugin : ClientDecoratableBuildPlugin() {
4040
override fun getName(): String = "rust-client-codegen"
4141

4242
override fun executeWithDecorator(
@@ -66,10 +66,10 @@ class RustClientCodegenPlugin : DecoratableBuildPlugin() {
6666
}
6767

6868
companion object {
69-
/** SymbolProvider
69+
/**
7070
* When generating code, smithy types need to be converted into Rust types—that is the core role of the symbol provider
7171
*
72-
* The Symbol provider is composed of a base `SymbolVisitor` which handles the core functionality, then is layered
72+
* The Symbol provider is composed of a base [SymbolVisitor] which handles the core functionality, then is layered
7373
* with other symbol providers, documented inline, to handle the full scope of Smithy types.
7474
*/
7575
fun baseSymbolProvider(model: Model, serviceShape: ServiceShape, symbolVisitorConfig: SymbolVisitorConfig) =

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/testutil/ClientCodegenIntegrationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fun clientIntegrationTest(
4444
* This exists to allow tests to easily customize the _real_ build without needing to list out customizations
4545
* or attempt to manually discover them from the path.
4646
*/
47-
abstract class DecoratableBuildPlugin : SmithyBuildPlugin {
47+
abstract class ClientDecoratableBuildPlugin : SmithyBuildPlugin {
4848
abstract fun executeWithDecorator(
4949
context: PluginContext,
5050
vararg decorator: ClientCodegenDecorator,

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/testutil/Rust.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,7 @@ fun generatePluginContext(
190190
)
191191
}
192192

193-
val settings = settingsBuilder.merge(additionalSettings)
194-
.build()
193+
val settings = settingsBuilder.merge(additionalSettings).build()
195194
val pluginContext = PluginContext.builder().model(model).fileManifest(manifest).settings(settings).build()
196195
return pluginContext to testPath
197196
}

codegen-server/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ dependencies {
2626
implementation(project(":codegen-core"))
2727
implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
2828
implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
29+
30+
// `smithy.framework#ValidationException` is defined here, which is used in `constraints.smithy`, which is used
31+
// in `CustomValidationExceptionWithReasonDecoratorTest`.
32+
testImplementation("software.amazon.smithy:smithy-validation-model:$smithyVersion")
2933
}
3034

3135
tasks.compileKotlin { kotlinOptions.jvmTarget = "1.8" }

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package software.amazon.smithy.rust.codegen.server.smithy
77

88
import software.amazon.smithy.build.PluginContext
9-
import software.amazon.smithy.build.SmithyBuildPlugin
109
import software.amazon.smithy.codegen.core.ReservedWordSymbolProvider
1110
import software.amazon.smithy.model.Model
1211
import software.amazon.smithy.model.shapes.ServiceShape
@@ -22,6 +21,8 @@ import software.amazon.smithy.rust.codegen.server.smithy.customizations.CustomVa
2221
import software.amazon.smithy.rust.codegen.server.smithy.customizations.ServerRequiredCustomizations
2322
import software.amazon.smithy.rust.codegen.server.smithy.customizations.SmithyValidationExceptionDecorator
2423
import software.amazon.smithy.rust.codegen.server.smithy.customize.CombinedServerCodegenDecorator
24+
import software.amazon.smithy.rust.codegen.server.smithy.customize.ServerCodegenDecorator
25+
import software.amazon.smithy.rust.codegen.server.smithy.testutil.ServerDecoratableBuildPlugin
2526
import java.util.logging.Level
2627
import java.util.logging.Logger
2728

@@ -32,38 +33,34 @@ import java.util.logging.Logger
3233
* `resources/META-INF.services/software.amazon.smithy.build.SmithyBuildPlugin` refers to this class by name which
3334
* enables the smithy-build plugin to invoke `execute` with all Smithy plugin context + models.
3435
*/
35-
class RustCodegenServerPlugin : SmithyBuildPlugin {
36+
class RustCodegenServerPlugin : ServerDecoratableBuildPlugin() {
3637
private val logger = Logger.getLogger(javaClass.name)
3738

3839
override fun getName(): String = "rust-server-codegen"
3940

40-
override fun execute(context: PluginContext) {
41-
// Suppress extremely noisy logs about reserved words
41+
/**
42+
* See [software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin].
43+
*/
44+
override fun executeWithDecorator(
45+
context: PluginContext,
46+
vararg decorator: ServerCodegenDecorator
47+
) {
4248
Logger.getLogger(ReservedWordSymbolProvider::class.java.name).level = Level.OFF
43-
// Discover [RustCodegenDecorators] on the classpath. [RustCodegenDecorator] returns different types of
44-
// customizations. A customization is a function of:
45-
// - location (e.g. the mutate section of an operation)
46-
// - context (e.g. the of the operation)
47-
// - writer: The active RustWriter at the given location
48-
val codegenDecorator: CombinedServerCodegenDecorator =
49+
val codegenDecorator =
4950
CombinedServerCodegenDecorator.fromClasspath(
5051
context,
5152
ServerRequiredCustomizations(),
5253
SmithyValidationExceptionDecorator(),
5354
CustomValidationExceptionWithReasonDecorator(),
55+
*decorator,
5456
)
55-
56-
// ServerCodegenVisitor is the main driver of code generation that traverses the model and generates code
5757
logger.info("Loaded plugin to generate pure Rust bindings for the server SDK")
5858
ServerCodegenVisitor(context, codegenDecorator).execute()
5959
}
6060

6161
companion object {
6262
/**
63-
* When generating code, smithy types need to be converted into Rust types—that is the core role of the symbol provider.
64-
*
65-
* The Symbol provider is composed of a base [SymbolVisitor] which handles the core functionality, then is layered
66-
* with other symbol providers, documented inline, to handle the full scope of Smithy types.
63+
* See [software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin].
6764
*/
6865
fun baseSymbolProvider(
6966
model: Model,

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

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ import software.amazon.smithy.model.shapes.IntegerShape
1616
import software.amazon.smithy.model.shapes.ListShape
1717
import software.amazon.smithy.model.shapes.LongShape
1818
import software.amazon.smithy.model.shapes.MapShape
19+
import software.amazon.smithy.model.shapes.NumberShape
1920
import software.amazon.smithy.model.shapes.OperationShape
2021
import software.amazon.smithy.model.shapes.ServiceShape
2122
import software.amazon.smithy.model.shapes.SetShape
2223
import software.amazon.smithy.model.shapes.Shape
23-
import software.amazon.smithy.model.shapes.ShapeId
2424
import software.amazon.smithy.model.shapes.ShapeVisitor
2525
import software.amazon.smithy.model.shapes.ShortShape
2626
import software.amazon.smithy.model.shapes.StringShape
@@ -63,7 +63,6 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedM
6363
import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedNumberGenerator
6464
import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedStringGenerator
6565
import software.amazon.smithy.rust.codegen.server.smithy.generators.ConstrainedTraitForEnumGenerator
66-
import software.amazon.smithy.rust.codegen.server.smithy.generators.ValidationExceptionConversionGenerator
6766
import software.amazon.smithy.rust.codegen.server.smithy.generators.MapConstraintViolationGenerator
6867
import software.amazon.smithy.rust.codegen.server.smithy.generators.PubCrateConstrainedCollectionGenerator
6968
import software.amazon.smithy.rust.codegen.server.smithy.generators.PubCrateConstrainedMapGenerator
@@ -76,6 +75,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerStruct
7675
import software.amazon.smithy.rust.codegen.server.smithy.generators.UnconstrainedCollectionGenerator
7776
import software.amazon.smithy.rust.codegen.server.smithy.generators.UnconstrainedMapGenerator
7877
import software.amazon.smithy.rust.codegen.server.smithy.generators.UnconstrainedUnionGenerator
78+
import software.amazon.smithy.rust.codegen.server.smithy.generators.ValidationExceptionConversionGenerator
7979
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol
8080
import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolGenerator
8181
import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtocolLoader
@@ -342,7 +342,13 @@ open class ServerCodegenVisitor(
342342

343343
if (isDirectlyConstrained || renderUnconstrainedList) {
344344
rustCrate.withModule(ModelsModule) {
345-
CollectionConstraintViolationGenerator(codegenContext, this, shape, constraintsInfo).render()
345+
CollectionConstraintViolationGenerator(
346+
codegenContext,
347+
this,
348+
shape,
349+
constraintsInfo,
350+
validationExceptionConversionGenerator,
351+
).render()
346352
}
347353
}
348354
}
@@ -382,7 +388,12 @@ open class ServerCodegenVisitor(
382388

383389
if (isDirectlyConstrained || renderUnconstrainedMap) {
384390
rustCrate.withModule(ModelsModule) {
385-
MapConstraintViolationGenerator(codegenContext, this, shape).render()
391+
MapConstraintViolationGenerator(
392+
codegenContext,
393+
this,
394+
shape,
395+
validationExceptionConversionGenerator,
396+
).render()
386397
}
387398
}
388399
}
@@ -394,42 +405,19 @@ open class ServerCodegenVisitor(
394405
*/
395406
override fun stringShape(shape: StringShape) {
396407
fun serverEnumGeneratorFactory(codegenContext: ServerCodegenContext, writer: RustWriter, shape: StringShape) =
397-
ServerEnumGenerator(codegenContext, writer, shape)
408+
ServerEnumGenerator(codegenContext, writer, shape, validationExceptionConversionGenerator)
398409
stringShape(shape, ::serverEnumGeneratorFactory)
399410
}
400411

401-
override fun integerShape(shape: IntegerShape) {
402-
if (shape.isDirectlyConstrained(codegenContext.symbolProvider)) {
403-
logger.info("[rust-server-codegen] Generating a constrained integer $shape")
404-
rustCrate.withModule(ModelsModule) {
405-
ConstrainedNumberGenerator(codegenContext, this, shape).render()
406-
}
407-
}
408-
}
409-
410-
override fun shortShape(shape: ShortShape) {
411-
if (shape.isDirectlyConstrained(codegenContext.symbolProvider)) {
412-
logger.info("[rust-server-codegen] Generating a constrained short $shape")
413-
rustCrate.withModule(ModelsModule) {
414-
ConstrainedNumberGenerator(codegenContext, this, shape).render()
415-
}
416-
}
417-
}
418-
419-
override fun longShape(shape: LongShape) {
420-
if (shape.isDirectlyConstrained(codegenContext.symbolProvider)) {
421-
logger.info("[rust-server-codegen] Generating a constrained long $shape")
422-
rustCrate.withModule(ModelsModule) {
423-
ConstrainedNumberGenerator(codegenContext, this, shape).render()
424-
}
425-
}
426-
}
427-
428-
override fun byteShape(shape: ByteShape) {
412+
override fun integerShape(shape: IntegerShape) = integralShape(shape)
413+
override fun shortShape(shape: ShortShape) = integralShape(shape)
414+
override fun longShape(shape: LongShape) = integralShape(shape)
415+
override fun byteShape(shape: ByteShape) = integralShape(shape)
416+
private fun integralShape(shape: NumberShape) {
429417
if (shape.isDirectlyConstrained(codegenContext.symbolProvider)) {
430-
logger.info("[rust-server-codegen] Generating a constrained byte $shape")
418+
logger.info("[rust-server-codegen] Generating a constrained integral $shape")
431419
rustCrate.withModule(ModelsModule) {
432-
ConstrainedNumberGenerator(codegenContext, this, shape).render()
420+
ConstrainedNumberGenerator(codegenContext, this, shape, validationExceptionConversionGenerator).render()
433421
}
434422
}
435423
}
@@ -552,7 +540,7 @@ open class ServerCodegenVisitor(
552540

553541
if (shape.isDirectlyConstrained(codegenContext.symbolProvider)) {
554542
rustCrate.withModule(ModelsModule) {
555-
ConstrainedBlobGenerator(codegenContext, this, shape).render()
543+
ConstrainedBlobGenerator(codegenContext, this, shape, validationExceptionConversionGenerator).render()
556544
}
557545
}
558546
}

0 commit comments

Comments
 (0)