Unverified Commit 7ab8f56d authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

Add support for Endpoints 2.0 Parameters (#1953)

* Add support for Endpoints 2.0 Parameters

This commit adds `EndpointDecorator` which injects Endpoint
parameters in to the operation property bag. These can come from
an ordered list of sources—this wires them all up. To facilitate
testing, this diff also writes the parameters into the property
bag during operation generation.

* remove println

* CR Feedback
parent a26b9ea2
Loading
Loading
Loading
Loading
+23 −0
Original line number Diff line number Diff line
@@ -6,8 +6,11 @@
package software.amazon.smithy.rustsdk

import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rulesengine.language.syntax.parameters.Builtins
import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameter
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.RulesEngineBuiltInResolver
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
@@ -97,6 +100,22 @@ class RegionDecorator : RustCodegenDecorator<ClientProtocolGenerator, ClientCode
        return baseCustomizations + PubUseRegion(codegenContext.runtimeConfig)
    }

    override fun builtInResolvers(codegenContext: ClientCodegenContext): List<RulesEngineBuiltInResolver> {
        return listOf(
            object : RulesEngineBuiltInResolver {
                override fun defaultFor(
                    parameter: Parameter,
                    configRef: String,
                ): Writable? {
                    return when (parameter) {
                        Builtins.REGION -> writable { rust("$configRef.region.as_ref().map(|r|r.as_ref())") }
                        else -> null
                    }
                }
            },
        )
    }

    override fun supportsCodegenContext(clazz: Class<out CodegenContext>): Boolean =
        clazz.isAssignableFrom(ClientCodegenContext::class.java)
}
@@ -117,8 +136,10 @@ class RegionProviderConfig(codegenContext: CodegenContext) : ConfigCustomization
                """,
                *codegenScope,
            )

            is ServiceConfig.BuilderStruct ->
                rustTemplate("region: Option<#{Region}>,", *codegenScope)

            ServiceConfig.BuilderImpl ->
                rustTemplate(
                    """
@@ -162,6 +183,7 @@ class RegionConfigPlugin : OperationCustomization() {
                    """,
                )
            }

            else -> emptySection
        }
    }
@@ -176,6 +198,7 @@ class PubUseRegion(private val runtimeConfig: RuntimeConfig) : LibRsCustomizatio
                    region(runtimeConfig),
                )
            }

            else -> emptySection
        }
    }
+1 −0
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ val allCodegenTests = "../codegen-core/common-test-models".let { commonModels ->
            """.trimIndent(),
            imports = listOf("$commonModels/naming-obstacle-course-structs.smithy"),
        ),
        CodegenTest("aws.protocoltests.json#TestService", "endpoint-rules"),
        CodegenTest("com.aws.example.rust#PokemonService", "pokemon-service-client", imports = listOf("$commonModels/pokemon.smithy", "$commonModels/pokemon-common.smithy")),
    )
}
+33 −0
Original line number Diff line number Diff line
$version: "1.0"

namespace aws.protocoltests.json

use smithy.rules#endpointRuleSet
use smithy.rules#endpointTests

use smithy.rules#clientContextParams
use smithy.rules#staticContextParams
use smithy.rules#contextParam
use aws.protocols#awsJson1_1

@awsJson1_1
@endpointRuleSet({
    "version": "1.0",
    "rules": [],
    "parameters": {
        "Bucket": { "required": false, "type": "String" },
        "Region": { "required": false, "type": "String", "builtIn": "AWS::Region" },
    }
})
service TestService {
    operations: [TestOperation]
}

operation TestOperation {
    input: TestOperationInput
}

structure TestOperationInput {
    @contextParam(name: "Bucket")
    bucket: String
}
+1 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ dependencies {
    implementation("software.amazon.smithy:smithy-aws-traits:$smithyVersion")
    implementation("software.amazon.smithy:smithy-protocol-test-traits:$smithyVersion")
    implementation("software.amazon.smithy:smithy-waiters:$smithyVersion")
    implementation("software.amazon.smithy:smithy-rules-engine:$smithyVersion")
    runtimeOnly(project(":rust-runtime"))
    testImplementation("org.junit.jupiter:junit-jupiter:5.6.1")
    testImplementation("io.kotest:kotest-assertions-core-jvm:$kotestVersion")
+5 −0
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@ package software.amazon.smithy.rust.codegen.client.smithy
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.rust.codegen.client.smithy.customize.RustCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
import software.amazon.smithy.rust.codegen.core.smithy.RustSymbolProvider
@@ -24,6 +26,9 @@ data class ClientCodegenContext(
    override val serviceShape: ServiceShape,
    override val protocol: ShapeId,
    override val settings: ClientRustSettings,
    // Expose the `rootDecorator`, enabling customizations to compose by referencing information from the root codegen
    // decorator
    val rootDecorator: RustCodegenDecorator<ClientProtocolGenerator, ClientCodegenContext>,
) : CodegenContext(
    model, symbolProvider, serviceShape, protocol, settings, CodegenTarget.CLIENT,
)
Loading