Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ysaito1001 committed May 23, 2024
1 parent fcb87f1 commit d45cfd6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
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")
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();")
}
}

Expand Down

0 comments on commit d45cfd6

Please sign in to comment.