Skip to content

Commit

Permalink
CR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoh committed Dec 1, 2022
1 parent 79b6de1 commit f6bc5c2
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.node.ObjectNode
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.CustomRuntimeFunction
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.CustomRuntimeFunction
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rulesgen.awsStandardLib
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
Expand All @@ -32,12 +32,12 @@ class AwsEndpointsStdLib() : RustCodegenDecorator<ClientProtocolGenerator, Clien
return clazz.isAssignableFrom(ClientCodegenContext::class.java)
}

private fun partitionsDotjson(sdkSettings: SdkSettings): ObjectNode {
private fun partitionMetadata(sdkSettings: SdkSettings): ObjectNode {
if (partitionsCache == null) {
val partitionsJson = when (val path = sdkSettings.partitionsDotJson) {
val partitionsJson = when (val path = sdkSettings.partitionsConfigPath) {
null -> (
javaClass.getResource("/default-partitions.json")
?: throw IllegalStateException("Failed to find default-default-partitions.json in the JAR")
?: throw IllegalStateException("Failed to find default-partitions.json in the JAR")
).readText()

else -> path.readText()
Expand All @@ -52,7 +52,7 @@ class AwsEndpointsStdLib() : RustCodegenDecorator<ClientProtocolGenerator, Clien
object : EndpointCustomization {
override fun customRuntimeFunctions(codegenContext: ClientCodegenContext): List<CustomRuntimeFunction> {
val sdkSettings = SdkSettings.from(codegenContext.settings)
return awsStandardLib(codegenContext.runtimeConfig, partitionsDotjson(sdkSettings))
return awsStandardLib(codegenContext.runtimeConfig, partitionMetadata(sdkSettings))
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ 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.CustomRuntimeFunction
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.EndpointCustomization
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.CustomRuntimeFunction
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SdkSettings private constructor(private val awsSdk: ObjectNode?) {
awsSdk?.getStringMember("endpointsConfigPath")?.orNull()?.value?.let { Paths.get(it) }

/** Path to the `default-partitions.json` configuration */
val partitionsDotJson: Path?
val partitionsConfigPath: Path?
get() =
awsSdk?.getStringMember("partitionsConfigPath")?.orNull()?.value?.let { Paths.get(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.generators.config.Servi
import software.amazon.smithy.rust.codegen.core.rustlang.RustReservedWords
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.docs
import software.amazon.smithy.rust.codegen.core.rustlang.docsOrFallback
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
Expand Down Expand Up @@ -75,12 +76,22 @@ internal class ClientContextDecorator(ctx: CodegenContext) : ConfigCustomization
}
ServiceConfig.BuilderImpl -> writable {
contextParams.forEach { param ->
param.docs?.also { docs(it) }
docsOrFallback(param.docs)
rust(
"""
pub fn ${param.name}(mut self, ${param.name}: impl Into<#T>) -> Self {
self.${param.name} = Some(${param.name}.into());
self
}""",
param.type,
)

docsOrFallback(param.docs)
rust(
"""
pub fn set_${param.name}(mut self, ${param.name}: Option<#T>) -> Self {
self.${param.name} = ${param.name};
self
}
""",
param.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ internal class EndpointConfigCustomization(
/// impl endpoint::ResolveEndpoint<EndpointParams> for PrefixResolver {
/// fn resolve_endpoint(&self, params: &EndpointParams) -> endpoint::Result {
/// self.base_resolver
/// .resolve_endpoint(params)
/// .resolve_endpoint(params)
/// .map(|ep|{
/// let url = ep.url().to_string();
/// ep.into_builder().url(format!("{}.{}", &self.prefix, url)).build()
/// let url = ep.url().to_string();
/// ep.into_builder().url(format!("{}.{}", &self.prefix, url)).build()
/// })
/// }
/// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import software.amazon.smithy.model.knowledge.KnowledgeIndex
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.rulesengine.language.EndpointRuleSet
import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait
import software.amazon.smithy.rulesengine.traits.EndpointTestsTrait
import software.amazon.smithy.rust.codegen.core.util.getTrait
import java.util.concurrent.ConcurrentHashMap

Expand All @@ -24,6 +25,8 @@ internal class EndpointRulesetIndex : KnowledgeIndex {
serviceShape,
) { serviceShape.getTrait<EndpointRuleSetTrait>()?.ruleSet?.let { EndpointRuleSet.fromNode(it) }?.also { it.typecheck() } }

fun endpointTests(serviceShape: ServiceShape) = serviceShape.getTrait<EndpointTestsTrait>()?.testCases ?: emptyList()

companion object {
fun of(model: Model): EndpointRulesetIndex {
return model.getKnowledge(EndpointRulesetIndex::class.java) { EndpointRulesetIndex() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,51 @@ package software.amazon.smithy.rust.codegen.client.smithy.endpoint
import software.amazon.smithy.rulesengine.language.EndpointRuleSet
import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameter
import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameters
import software.amazon.smithy.rulesengine.traits.EndpointTestsTrait
import software.amazon.smithy.rulesengine.traits.EndpointTestCase
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointParamsGenerator
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointResolverGenerator
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointTestGenerator
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.util.getTrait

/**
* Entrypoint for Endpoints 2.0 Code generation
*
* This exposes [RuntimeType]s for the individual components of endpoints 2.0
*/
class EndpointTypesGenerator(codegenContext: ClientCodegenContext, private val rules: EndpointRuleSet) {
class EndpointTypesGenerator(
codegenContext: ClientCodegenContext,
private val rules: EndpointRuleSet,
private val tests: List<EndpointTestCase>,
) {
val params: Parameters = rules.parameters
private val runtimeConfig = codegenContext.runtimeConfig
private val customizations = codegenContext.rootDecorator.endpointCustomizations(codegenContext)
private val stdlib = customizations
.flatMap { it.customRuntimeFunctions(codegenContext) }
private val tests = codegenContext.serviceShape.getTrait<EndpointTestsTrait>()?.testCases ?: emptyList()

companion object {
fun fromContext(codegenContext: ClientCodegenContext): EndpointTypesGenerator? {
val index = EndpointRulesetIndex.of(codegenContext.model)
val rules = index.endpointRulesForService(codegenContext.serviceShape) ?: return null
return EndpointTypesGenerator(codegenContext, rules)
val rulesOrNull = index.endpointRulesForService(codegenContext.serviceShape)
return rulesOrNull?.let { rules ->
EndpointTypesGenerator(codegenContext, rules, index.endpointTests(codegenContext.serviceShape))
}
}
}

fun paramsStruct(): RuntimeType = EndpointParamsGenerator(params).paramsStruct()
fun defaultResolver(): RuntimeType = EndpointResolverGenerator(stdlib, runtimeConfig).defaultEndpointResolver(rules)
fun testGenerator(): Writable = EndpointTestGenerator(tests, paramsStruct(), defaultResolver(), params, runtimeConfig).generate()
fun testGenerator(): Writable =
EndpointTestGenerator(tests, paramsStruct(), defaultResolver(), params, runtimeConfig).generate()

/**
* Load the builtIn value for [parameter] from the endpoint customizations. If the built-in comes from service config,
* [config] refers to `&crate::config::Config`
*
* Exactly one endpoint customization must provide the value for this builtIn or null is returned.
*/
fun builtInFor(parameter: Parameter, config: String): Writable? {
val defaultProviders = customizations
.mapNotNull { it.builtInDefaultValue(parameter, config) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameters
import software.amazon.smithy.rulesengine.traits.ContextIndex
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.generators.CustomRuntimeFunction
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointParamsGenerator
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointTests
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointsModule
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.rulesgen.SmithyEndpointsStdLib
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolGenerator
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
Expand Down Expand Up @@ -45,10 +47,10 @@ interface EndpointCustomization {
/**
* Decorator that injects endpoints 2.0 resolvers throughout the entire client.
*
* This _does not_ inject any standard library functions. For the standard library, ensure that
* [NativeSmithyEndpointsStdLib] is included as a decorator on the classpath.
* This decorator installs the core standard library functions. It DOES NOT inject the AWS specific functions which
* must be injected separately.
*
* If the service _does not_ provide custom endpoint rules, this decorator is a no-op.
* If the service DOES NOT provide custom endpoint rules, this decorator is a no-op.
*/
class EndpointsDecorator : RustCodegenDecorator<ClientProtocolGenerator, ClientCodegenContext> {
override val name: String = "Endpoints"
Expand All @@ -73,6 +75,16 @@ class EndpointsDecorator : RustCodegenDecorator<ClientProtocolGenerator, ClientC
) + baseCustomizations
}

override fun endpointCustomizations(codegenContext: ClientCodegenContext): List<EndpointCustomization> {
return listOf(
object : EndpointCustomization {
override fun customRuntimeFunctions(codegenContext: ClientCodegenContext): List<CustomRuntimeFunction> {
return SmithyEndpointsStdLib
}
},
)
}

override fun configCustomizations(
codegenContext: ClientCodegenContext,
baseCustomizations: List<ConfigCustomization>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import software.amazon.smithy.rulesengine.language.syntax.parameters.Parameter
import software.amazon.smithy.rulesengine.language.syntax.parameters.ParameterType
import software.amazon.smithy.rulesengine.traits.ContextParamTrait
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointsStdLib
import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.FunctionRegistry
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.InlineDependency
import software.amazon.smithy.rust.codegen.core.rustlang.RustDependency
Expand Down
Loading

0 comments on commit f6bc5c2

Please sign in to comment.