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

Remove unneeded doc parser #1123

Merged
merged 5 commits into from
Jan 28, 2022
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
5 changes: 5 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@
# references = ["smithy-rs#920"]
# meta = { "breaking" = false, "tada" = false, "bug" = false }
# author = "rcoh"
[[smithy-rs]]
message = "Refactor `Document` shape parser generation"
references = ["smithy-rs#1123"]
meta = { "breaking" = false, "tada" = false, "bug" = false }
author = "rcoh"
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package software.amazon.smithy.rust.codegen.server.smithy.generators.http


import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.rust.codegen.rustlang.RustWriter
import software.amazon.smithy.rust.codegen.smithy.CodegenContext
Expand All @@ -29,14 +28,12 @@ class ServerRequestBindingGenerator(
operationShape: OperationShape,
binding: HttpBindingDescriptor,
errorT: RuntimeType,
structuredHandler: RustWriter.(String) -> Unit,
docHandler: RustWriter.(String) -> Unit
structuredHandler: RustWriter.(String) -> Unit
): RuntimeType = httpBindingGenerator.generateDeserializePayloadFn(
operationShape,
binding,
errorT,
structuredHandler,
docHandler,
HttpMessageType.REQUEST
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -561,20 +561,13 @@ private class ServerHttpProtocolImplGenerator(
return when (binding.location) {
HttpLocation.HEADER -> writable { serverRenderHeaderParser(this, binding, operationShape) }
HttpLocation.PAYLOAD -> {
val docShapeHandler: RustWriter.(String) -> Unit = { body ->
rust(
"#T($body)",
structuredDataParser.documentParser(operationShape),
)
}
val structureShapeHandler: RustWriter.(String) -> Unit = { body ->
rust("#T($body)", structuredDataParser.payloadParser(binding.member))
}
val deserializer = httpBindingGenerator.generateDeserializePayloadFn(
operationShape,
binding,
errorSymbol,
docHandler = docShapeHandler,
structuredHandler = structureShapeHandler
)
return if (binding.member.isStreaming(model)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,8 @@ class HttpBindingGenerator(
operationShape: OperationShape,
binding: HttpBindingDescriptor,
errorT: RuntimeType,
// Deserialize a single structure or union member marked as a payload
structuredHandler: RustWriter.(String) -> Unit,
// Deserialize a document type marked as a payload
docHandler: RustWriter.(String) -> Unit,
// Deserialize a single structure, union or document member marked as a payload
payloadParser: RustWriter.(String) -> Unit,
httpMessageType: HttpMessageType = HttpMessageType.RESPONSE
): RuntimeType {
check(binding.location == HttpBinding.Location.PAYLOAD)
Expand All @@ -190,8 +188,7 @@ class HttpBindingGenerator(
deserializePayloadBody(
binding,
errorT,
structuredHandler = structuredHandler,
docShapeHandler = docHandler,
structuredHandler = payloadParser,
httpMessageType
)
}
Expand Down Expand Up @@ -239,7 +236,6 @@ class HttpBindingGenerator(
binding: HttpBindingDescriptor,
errorSymbol: RuntimeType,
structuredHandler: RustWriter.(String) -> Unit,
docShapeHandler: RustWriter.(String) -> Unit,
httpMessageType: HttpMessageType = HttpMessageType.RESPONSE
) {
val member = binding.member
Expand All @@ -248,7 +244,7 @@ class HttpBindingGenerator(
// of an empty instance of the response type.
withBlock("(!body.is_empty()).then(||{", "}).transpose()") {
when (targetShape) {
is StructureShape, is UnionShape -> this.structuredHandler("body")
is StructureShape, is UnionShape, is DocumentShape -> this.structuredHandler("body")
is StringShape -> {
when (httpMessageType) {
HttpMessageType.RESPONSE -> {
Expand All @@ -274,7 +270,6 @@ class HttpBindingGenerator(
"Ok(#T::new(body))",
RuntimeType.Blob(runtimeConfig)
)
is DocumentShape -> this.docShapeHandler("body")
// `httpPayload` can be applied to set/map/list shapes.
// However, none of the AWS protocols support it.
// Smithy CLI will refuse to build the model if you apply the trait to these shapes, so this branch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ class ResponseBindingGenerator(
operationShape: OperationShape,
binding: HttpBindingDescriptor,
errorT: RuntimeType,
structuredHandler: RustWriter.(String) -> Unit,
docHandler: RustWriter.(String) -> Unit
payloadParser: RustWriter.(String) -> Unit
): RuntimeType = httpBindingGenerator.generateDeserializePayloadFn(
operationShape,
binding,
errorT,
structuredHandler,
docHandler
payloadParser
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -361,22 +361,14 @@ class HttpBoundProtocolTraitImplGenerator(
null
}
HttpLocation.PAYLOAD -> {
val docShapeHandler: RustWriter.(String) -> Unit = { body ->
rust(
"#T($body).map_err(#T::unhandled)",
structuredDataParser.documentParser(operationShape),
errorSymbol
)
}
val structureShapeHandler: RustWriter.(String) -> Unit = { body ->
val payloadParser: RustWriter.(String) -> Unit = { body ->
rust("#T($body).map_err(#T::unhandled)", structuredDataParser.payloadParser(member), errorSymbol)
}
val deserializer = httpBindingGenerator.generateDeserializePayloadFn(
operationShape,
binding,
errorSymbol,
docHandler = docShapeHandler,
structuredHandler = structureShapeHandler
payloadParser = payloadParser
)
return if (binding.member.isStreaming(model)) {
writable { rust("Some(#T(response.body_mut())?)", deserializer) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package software.amazon.smithy.rust.codegen.smithy.protocols

import software.amazon.smithy.model.shapes.DocumentShape
import software.amazon.smithy.model.shapes.ListShape
import software.amazon.smithy.model.shapes.MapShape
import software.amazon.smithy.model.shapes.MemberShape
Expand Down Expand Up @@ -66,6 +67,7 @@ private fun RustSymbolProvider.shapeFunctionName(prefix: String, shape: Shape):
is SetShape -> "set_${shape.id.toRustIdentifier()}"
is StructureShape -> "structure_$symbolNameSnakeCase"
is UnionShape -> "union_$symbolNameSnakeCase"
is DocumentShape -> "document"
else -> PANIC("SerializerFunctionNamer.name: $shape")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,23 @@ class JsonParserGenerator(

override fun payloadParser(member: MemberShape): RuntimeType {
val shape = model.expectShape(member.target)
check(shape is UnionShape || shape is StructureShape) { "payload parser should only be used on structures & unions" }
check(shape is UnionShape || shape is StructureShape || shape is DocumentShape) { "payload parser should only be used on structures & unions" }
val fnName = symbolProvider.deserializeFunctionName(shape) + "_payload"
return RuntimeType.forInlineFun(fnName, jsonDeserModule) {
it.rustBlockTemplate(
"pub fn $fnName(input: &[u8]) -> Result<#{Shape}, #{Error}>",
*codegenScope,
"Shape" to symbolProvider.toSymbol(shape)
) {
val input = if (shape is DocumentShape) {
"input"
} else {
"#{or_empty}(input)"
}

rustTemplate(
"""
let mut tokens_owned = #{json_token_iter}(#{or_empty}(input)).peekable();
let mut tokens_owned = #{json_token_iter}($input).peekable();
let tokens = &mut tokens_owned;
""",
*codegenScope
Expand Down Expand Up @@ -161,28 +167,6 @@ class JsonParserGenerator(
return structureParser(fnName, errorShape, errorShape.members().toList())
}

override fun documentParser(operationShape: OperationShape): RuntimeType {
val fnName = "parse_document"
return RuntimeType.forInlineFun(fnName, jsonDeserModule) {
it.rustBlockTemplate(
"pub fn $fnName(input: &[u8]) -> Result<#{Document}, #{Error}>",
"Document" to RuntimeType.Document(runtimeConfig),
*codegenScope,
) {
rustTemplate(
"""
let mut tokens_owned = #{json_token_iter}(input).peekable();
let tokens = &mut tokens_owned;
""",
*codegenScope
)
rustTemplate("let result = #{expect_document}(tokens);", *codegenScope)
expectEndOfTokenStream()
rust("result")
}
}
}

private fun orEmptyJson(): RuntimeType = RuntimeType.forInlineFun("or_empty_doc", jsonDeserModule) {
it.rust(
"""
Expand Down Expand Up @@ -385,8 +369,9 @@ class JsonParserGenerator(
withBlock("Ok(Some(builder.build()", "))") {
if (StructureGenerator.fallibleBuilder(shape, symbolProvider)) {
rustTemplate(
".map_err(|err| #{Error}::new(#{ErrorReason}::Custom(" +
"format!(\"{}\", err).into()), None))?",
""".map_err(|err| #{Error}::new(
#{ErrorReason}::Custom(format!({}, err).into()), None)
)?""",
*codegenScope
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ interface StructuredDataParserGenerator {
*/
fun errorParser(errorShape: StructureShape): RuntimeType?

/**
* ```rust
* fn parse_document(inp: &[u8]) -> Result<Document, Error> {
* ...
* }
* ```
*/
fun documentParser(operationShape: OperationShape): RuntimeType

/**
* Generate a parser for a server operation input structure
* ```rust
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,6 @@ class XmlBindingTraitParserGenerator(
}
}

override fun documentParser(operationShape: OperationShape): RuntimeType {
PANIC("Document shapes are not supported by rest XML")
}

override fun serverInputParser(operationShape: OperationShape): RuntimeType? {
val inputShape = operationShape.inputShape(model)
val fnName = symbolProvider.deserializeFunctionName(operationShape)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class JsonParserGeneratorTest {
::restJsonFieldName
)
val operationGenerator = parserGenerator.operationParser(model.lookup("test#Op"))
val documentGenerator = parserGenerator.documentParser(model.lookup("test#Op"))
val payloadGenerator = parserGenerator.payloadParser(model.lookup("test#OpOutput\$top"))
val errorParser = parserGenerator.errorParser(model.lookup("test#Error"))

Expand All @@ -132,7 +131,6 @@ class JsonParserGeneratorTest {
use model::Choice;

// Generate the document serializer even though it's not tested directly
// ${writer.format(documentGenerator)}
// ${writer.format(payloadGenerator)}

let json = br#"
Expand Down