Unverified Commit 4809a5b4 authored by Zelda Hessler's avatar Zelda Hessler Committed by GitHub
Browse files

Remove the need for operation type aliasing in codegen (#1710)



* remove: need for operation type aliasing
rename: FluentClientGenerics.sendBounds params to be more accurate
update: FlexibleClientGenerics.sendBounds impl for readability
update: type of FluentClientGenerator input param `retryPolicyType` to be `Any` with a default of `RustType.Unit`
update: PaginatorGenerator to take retryPolicy as an input
chore: fix some spelling and grammar issues
remove: redundant `nextTokenEmpty` function from PaginatorGenerator

* Update CHANGELOG.next.toml

Co-authored-by: default avatarJohn DiSanti <jdisanti@amazon.com>

* add: `writable` property to RustType that returns the type as a Writable
add: test for RustType writable
add: `writable` property to RuntimeType that returns the type as a Writable
update: FluentClientGenerator to take a writable for retry

* format: run formatter

Co-authored-by: default avatarJohn DiSanti <jdisanti@amazon.com>
parent 96e9f61f
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -169,6 +169,18 @@ references = ["smithy-rs#1647", "smithy-rs#1112"]
meta = { "breaking" = false, "tada" = true, "bug" = false, "target" = "client"}
author = "Velfi"

[[smithy-rs]]
message = "Removed the need to generate operation output and retry aliases in codegen."
references = ["smithy-rs#976", "smithy-rs#1710"]
meta = { "breaking" = true, "tada" = false, "bug" = false, "target" = "client" }
author = "Velfi"

[[smithy-rs]]
message = "Added `writable` property to `RustType` and `RuntimeType` that returns them in `Writable` form"
references = ["smithy-rs#1710"]
meta = { "breaking" = false, "tada" = false, "bug" = false, "target" = "all" }
author = "Velfi"

[[smithy-rs]]
message = "Smithy IDL v2 mixins are now supported"
references = ["smithy-rs#1680"]
+2 −2
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ private class AwsClientGenerics(private val types: Types) : FluentClientGenerics
    override val bounds = writable { }

    /** Bounds for generated `send()` functions */
    override fun sendBounds(input: Symbol, output: Symbol, error: RuntimeType): Writable = writable { }
    override fun sendBounds(operation: Symbol, operationOutput: Symbol, operationError: RuntimeType, retryPolicy: Writable): Writable = writable { }

    override fun toGenericsGenerator(): GenericsGenerator {
        return GenericsGenerator()
@@ -98,7 +98,7 @@ class AwsFluentClientDecorator : RustCodegenDecorator<ClientCodegenContext> {
                AwsPresignedFluentBuilderMethod(runtimeConfig),
                AwsFluentClientDocs(codegenContext),
            ),
            retryPolicyType = runtimeConfig.awsHttp().asType().member("retry::AwsErrorRetryPolicy"),
            retryPolicy = runtimeConfig.awsHttp().asType().member("retry::AwsErrorRetryPolicy").writable,
        ).render(rustCrate)
        rustCrate.withModule(FluentClientGenerator.customizableOperationModule) { writer ->
            renderCustomizableOperationSendMethod(runtimeConfig, generics, writer)
+47 −1
Original line number Diff line number Diff line
@@ -42,6 +42,46 @@ sealed class RustType {

    open val namespace: kotlin.String? = null

    /**
     * Get a writable for this `RustType`
     *
     * ```kotlin
     * // Declare a RustType
     * val t = RustType.Unit.writable
     * // Then, invoke the writable directly
     * t.invoke(writer)
     * // OR template it out
     * writer.rustInlineTemplate("#{t:W}", "t" to t)
     * ```
     *
     * When formatted, the converted type will appear as such:
     *
     * | Type                                               | Formatted                                                           |
     * | -------------------------------------------------- | ------------------------------------------------------------------- |
     * | RustType.Unit                                      | ()                                                                  |
     * | RustType.Bool                                      | bool                                                                |
     * | RustType.Float(32)                                 | f32                                                                 |
     * | RustType.Float(64)                                 | f64                                                                 |
     * | RustType.Integer(8)                                | i8                                                                  |
     * | RustType.Integer(16)                               | i16                                                                 |
     * | RustType.Integer(32)                               | i32                                                                 |
     * | RustType.Integer(64)                               | i64                                                                 |
     * | RustType.String                                    | std::string::String                                                 |
     * | RustType.Vec(RustType.String)                      | std::vec::Vec<std::string::String>                                  |
     * | RustType.Slice(RustType.String)                    | [std::string::String]                                               |
     * | RustType.HashMap(RustType.String, RustType.String) | std::collections::HashMap<std::string::String, std::string::String> |
     * | RustType.HashSet(RustType.String)                  | std::vec::Vec<std::string::String>                                  |
     * | RustType.Reference("&", RustType.String)           | &std::string::String                                                |
     * | RustType.Reference("&mut", RustType.String)        | &mut std::string::String                                            |
     * | RustType.Reference("&'static", RustType.String)    | &'static std::string::String                                        |
     * | RustType.Option(RustType.String)                   | std::option::Option<std::string::String>                            |
     * | RustType.Box(RustType.String)                      | std::boxed::Box<std::string::String>                                |
     * | RustType.Opaque("SoCool", "zelda_is")              | zelda_is::SoCool                                                    |
     * | RustType.Opaque("SoCool")                          | SoCool                                                              |
     * | RustType.Dyn(RustType.Opaque("Foo", "foo"))        | dyn foo::Foo                                                        |
     */
    val writable = writable { rustInlineTemplate("#{this}", "this" to this@RustType) }

    object Unit : RustType() {
        override val name: kotlin.String = "()"
    }
@@ -186,7 +226,13 @@ fun RustType.render(fullyQualified: Boolean = true): String {
        is RustType.Slice -> "[${this.member.render(fullyQualified)}]"
        is RustType.HashMap -> "${this.name}<${this.key.render(fullyQualified)}, ${this.member.render(fullyQualified)}>"
        is RustType.HashSet -> "${this.name}<${this.member.render(fullyQualified)}>"
        is RustType.Reference -> "&${this.lifetime?.let { "'$it" } ?: ""} ${this.member.render(fullyQualified)}"
        is RustType.Reference -> {
            if (this.lifetime == "&") {
                "&${this.member.render(fullyQualified)}"
            } else {
                "&${this.lifetime?.let { "'$it" } ?: ""} ${this.member.render(fullyQualified)}"
            }
        }
        is RustType.Option -> "${this.name}<${this.member.render(fullyQualified)}>"
        is RustType.Box -> "${this.name}<${this.member.render(fullyQualified)}>"
        is RustType.Dyn -> "${this.name} ${this.member.render(fullyQualified)}"
+1 −2
Original line number Diff line number Diff line
@@ -544,9 +544,8 @@ class RustWriter private constructor(
     * Formatter to enable formatting any [writable] with the #W formatter.
     */
    inner class RustWriteableInjector : BiFunction<Any, String, String> {
        @Suppress("UNCHECKED_CAST")
        override fun apply(t: Any, u: String): String {
            val func = t as RustWriter.() -> Unit
            val func = t as? RustWriter.() -> Unit ?: throw CodegenException("RustWriteableInjector.apply choked on non-function t ($t)")
            val innerWriter = RustWriter(filename, namespace, printWarning = false)
            func(innerWriter)
            innerWriter.dependencies.forEach { addDependency(it) }
+11 −3
Original line number Diff line number Diff line
@@ -5,10 +5,10 @@

package software.amazon.smithy.rust.codegen.rustlang

import software.amazon.smithy.codegen.core.CodegenException
import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.rust.codegen.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.smithy.generators.GenericsGenerator
import software.amazon.smithy.rust.codegen.util.PANIC

typealias Writable = RustWriter.() -> Unit

@@ -56,13 +56,21 @@ fun rustTypeParameters(
        val iterator: Iterator<Any> = typeParameters.iterator()
        while (iterator.hasNext()) {
            when (val typeParameter = iterator.next()) {
                is Symbol, is RustType.Unit, is RuntimeType -> rustInlineTemplate("#{it}", "it" to typeParameter)
                is Symbol, is RuntimeType, is RustType -> rustInlineTemplate("#{it}", "it" to typeParameter)
                is String -> rustInlineTemplate(typeParameter)
                is GenericsGenerator -> rustInlineTemplate(
                    "#{gg:W}",
                    "gg" to typeParameter.declaration(withAngleBrackets = false),
                )
                else -> PANIC("Unhandled type '$typeParameter' encountered by rustTypeParameters writer")
                else -> {
                    // Check if it's a writer. If it is, invoke it; Else, throw a codegen error.
                    val func = typeParameter as? RustWriter.() -> Unit
                    if (func != null) {
                        func.invoke(this)
                    } else {
                        throw CodegenException("Unhandled type '$typeParameter' encountered by rustTypeParameters writer")
                    }
                }
            }

            if (iterator.hasNext()) {
Loading