Unverified Commit 3ea59928 authored by AWS SDK Rust Bot's avatar AWS SDK Rust Bot Committed by GitHub
Browse files

Merge release branch (and Smithy Upgrade) back into main (#3340)

parents cfd89422 4b600794
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -581,7 +581,13 @@ class DefaultProtocolTestGenerator(
        private val RestXml = "aws.protocoltests.restxml#RestXml"
        private val AwsQuery = "aws.protocoltests.query#AwsQuery"
        private val Ec2Query = "aws.protocoltests.ec2#AwsEc2"
        private val ExpectFail = setOf<FailingTest>()
        private val ExpectFail =
            setOf<FailingTest>(
                // Failing because we don't serialize default values if they match the default
                FailingTest(JsonRpc10, "AwsJson10ClientPopulatesDefaultsValuesWhenMissingInResponse", Action.Request),
                FailingTest(JsonRpc10, "AwsJson10ClientUsesExplicitlyProvidedMemberValuesOverDefaults", Action.Request),
                FailingTest(JsonRpc10, "AwsJson10ClientPopulatesDefaultValuesInInput", Action.Request),
            )
        private val RunOnly: Set<String>? = null

        // These tests are not even attempted to be generated, either because they will not compile
+2 −0
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.BlobShape
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.IntEnumShape
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.MemberShape
@@ -114,6 +115,7 @@ class BaseSymbolMetadataProvider(
            // This covers strings with the enum trait for now, and can be removed once we're fully on EnumShape
            // TODO(https://github.com/smithy-lang/smithy-rs/issues/1700): Remove this `is StringShape` match arm
            is StringShape -> RustMetadata(visibility = Visibility.PUBLIC)
            is IntEnumShape -> RustMetadata(visibility = Visibility.PUBLIC)

            else -> TODO("Unrecognized container type: $container")
        }
+8 −1
Original line number Diff line number Diff line
@@ -18,7 +18,9 @@ import software.amazon.smithy.model.shapes.BlobShape
import software.amazon.smithy.model.shapes.BooleanShape
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.DocumentShape
import software.amazon.smithy.model.shapes.DoubleShape
import software.amazon.smithy.model.shapes.EnumShape
import software.amazon.smithy.model.shapes.FloatShape
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.MemberShape
@@ -520,7 +522,12 @@ class PrimitiveInstantiator(private val runtimeConfig: RuntimeConfig, private va
                            )
                        }

                        is NumberNode -> write(data.value)
                        is NumberNode ->
                            when (shape) {
                                is FloatShape -> rust("${data.value}_f32")
                                is DoubleShape -> rust("${data.value}_f64")
                                else -> rust(data.value.toString())
                            }
                    }

                is BooleanShape -> rust(data.asBooleanNode().get().toString())
+32 −40
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import software.amazon.smithy.model.shapes.DocumentShape
import software.amazon.smithy.model.shapes.DoubleShape
import software.amazon.smithy.model.shapes.EnumShape
import software.amazon.smithy.model.shapes.FloatShape
import software.amazon.smithy.model.shapes.IntEnumShape
import software.amazon.smithy.model.shapes.IntegerShape
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.LongShape
@@ -32,15 +31,16 @@ import software.amazon.smithy.model.shapes.ShortShape
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.shapes.TimestampShape
import software.amazon.smithy.model.traits.DefaultTrait
import software.amazon.smithy.model.traits.EnumDefinition
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.map
import software.amazon.smithy.rust.codegen.core.rustlang.qualifiedName
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider
import software.amazon.smithy.rust.codegen.core.smithy.generators.EnumMemberModel
import software.amazon.smithy.rust.codegen.core.util.UNREACHABLE
import software.amazon.smithy.rust.codegen.core.smithy.generators.PrimitiveInstantiator
import software.amazon.smithy.rust.codegen.core.smithy.rustType
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.expectTrait
import software.amazon.smithy.rust.codegen.core.util.isStreaming
@@ -82,8 +82,14 @@ fun generateFallbackCodeToDefaultValue(
    symbolProvider: RustSymbolProvider,
    publicConstrainedTypes: Boolean,
) {
    val defaultValue = defaultValue(model, runtimeConfig, symbolProvider, member)
    var defaultValue = defaultValue(model, runtimeConfig, symbolProvider, member)
    val targetShape = model.expectShape(member.target)
    val targetSymbol = symbolProvider.toSymbol(targetShape)
    // We need an .into() conversion to create defaults for the server types. A larger scale refactoring could store this information in the
    // symbol, however, retrieving it in this manner works for the moment.
    if (targetSymbol.rustType().qualifiedName().startsWith("::aws_smithy_http_server_python")) {
        defaultValue = defaultValue.map { rust("#T.into()", it) }
    }

    if (member.isStreaming(model)) {
        writer.rust(".unwrap_or_default()")
@@ -131,36 +137,7 @@ fun defaultValue(
    val unsupportedDefaultValueException =
        CodegenException("Default value $node for member shape ${member.id} is unsupported or cannot exist; please file a bug report under https://github.com/smithy-lang/smithy-rs/issues")
    when (val target = model.expectShape(member.target)) {
        is EnumShape, is IntEnumShape -> {
            val value =
                when (target) {
                    is IntEnumShape -> node.expectNumberNode().value
                    is EnumShape -> node.expectStringNode().value
                    else -> throw CodegenException("Default value for shape ${target.id} must be of EnumShape or IntEnumShape")
                }
            val enumValues =
                when (target) {
                    is IntEnumShape -> target.enumValues
                    is EnumShape -> target.enumValues
                    else ->
                        UNREACHABLE(
                            "Target shape ${target.id} must be an `EnumShape` or an `IntEnumShape` at this point, otherwise it would have failed above",
                        )
                }
            val variant =
                enumValues
                    .entries
                    .filter { entry -> entry.value == value }
                    .map { entry ->
                        EnumMemberModel.toEnumVariantName(
                            symbolProvider,
                            target,
                            EnumDefinition.builder().name(entry.key).value(entry.value.toString()).build(),
                        )!!
                    }
                    .first()
            rust("#T::${variant.name}", symbolProvider.toSymbol(target))
        }
        is EnumShape -> PrimitiveInstantiator(runtimeConfig, symbolProvider).instantiate(target, node)(this)

        is ByteShape -> rust(node.expectNumberNode().value.toString() + "i8")
        is ShortShape -> rust(node.expectNumberNode().value.toString() + "i16")
@@ -172,7 +149,7 @@ fun defaultValue(
        is StringShape -> rust("String::from(${node.expectStringNode().value.dq()})")
        is TimestampShape ->
            when (node) {
                is NumberNode -> rust(node.expectNumberNode().value.toString())
                is NumberNode -> PrimitiveInstantiator(runtimeConfig, symbolProvider).instantiate(target, node)(this)
                is StringNode -> {
                    val value = node.expectStringNode().value
                    rustTemplate(
@@ -189,10 +166,12 @@ fun defaultValue(
            check(node is ArrayNode && node.isEmpty)
            rust("Vec::new()")
        }

        is MapShape -> {
            check(node is ObjectNode && node.isEmpty)
            rust("std::collections::HashMap::new()")
        }

        is DocumentShape -> {
            when (node) {
                is NullNode ->
@@ -201,8 +180,18 @@ fun defaultValue(
                        "SmithyTypes" to types,
                    )

                is BooleanNode -> rustTemplate("""#{SmithyTypes}::Document::Bool(${node.value})""", "SmithyTypes" to types)
                is StringNode -> rustTemplate("#{SmithyTypes}::Document::String(String::from(${node.value.dq()}))", "SmithyTypes" to types)
                is BooleanNode ->
                    rustTemplate(
                        """#{SmithyTypes}::Document::Bool(${node.value})""",
                        "SmithyTypes" to types,
                    )

                is StringNode ->
                    rustTemplate(
                        "#{SmithyTypes}::Document::String(String::from(${node.value.dq()}))",
                        "SmithyTypes" to types,
                    )

                is NumberNode -> {
                    val value = node.value.toString()
                    val variant =
@@ -223,14 +212,17 @@ fun defaultValue(

                is ObjectNode -> {
                    check(node.isEmpty)
                    rustTemplate("#{SmithyTypes}::Document::Object(std::collections::HashMap::new())", "SmithyTypes" to types)
                    rustTemplate(
                        "#{SmithyTypes}::Document::Object(std::collections::HashMap::new())",
                        "SmithyTypes" to types,
                    )
                }

                else -> throw unsupportedDefaultValueException
            }
        }

        is BlobShape -> rust("Default::default()")
        is BlobShape -> PrimitiveInstantiator(runtimeConfig, symbolProvider).instantiate(target, node)(this)

        else -> throw unsupportedDefaultValueException
    }
+1 −0
Original line number Diff line number Diff line
@@ -78,6 +78,7 @@ class ServerInstantiator(codegenContext: CodegenContext, customWritable: CustomW
        ServerBuilderKindBehavior(codegenContext),
        defaultsForRequiredFields = true,
        customizations = listOf(ServerAfterInstantiatingValueConstrainItIfNecessary(codegenContext)),
        // Construct with direct pattern to more closely replicate actual server customer usage
        constructPattern = InstantiatorConstructPattern.DIRECT,
        customWritable = customWritable,
    )
Loading