Skip to content

Commit

Permalink
Fix formatting and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Fahad Zubair committed Sep 4, 2024
1 parent f2c11b8 commit 763d598
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,10 @@ class CborParserGenerator(
return protocolFunctions.deserializeFn(shape, fnNameSuffix = "payload") { fnName ->
rustTemplate(
"""
pub(crate) fn $fnName(value: &[u8]) -> #{Result}<#{ReturnType}, #{Error}> {
let decoder = &mut #{Decoder}::new(value);
#{DeserializeMember}
}
pub(crate) fn $fnName(value: &[u8]) -> #{Result}<#{ReturnType}, #{Error}> {
let decoder = &mut #{Decoder}::new(value);
#{DeserializeMember}
}
""",
"ReturnType" to returnSymbol.symbol,
"DeserializeMember" to deserializeMember(member),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ class CborSerializerGenerator(
}

val httpDocumentMembers = httpBindingResolver.requestMembers(operationShape, HttpLocation.DOCUMENT)

val inputShape = operationShape.inputShape(model)
return protocolFunctions.serializeFn(operationShape, fnNameSuffix = "input") { fnName ->
rustBlockTemplate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ open class ServerCodegenVisitor(
.let { AttachValidationExceptionToConstrainedOperationInputs.transform(it, settings) }
// Tag aggregate shapes reachable from operation input
.let(ShapesReachableFromOperationInputTagger::transform)
// Remove traits that are not supported by the chosen protocol
// Remove traits that are not supported by the chosen protocol.
.let { ServerProtocolBasedTransformationFactory.transform(it, settings) }
// Normalize event stream operations
.let(EventStreamNormalizer::transform)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.Value
import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenContext
import software.amazon.smithy.rust.codegen.server.smithy.workingWithPublicConstrainedWrapperTupleType

/**
* Constrained shapes are wrapped in a Rust tuple struct that implements all necessary checks. However,
* for serialization purposes, the inner type of the constrained shape is used for serialization.
*
* The `BeforeSerializingMemberCborCustomization` class generates a reference to the inner type when the shape being
* code-generated is constrained and the `publicConstrainedTypes` codegen flag is set.
*/
class BeforeSerializingMemberCborCustomization(private val codegenContext: ServerCodegenContext) : CborSerializerCustomization() {
override fun section(section: CborSerializerSection): Writable =
when (section) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,25 @@ enum class ModelProtocol(val trait: AbstractTrait) {
Rpcv2Cbor(Rpcv2CborTrait.builder().build()),
}

fun loadSmithyConstraintsModelForProtocol(modelProtocol: ModelProtocol): Pair<ShapeId, Model> {
val (serviceShapeId, model) = loadSmithyConstraintsModel()
return Pair(serviceShapeId, model.replaceProtocolTrait(serviceShapeId, modelProtocol))
/**
* Returns the Smithy constraints model from the common repository, with the specified protocol
* applied to the service.
*/
fun loadSmithyConstraintsModelForProtocol(modelProtocol: ModelProtocol): Pair<Model, ShapeId> {
val (model, serviceShapeId) = loadSmithyConstraintsModel()
return Pair(model.replaceProtocolTrait(serviceShapeId, modelProtocol), serviceShapeId)
}

fun loadSmithyConstraintsModel(): Pair<ShapeId, Model> {
/**
* Loads the Smithy constraints model defined in the common repository and returns the model along with
* the service shape defined in it.
*/
fun loadSmithyConstraintsModel(): Pair<Model, ShapeId> {
val filePath = "../codegen-core/common-test-models/constraints.smithy"
val serviceShapeId = ShapeId.from("com.amazonaws.constraints#ConstraintsService")
val model =
File(filePath).readText().asSmithyModel()
return Pair(serviceShapeId, model)
return Pair(model, serviceShapeId)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package software.amazon.smithy.rust.codegen.server.smithy.protocols.serialize

import org.junit.jupiter.api.Test
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
import software.amazon.smithy.rust.codegen.core.testutil.ServerAdditionalSettings
import software.amazon.smithy.rust.codegen.server.smithy.ModelProtocol
import software.amazon.smithy.rust.codegen.server.smithy.loadSmithyConstraintsModelForProtocol
import software.amazon.smithy.rust.codegen.server.smithy.removeOperations
import software.amazon.smithy.rust.codegen.server.smithy.testutil.serverIntegrationTest

class CborConstraintsIntegrationTest {
@Test
fun `ensure CBOR implementation works for all constraint types`() {
val (serviceShape, model) = loadSmithyConstraintsModelForProtocol(ModelProtocol.Rpcv2Cbor)
val (model, serviceShape) = loadSmithyConstraintsModelForProtocol(ModelProtocol.Rpcv2Cbor)
// The test should compile; no further testing is required.
serverIntegrationTest(
model,
Expand Down

0 comments on commit 763d598

Please sign in to comment.