Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add codegen helper for defining operation-specific CustomizableOperation methods. #3918

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegen
import software.amazon.smithy.rust.codegen.client.smithy.generators.ClientEnumGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.client.CustomizableOperationImplGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.error.ErrorGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.error.OperationErrorGenerator
import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ClientProtocolTestGenerator
Expand Down Expand Up @@ -311,10 +312,10 @@ class ClientCodegenVisitor(
* Generate operations
*/
override fun operationShape(operationShape: OperationShape) {
rustCrate.useShapeWriter(operationShape) operationWriter@{
rustCrate.useShapeWriter(operationShape) {
// Render the operation shape
operationGenerator.renderOperation(
this@operationWriter,
this,
operationShape,
codegenDecorator,
)
Expand All @@ -327,16 +328,24 @@ class ClientCodegenVisitor(
protocolGeneratorFactory.support(),
operationShape,
),
).render(this@operationWriter)
).render(this)
}

rustCrate.withModule(symbolProvider.moduleForOperationError(operationShape)) {
OperationErrorGenerator(
model,
symbolProvider,
operationShape,
codegenDecorator.errorCustomizations(codegenContext, emptyList()),
).render(this)
}
rustCrate.withModule(symbolProvider.moduleForOperationError(operationShape)) {
OperationErrorGenerator(
model,
symbolProvider,
operationShape,
codegenDecorator.errorCustomizations(codegenContext, emptyList()),
).render(this)
}

rustCrate.withModule(ClientRustModule.Client.customize) {
CustomizableOperationImplGenerator(
codegenContext,
operationShape,
codegenDecorator.operationCustomizations(codegenContext, operationShape, emptyList()),
).render(this)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ sealed class OperationSection(name: String) : Section(name) {
writer.rustTemplate(".with_retry_classifier(#{classifier})", "classifier" to classifier)
}
}

data class CustomizableOperationImpl(
override val customizations: List<OperationCustomization>,
val operationShape: OperationShape,
) : OperationSection("CustomizableOperationImpl")
}

abstract class OperationCustomization : NamedCustomization<OperationSection>()
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.rust.codegen.client.smithy.generators.client

import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationSection
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.smithy.customize.allCustomizationsAreEmpty
import software.amazon.smithy.rust.codegen.core.smithy.customize.writeCustomizations

class CustomizableOperationImplGenerator(
private val codegenContext: ClientCodegenContext,
private val operation: OperationShape,
private val customizations: List<OperationCustomization>,
) {
fun render(writer: RustWriter) {
val section = OperationSection.CustomizableOperationImpl(customizations, operation)
// When no customizations are set or there is nothing to write, return early.
if (customizations.isEmpty() || allCustomizationsAreEmpty(customizations, section)) {
return
}

writer.rust("impl<E, B> CustomizableOperation<#T, E, B> {", codegenContext.symbolProvider.toSymbol(operation))
writer.writeCustomizations(customizations, section)
writer.rust("}")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ fun <T : Section> RustWriter.writeCustomizationsOrElse(
orElse(this)
}
}

fun <T : Section> allCustomizationsAreEmpty(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary? If customizations is empty then how would writeCustomizations end up with a "dirty" writer?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great question!

How can you tell if the following function will write when called through RustWriter.writeCustomizations?

    override fun operationCustomizations(
        codegenContext: ClientCodegenContext,
        operation: OperationShape,
        baseCustomizations: List<OperationCustomization>,
    ): List<OperationCustomization> {
        return baseCustomizations +
            object : OperationCustomization() {
                override fun section(section: OperationSection): Writable {
                    return writable {
                        when (section) {
                            is OperationSection.CustomizableOperationImpl -> {
                                    rust("// Writing occurred!")
                                }
                            }

                            else -> {}
                        }
                    }
                }
            }
    }

It just returns a list of customizations with method implementations. You can't know anything about the implementations, including what sections they actually care about. There's no way to know if this will write anything without actually calling it and trying to write something.

So why is it important to know if something was written? It's important in cases where you want to avoid empty wrappers. Imagine that you want to generate a block of code with functions for converting a type. If you always have n+1 customizations, then it's always OK to write the enclosing block. But if you only have n customizations then you'll write an empty block and clippy will be mad at you.

Enter allCustomizationsAreEmpty. With this function, we can see if anything would actually be written, giving us the power to avoid writing an enclosing block when it would be empty.

Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, one of my concerns was triggering unrelated/additional side effects (e.g. imports that trigger dependencies being added, etc). We saw this in Kotlin though for a similar but slightly different reason.

Would be nice if we had a cleaner way to do this but I suppose it's fine for now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe adding source code comment with an example Rust snippet that this function is trying to solve is useful?

customizations: List<NamedCustomization<T>>,
section: T,
): Boolean {
val test = RustWriter.root()
test.writeCustomizations(customizations, section)
// If they're not dirty, then they're empty.
return !test.dirty()
}
Loading