Skip to content

Commit

Permalink
Reorganize the types/models/primitives modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jdisanti authored and aws-sdk-rust-ci committed Feb 23, 2023
1 parent 0da92a4 commit 3221246
Show file tree
Hide file tree
Showing 20 changed files with 119 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ abstract class BaseRequestIdDecorator : ClientCodegenDecorator {
): List<BuilderCustomization> = baseCustomizations + listOf(RequestIdBuilderCustomization())

override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) {
rustCrate.withModule(ClientRustModule.Types) {
rustCrate.withModule(
when (codegenContext.settings.codegenConfig.enableNewCrateOrganizationScheme) {
true -> ClientRustModule.Operation
else -> ClientRustModule.types
},
) {
// Re-export RequestId in generated crate
rust("pub use #T;", accessorTrait(codegenContext))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,13 @@ package software.amazon.smithy.rustsdk

import org.junit.jupiter.api.Test
import software.amazon.smithy.rust.codegen.client.testutil.validateConfigCustomizations
import software.amazon.smithy.rust.codegen.core.smithy.CoreRustSettings
import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
import software.amazon.smithy.rust.codegen.core.testutil.rustSettings

class HttpConnectorConfigCustomizationTest {
@Test
fun `generates a valid config`() {
val project = TestWorkspace.testProject()
val projectSettings = project.rustSettings()
val codegenContext = awsTestCodegenContext(
coreRustSettings = CoreRustSettings(
service = projectSettings.service,
moduleName = projectSettings.moduleName,
moduleVersion = projectSettings.moduleVersion,
moduleAuthors = projectSettings.moduleAuthors,
moduleDescription = projectSettings.moduleDescription,
moduleRepository = projectSettings.moduleRepository,
runtimeConfig = AwsTestRuntimeConfig,
codegenConfig = projectSettings.codegenConfig,
license = projectSettings.license,
examplesUri = projectSettings.examplesUri,
customizationConfig = projectSettings.customizationConfig,
),
)
val codegenContext = awsTestCodegenContext()
validateConfigCustomizations(HttpConnectorConfigCustomization(codegenContext), project)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,21 @@
package software.amazon.smithy.rustsdk

import org.junit.jupiter.api.Test
import software.amazon.smithy.rust.codegen.client.testutil.testClientRustSettings
import software.amazon.smithy.rust.codegen.client.testutil.validateConfigCustomizations
import software.amazon.smithy.rust.codegen.core.smithy.CoreRustSettings
import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
import software.amazon.smithy.rust.codegen.core.testutil.rustSettings

internal class RegionProviderConfigTest {
@Test
fun `generates a valid config`() {
val project = TestWorkspace.testProject()
val projectSettings = project.rustSettings()
val coreRustSettings = CoreRustSettings(
service = projectSettings.service,
moduleName = projectSettings.moduleName,
moduleVersion = projectSettings.moduleVersion,
moduleAuthors = projectSettings.moduleAuthors,
moduleDescription = projectSettings.moduleDescription,
moduleRepository = projectSettings.moduleRepository,
runtimeConfig = AwsTestRuntimeConfig,
codegenConfig = projectSettings.codegenConfig,
license = projectSettings.license,
examplesUri = projectSettings.examplesUri,
customizationConfig = projectSettings.customizationConfig,
val codegenContext = awsTestCodegenContext(
settings = testClientRustSettings(
moduleName = project.rustSettings().moduleName,
runtimeConfig = AwsTestRuntimeConfig,
),
)
val codegenContext = awsTestCodegenContext(coreRustSettings = coreRustSettings)
validateConfigCustomizations(RegionProviderConfig(codegenContext), project)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ package software.amazon.smithy.rustsdk
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.node.ObjectNode
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustSettings
import software.amazon.smithy.rust.codegen.client.testutil.clientIntegrationTest
import software.amazon.smithy.rust.codegen.client.testutil.testCodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.CoreRustSettings
import software.amazon.smithy.rust.codegen.client.testutil.testClientCodegenContext
import software.amazon.smithy.rust.codegen.client.testutil.testClientRustSettings
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeCrateLocation
import software.amazon.smithy.rust.codegen.core.smithy.RustCrate
import software.amazon.smithy.rust.codegen.core.testutil.IntegrationTestParams
import software.amazon.smithy.rust.codegen.core.testutil.TestRuntimeConfig
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.testutil.testRustSettings
import java.io.File

// In aws-sdk-codegen, the working dir when gradle runs tests is actually `./aws`. So, to find the smithy runtime, we need
Expand All @@ -29,10 +29,10 @@ val AwsTestRuntimeConfig = TestRuntimeConfig.copy(
},
)

fun awsTestCodegenContext(model: Model? = null, coreRustSettings: CoreRustSettings?) =
testCodegenContext(
fun awsTestCodegenContext(model: Model? = null, settings: ClientRustSettings? = null) =
testClientCodegenContext(
model ?: "namespace test".asSmithyModel(),
settings = coreRustSettings ?: testRustSettings(runtimeConfig = AwsTestRuntimeConfig),
settings = settings ?: testClientRustSettings(runtimeConfig = AwsTestRuntimeConfig),
)

fun awsSdkIntegrationTest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,47 @@ object ClientRustModule {

/** crate::client */
val client = Client.self

object Client {
/** crate::client */
val self = RustModule.public("client", "Client and fluent builders for calling the service.")

/** crate::client::customize */
val customize = RustModule.public("customize", "Operation customization and supporting types", parent = self)
val customize = RustModule.public("customize", parent = self, documentation = "Operation customization and supporting types")
}

val Config = RustModule.public("config", documentation = "Configuration for the service.")
val Error = RustModule.public("error", documentation = "All error types that operations can return. Documentation on these types is copied from the model.")
val Operation = RustModule.public("operation", documentation = "All operations that this crate can perform.")
val Meta = RustModule.public("meta", documentation = "Information about this crate.")
val Model = RustModule.public("model", documentation = "Data structures used by operation inputs/outputs. Documentation on these types is copied from the model.")
val Input = RustModule.public("input", documentation = "Input structures for operations. Documentation on these types is copied from the model.")
val Output = RustModule.public("output", documentation = "Output structures for operations. Documentation on these types is copied from the model.")
val Types = RustModule.public("types", documentation = "Data primitives referenced by other data types.")
val Primitives = RustModule.public("primitives", documentation = "Data primitives referenced by other data types.")

/** crate::types */
val types = Types.self
object Types {
/** crate::types */
val self = RustModule.public("types", documentation = "Data primitives referenced by other data types.")

/** crate::types::error */
val Error = RustModule.public("error", parent = self, documentation = "All error types that operations can return. Documentation on these types is copied from the model.")
}

// TODO(CrateReorganization): Remove this module when cleaning up `enableNewCrateOrganizationScheme`
val Model = RustModule.public("model", documentation = "Data structures used by operation inputs/outputs. Documentation on these types is copied from the model.")
}

object ClientModuleProvider : ModuleProvider {
override fun moduleForShape(context: ModuleProviderContext, shape: Shape): RustModule.LeafModule = when (shape) {
is OperationShape -> perOperationModule(context, shape)
is StructureShape -> when {
shape.hasTrait<ErrorTrait>() -> ClientRustModule.Error
shape.hasTrait<ErrorTrait>() -> ClientRustModule.Types.Error
shape.hasTrait<SyntheticInputTrait>() -> perOperationModule(context, shape)
shape.hasTrait<SyntheticOutputTrait>() -> perOperationModule(context, shape)
else -> ClientRustModule.Model
else -> ClientRustModule.types
}

else -> ClientRustModule.Model
else -> ClientRustModule.types
}

override fun moduleForOperationError(
Expand Down Expand Up @@ -173,3 +184,9 @@ fun ClientCodegenContext.featureGatedPaginatorModule(symbolProvider: RustSymbolP
)
else -> RustModule.public("paginator", "Paginators for the service")
}

// TODO(CrateReorganization): Remove when cleaning up `enableNewCrateOrganizationScheme`
fun ClientCodegenContext.featureGatedPrimitivesModule() = when (settings.codegenConfig.enableNewCrateOrganizationScheme) {
true -> ClientRustModule.Primitives
else -> ClientRustModule.types
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customizations.HttpVers
import software.amazon.smithy.rust.codegen.client.smithy.customizations.IdempotencyTokenGenerator
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyConfigCustomization
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyReExportCustomization
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedPrimitivesModule
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedMetaModule
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
import software.amazon.smithy.rust.codegen.core.rustlang.Feature
Expand Down Expand Up @@ -68,7 +69,7 @@ class RequiredCustomizations : ClientCodegenDecorator {
// Re-export resiliency types
ResiliencyReExportCustomization(codegenContext.runtimeConfig).extras(rustCrate)

rustCrate.withModule(ClientRustModule.Types) {
rustCrate.withModule(codegenContext.featureGatedPrimitivesModule()) {
pubUseSmithyPrimitives(codegenContext, codegenContext.model)(this)
if (!codegenContext.settings.codegenConfig.enableNewCrateOrganizationScheme) {
pubUseSmithyErrorTypes(codegenContext)(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
package software.amazon.smithy.rust.codegen.client.smithy.generators

import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.featureGatedPrimitivesModule
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
Expand All @@ -15,7 +16,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
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.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.generators.EnumGenerator
import software.amazon.smithy.rust.codegen.core.smithy.generators.EnumGeneratorContext
Expand Down Expand Up @@ -87,7 +87,7 @@ data class InfallibleEnumType(
}

private fun unknownVariantValue(context: EnumGeneratorContext): RuntimeType {
return RuntimeType.forInlineFun(UnknownVariantValue, ClientRustModule.Types) {
return RuntimeType.forInlineFun(UnknownVariantValue, unknownVariantModule) {
docs(
"""
Opaque struct used as inner data for the `Unknown` variant defined in enums in
Expand Down Expand Up @@ -167,5 +167,10 @@ data class InfallibleEnumType(
}
}

class ClientEnumGenerator(codegenContext: CodegenContext, shape: StringShape) :
EnumGenerator(codegenContext.model, codegenContext.symbolProvider, shape, InfallibleEnumType(ClientRustModule.Types))
class ClientEnumGenerator(codegenContext: ClientCodegenContext, shape: StringShape) :
EnumGenerator(
codegenContext.model,
codegenContext.symbolProvider,
shape,
InfallibleEnumType(codegenContext.featureGatedPrimitivesModule()),
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import software.amazon.smithy.model.node.ObjectNode
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenConfig
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.ClientRustSettings
import software.amazon.smithy.rust.codegen.client.smithy.OldModuleSchemeClientModuleProvider
import software.amazon.smithy.rust.codegen.client.smithy.RustClientCodegenPlugin
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.CoreRustSettings
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.customize.CombinedClientCodegenDecorator
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.RustSymbolProviderConfig
import software.amazon.smithy.rust.codegen.core.testutil.TestRuntimeConfig
import software.amazon.smithy.rust.codegen.core.testutil.testRustSettings
import software.amazon.smithy.rust.codegen.core.testutil.TestWriterDelegator

fun clientTestRustSettings(
fun testClientRustSettings(
service: ShapeId = ShapeId.from("notrelevant#notrelevant"),
moduleName: String = "test-module",
moduleVersion: String = "0.0.1",
Expand All @@ -49,7 +49,7 @@ fun clientTestRustSettings(
customizationConfig,
)

val ClientTestRustSymbolProviderConfig = RustSymbolProviderConfig(
val TestClientRustSymbolProviderConfig = RustSymbolProviderConfig(
runtimeConfig = TestRuntimeConfig,
renameExceptions = true,
nullabilityCheckMode = NullableIndex.CheckMode.CLIENT_ZERO_VALUE_V1,
Expand All @@ -58,24 +58,32 @@ val ClientTestRustSymbolProviderConfig = RustSymbolProviderConfig(

fun testSymbolProvider(model: Model, serviceShape: ServiceShape? = null): RustSymbolProvider =
RustClientCodegenPlugin.baseSymbolProvider(
clientTestRustSettings(),
testClientRustSettings(),
model,
serviceShape ?: ServiceShape.builder().version("test").id("test#Service").build(),
ClientTestRustSymbolProviderConfig,
TestClientRustSymbolProviderConfig,
)

fun testCodegenContext(
fun testClientCodegenContext(
model: Model,
symbolProvider: RustSymbolProvider? = null,
serviceShape: ServiceShape? = null,
settings: CoreRustSettings = testRustSettings(),
codegenTarget: CodegenTarget = CodegenTarget.CLIENT,
): CodegenContext = CodegenContext(
settings: ClientRustSettings = testClientRustSettings(),
rootDecorator: ClientCodegenDecorator? = null,
): ClientCodegenContext = ClientCodegenContext(
model,
testSymbolProvider(model),
symbolProvider ?: testSymbolProvider(model),
serviceShape
?: model.serviceShapes.firstOrNull()
?: ServiceShape.builder().version("test").id("test#Service").build(),
ShapeId.from("test#Protocol"),
settings,
codegenTarget,
rootDecorator ?: CombinedClientCodegenDecorator(emptyList()),
)

fun TestWriterDelegator.clientRustSettings() =
testClientRustSettings(
service = ShapeId.from("fake#Fake"),
moduleName = "test_${baseDir.toFile().nameWithoutExtension}",
codegenConfig = codegenConfig as ClientCodegenConfig,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
package software.amazon.smithy.rust.codegen.client.customizations

import org.junit.jupiter.api.Test
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenConfig
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyConfigCustomization
import software.amazon.smithy.rust.codegen.client.smithy.customizations.ResiliencyReExportCustomization
import software.amazon.smithy.rust.codegen.client.testutil.clientRustSettings
import software.amazon.smithy.rust.codegen.client.testutil.stubConfigProject
import software.amazon.smithy.rust.codegen.client.testutil.testCodegenContext
import software.amazon.smithy.rust.codegen.client.testutil.testClientCodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.transformers.OperationNormalizer
import software.amazon.smithy.rust.codegen.core.smithy.transformers.RecursiveShapeBoxer
import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest
import software.amazon.smithy.rust.codegen.core.testutil.rustSettings

internal class ResiliencyConfigCustomizationTest {
private val baseModel = """
Expand All @@ -37,8 +38,8 @@ internal class ResiliencyConfigCustomizationTest {
@Test
fun `generates a valid config`() {
val model = RecursiveShapeBoxer().transform(OperationNormalizer.transform(baseModel))
val project = TestWorkspace.testProject()
val codegenContext = testCodegenContext(model, settings = project.rustSettings())
val project = TestWorkspace.testProject(model, ClientCodegenConfig())
val codegenContext = testClientCodegenContext(model, settings = project.clientRustSettings())

stubConfigProject(ResiliencyConfigCustomization(codegenContext), project)
ResiliencyReExportCustomization(codegenContext.runtimeConfig).extras(project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package software.amazon.smithy.rust.codegen.client.endpoint

import org.junit.jupiter.api.Test
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.ClientContextConfigCustomization
import software.amazon.smithy.rust.codegen.client.testutil.testCodegenContext
import software.amazon.smithy.rust.codegen.client.testutil.testClientCodegenContext
import software.amazon.smithy.rust.codegen.client.testutil.validateConfigCustomizations
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
Expand Down Expand Up @@ -52,6 +52,6 @@ class ClientContextConfigCustomizationTest {
""",
)
}
validateConfigCustomizations(ClientContextConfigCustomization(testCodegenContext(model)), project)
validateConfigCustomizations(ClientContextConfigCustomization(testClientCodegenContext(model)), project)
}
}
Loading

0 comments on commit 3221246

Please sign in to comment.