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

Implement new JsonSerializerGenerator without document/operation support #416

Merged
merged 3 commits into from
May 25, 2021
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 @@ -187,6 +187,8 @@ data class CargoDependency(
"protocol-test-helpers", Local(runtimeConfig.relativePath), scope = DependencyScope.Dev
)

fun smithyJson(runtimeConfig: RuntimeConfig): CargoDependency =
CargoDependency("${runtimeConfig.cratePrefix}-json", Local(runtimeConfig.relativePath))
fun smithyXml(runtimeConfig: RuntimeConfig): CargoDependency =
CargoDependency("${runtimeConfig.cratePrefix}-xml", Local(runtimeConfig.relativePath))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,15 @@ fun RustType.render(fullyQualified: Boolean = true): String {
* Option<Instant>.contains(Instant) would return true.
* Option<Instant>.contains(Blob) would return false.
*/
fun <T : RustType> RustType.contains(t: T): Boolean {
if (t == this) {
return true
}

return when (this) {
is RustType.Container -> this.member.contains(t)
else -> false
}
fun <T : RustType> RustType.contains(t: T): Boolean = when (this) {
t -> true
is RustType.Container -> this.member.contains(t)
else -> false
}

inline fun <reified T : RustType.Container> RustType.stripOuter(): RustType {
return when (this) {
is T -> this.member
else -> this
}
inline fun <reified T : RustType.Container> RustType.stripOuter(): RustType = when (this) {
is T -> this.member
else -> this
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ fun <T : CodeWriter> T.rust(
this.write(contents, *args)
}

/**
* Convenience wrapper that tells Intellij that the contents of this block are Rust
*/
fun <T : CodeWriter> T.rustInline(
@Language("Rust", prefix = "macro_rules! foo { () => {{ ", suffix = "}}}") contents: String,
vararg args: Any
) {
this.writeInline(contents, *args)
}

/**
* Sibling method to [rustBlock] that enables `#{variablename}` style templating
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ data class RuntimeType(val name: String?, val dependency: RustDependency?, val n
namespace = "${runtimeConfig.cratePrefix}_types::retry"
)

val Default: RuntimeType = RuntimeType("Default", dependency = null, namespace = "std::default")
val From = RuntimeType("From", dependency = null, namespace = "std::convert")
val AsRef = RuntimeType("AsRef", dependency = null, namespace = "std::convert")
val std = RuntimeType(null, dependency = null, namespace = "std")
val stdfmt = std.member("fmt")
val StdError = RuntimeType("Error", dependency = null, namespace = "std::error")

val AsRef = RuntimeType("AsRef", dependency = null, namespace = "std::convert")
val ByteSlab = RuntimeType("Vec<u8>", dependency = null, namespace = "std::vec")
val Clone = std.member("clone::Clone")
val Debug = stdfmt.member("Debug")
val Default: RuntimeType = RuntimeType("Default", dependency = null, namespace = "std::default")
val From = RuntimeType("From", dependency = null, namespace = "std::convert")
val PartialEq = std.member("cmp::PartialEq")
val Clone = std.member("clone::Clone")
val StdError = RuntimeType("Error", dependency = null, namespace = "std::error")
val String = RuntimeType("String", dependency = null, namespace = "std::string")

fun Instant(runtimeConfig: RuntimeConfig) =
RuntimeType("Instant", CargoDependency.SmithyTypes(runtimeConfig), "${runtimeConfig.cratePrefix}_types")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import software.amazon.smithy.rust.codegen.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.smithy.generators.operationBuildError
import software.amazon.smithy.rust.codegen.smithy.locatedIn
import software.amazon.smithy.rust.codegen.smithy.meta
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.JsonParserGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.JsonSerializerGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.SerdeJsonParserGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.SerdeJsonSerializerGenerator
import software.amazon.smithy.rust.codegen.smithy.rustType
import software.amazon.smithy.rust.codegen.smithy.traits.InputBodyTrait
import software.amazon.smithy.rust.codegen.smithy.traits.OutputBodyTrait
Expand Down Expand Up @@ -198,7 +198,7 @@ class BasicAwsJsonGenerator(
}

override fun RustWriter.body(self: String, operationShape: OperationShape): BodyMetadata {
val generator = JsonSerializerGenerator(protocolConfig)
val generator = SerdeJsonSerializerGenerator(protocolConfig)
val serializer = generator.operationSerializer(operationShape)
serializer?.also { sym ->
rustTemplate(
Expand All @@ -214,7 +214,7 @@ class BasicAwsJsonGenerator(
val outputShape = operationIndex.getOutput(operationShape).get()
val errorSymbol = operationShape.errorSymbol(symbolProvider)
val jsonErrors = RuntimeType.awsJsonErrors(protocolConfig.runtimeConfig)
val generator = JsonParserGenerator(protocolConfig)
val generator = SerdeJsonParserGenerator(protocolConfig)

fromResponseFun(implBlockWriter, operationShape) {
rustBlock("if #T::is_error(&response)", jsonErrors) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import software.amazon.smithy.rust.codegen.smithy.RustSymbolProvider
import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolConfig
import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolGeneratorFactory
import software.amazon.smithy.rust.codegen.smithy.generators.ProtocolSupport
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.JsonParserGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.JsonSerializerGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.SerdeJsonParserGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.SerdeJsonSerializerGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.StructuredDataParserGenerator
import software.amazon.smithy.rust.codegen.smithy.protocols.parsers.StructuredDataSerializerGenerator
import software.amazon.smithy.rust.codegen.smithy.transformers.OperationNormalizer
Expand Down Expand Up @@ -77,11 +77,11 @@ class AwsRestJsonFactory : ProtocolGeneratorFactory<HttpTraitProtocolGenerator>
class RestJson(private val protocolConfig: ProtocolConfig) : Protocol {
private val runtimeConfig = protocolConfig.runtimeConfig
override fun structuredDataParser(operationShape: OperationShape): StructuredDataParserGenerator {
return JsonParserGenerator(protocolConfig)
return SerdeJsonParserGenerator(protocolConfig)
}

override fun structuredDataSerializer(operationShape: OperationShape): StructuredDataSerializerGenerator {
return JsonSerializerGenerator(protocolConfig)
return SerdeJsonSerializerGenerator(protocolConfig)
}

override fun parseGenericError(operationShape: OperationShape): RuntimeType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class XmlNameIndex(private val model: Model) : KnowledgeIndex {
}

fun operationInputShapeName(operationShape: OperationShape): String? {
val outputShape = operationShape.inputShape(model)
val rename = outputShape.getTrait<XmlNameTrait>()?.value
return rename ?: outputShape.expectTrait<SyntheticInputTrait>().originalId?.name
val inputShape = operationShape.inputShape(model)
val rename = inputShape.getTrait<XmlNameTrait>()?.value
return rename ?: inputShape.expectTrait<SyntheticInputTrait>().originalId?.name
}

fun memberName(member: MemberShape): String {
Expand Down
Loading