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

Refactor Instantiator #1863

Merged
merged 9 commits into from
Oct 19, 2022
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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

import software.amazon.smithy.codegen.core.Symbol
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.generators.Instantiator

private fun enumFromStringFn(enumSymbol: Symbol, data: String): Writable = writable {
rust("#T::from($data)", enumSymbol)
}

fun clientInstantiator(codegenContext: CodegenContext) =
Instantiator(
codegenContext.symbolProvider,
codegenContext.model,
codegenContext.runtimeConfig,
::enumFromStringFn,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import software.amazon.smithy.protocoltests.traits.HttpRequestTestCase
import software.amazon.smithy.protocoltests.traits.HttpRequestTestsTrait
import software.amazon.smithy.protocoltests.traits.HttpResponseTestCase
import software.amazon.smithy.protocoltests.traits.HttpResponseTestsTrait
import software.amazon.smithy.rust.codegen.client.smithy.generators.clientInstantiator
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute
import software.amazon.smithy.rust.codegen.core.rustlang.CargoDependency
import software.amazon.smithy.rust.codegen.core.rustlang.RustMetadata
Expand All @@ -33,9 +34,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock
import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate
import software.amazon.smithy.rust.codegen.core.rustlang.withBlock
import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.CodegenTarget
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.generators.Instantiator
import software.amazon.smithy.rust.codegen.core.smithy.generators.error.errorSymbol
import software.amazon.smithy.rust.codegen.core.smithy.generators.protocol.ProtocolSupport
import software.amazon.smithy.rust.codegen.core.testutil.TokioTest
Expand Down Expand Up @@ -66,9 +65,7 @@ class ProtocolTestGenerator(
private val operationSymbol = codegenContext.symbolProvider.toSymbol(operationShape)
private val operationIndex = OperationIndex.of(codegenContext.model)

private val instantiator = with(codegenContext) {
Instantiator(symbolProvider, model, runtimeConfig, CodegenTarget.CLIENT)
}
private val instantiator = clientInstantiator(codegenContext)

private val codegenScope = arrayOf(
"SmithyHttp" to CargoDependency.SmithyHttp(codegenContext.runtimeConfig).asType(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class HttpBoundProtocolTraitImplGenerator(
}
}

val err = if (StructureGenerator.fallibleBuilder(outputShape, symbolProvider)) {
val err = if (StructureGenerator.hasFallibleBuilder(outputShape, symbolProvider)) {
".map_err(${format(errorSymbol)}::unhandled)?"
} else ""

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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

import org.junit.jupiter.api.Test
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.rust.codegen.client.testutil.testCodegenContext
import software.amazon.smithy.rust.codegen.core.rustlang.RustModule
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.withBlock
import software.amazon.smithy.rust.codegen.core.smithy.generators.EnumGenerator
import software.amazon.smithy.rust.codegen.core.testutil.TestWorkspace
import software.amazon.smithy.rust.codegen.core.testutil.asSmithyModel
import software.amazon.smithy.rust.codegen.core.testutil.compileAndTest
import software.amazon.smithy.rust.codegen.core.testutil.unitTest
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.expectTrait
import software.amazon.smithy.rust.codegen.core.util.lookup

internal class ClientInstantiatorTest {
private val model = """
namespace com.test

@enum([
{ value: "t2.nano" },
{ value: "t2.micro" },
])
string UnnamedEnum

@enum([
{
value: "t2.nano",
name: "T2_NANO",
},
{
value: "t2.micro",
name: "T2_MICRO",
},
])
string NamedEnum
""".asSmithyModel()

private val codegenContext = testCodegenContext(model)
private val symbolProvider = codegenContext.symbolProvider

@Test
fun `generate named enums`() {
val shape = model.lookup<StringShape>("com.test#NamedEnum")
val sut = clientInstantiator(codegenContext)
val data = Node.parse("t2.nano".dq())

val project = TestWorkspace.testProject()
project.withModule(RustModule.Model) {
EnumGenerator(model, symbolProvider, this, shape, shape.expectTrait()).render()
unitTest("generate_named_enums") {
withBlock("let result = ", ";") {
sut.render(this, shape, data)
}
rust("assert_eq!(result, NamedEnum::T2Nano);")
}
}
project.compileAndTest()
}

@Test
fun `generate unnamed enums`() {
val shape = model.lookup<StringShape>("com.test#UnnamedEnum")
val sut = clientInstantiator(codegenContext)
val data = Node.parse("t2.nano".dq())

val project = TestWorkspace.testProject()
project.withModule(RustModule.Model) {
EnumGenerator(model, symbolProvider, this, shape, shape.expectTrait()).render()
unitTest("generate_unnamed_enums") {
withBlock("let result = ", ";") {
sut.render(this, shape, data)
}
rust("""assert_eq!(result, UnnamedEnum("t2.nano".to_owned()));""")
}
}
project.compileAndTest()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class BuilderGenerator(
}

private fun renderBuildFn(implBlockWriter: RustWriter) {
val fallibleBuilder = StructureGenerator.fallibleBuilder(shape, symbolProvider)
val fallibleBuilder = StructureGenerator.hasFallibleBuilder(shape, symbolProvider)
val outputSymbol = symbolProvider.toSymbol(shape)
val returnType = when (fallibleBuilder) {
true -> "Result<${implBlockWriter.format(outputSymbol)}, ${implBlockWriter.format(runtimeConfig.operationBuildError())}>"
Expand Down
Loading