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

Upgrade Smithy to 1.49 #3662

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -6,10 +6,13 @@
package software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize

import software.amazon.smithy.aws.traits.protocols.Ec2QueryNameTrait
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.XmlNameTrait
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
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.util.getTrait
Expand All @@ -32,4 +35,13 @@ class Ec2QuerySerializerGenerator(codegenContext: CodegenContext) : QuerySeriali
override fun serverErrorSerializer(shape: ShapeId): RuntimeType {
TODO("Not yet implemented")
}

override fun RustWriter.serializeCollection(
memberContext: MemberContext,
context: Context<CollectionShape>,
) {
rustBlock("if !${context.valueExpression.asRef()}.is_empty()") {
super.serializeCollectionInner(memberContext, context, this)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -282,30 +282,38 @@ abstract class QuerySerializerGenerator(private val codegenContext: CodegenConte
}
}

private fun RustWriter.serializeCollection(
protected open fun RustWriter.serializeCollection(
memberContext: MemberContext,
context: Context<CollectionShape>,
) {
serializeCollectionInner(memberContext, context, this)
}

protected fun serializeCollectionInner(
memberContext: MemberContext,
context: Context<CollectionShape>,
writer: RustWriter,
) {
val flat = memberContext.shape.isFlattened()
val memberOverride =
when (val override = context.shape.member.getTrait<XmlNameTrait>()?.value) {
null -> "None"
else -> "Some(${override.dq()})"
}
val itemName = safeName("item")
safeName("list").also { listName ->
rust("let mut $listName = ${context.writerExpression}.start_list($flat, $memberOverride);")
rustBlock("for $itemName in ${context.valueExpression.asRef()}") {
val itemName = writer.safeName("item")
ysaito1001 marked this conversation as resolved.
Show resolved Hide resolved
writer.safeName("list").also { listName ->
writer.rust("let mut $listName = ${context.writerExpression}.start_list($flat, $memberOverride);")
writer.rustBlock("for $itemName in ${context.valueExpression.asRef()}") {
val entryName = safeName("entry")
Attribute.AllowUnusedMut.render(this)
rust("let mut $entryName = $listName.entry();")
writer.rust("let mut $entryName = $listName.entry();")
val targetShape = model.expectShape(context.shape.member.target)
serializeMemberValue(
writer.serializeMemberValue(
MemberContext(entryName, ValueExpression.Reference(itemName), context.shape.member),
targetShape,
)
}
rust("$listName.finish();")
writer.rust("$listName.finish();")
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks to be doing the same thing. Why update it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is working around Kotlin's limitation that an extension method cannot be overridden. Ec2QuerySerializerGenerator, a subclass of QuerySerializerGenerator, needs to override RustWriter.serializeCollection so that it should only call it if an input list is NOT empty (otherwise &ListArg= would be rendered which ec2 query protocol doesn't like in Smithy 1.49). However, we currently couldn't do this in Kotlin if it were not for serializeCollectionInner:

override fun RustWriter.serializeCollection(
        memberContext: MemberContext,
        context: Context<CollectionShape>,
    ) {
        rustBlock("if !${context.valueExpression.asRef()}.is_empty()") {
            super.serializeCollection(memberContext, context) // cannot call super class's extension method like this
        }

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ kotlin.code.style=official

# codegen
smithyGradlePluginVersion=0.9.0
smithyVersion=1.47.0
smithyVersion=1.49.0
allowLocalDeps=false

# kotlin
Expand Down