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

Fix smithy sdk no default client #564

Merged
merged 6 commits into from
Jan 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class ServiceGenerator(private val ctx: RenderingContext<ServiceShape>) {
require(ctx.shape is ServiceShape) { "ServiceShape is required for generating a service interface; was: ${ctx.shape}" }
}

private val service: ServiceShape = requireNotNull(ctx.shape) { "ServiceShape is required to render a service client" }
private val service: ServiceShape =
requireNotNull(ctx.shape) { "ServiceShape is required to render a service client" }
private val serviceSymbol = ctx.symbolProvider.toSymbol(service)
private val writer = ctx.writer

Expand Down Expand Up @@ -72,7 +73,7 @@ class ServiceGenerator(private val ctx: RenderingContext<ServiceShape>) {
SectionServiceCompanionObject,
context = mapOf(SectionServiceCompanionObject.ServiceSymbol to serviceSymbol)
) {
renderCompanionObject()
renderCompanionObject(ctx.protocolGenerator != null)
Copy link
Contributor

Choose a reason for hiding this comment

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

style

ctx is a property on the class, no real reason to thread through the parameter

}
writer.write("")
renderServiceConfig()
Expand Down Expand Up @@ -109,16 +110,20 @@ class ServiceGenerator(private val ctx: RenderingContext<ServiceShape>) {
* operator fun invoke(config: Config): LambdaClient = DefaultLambdaClient(config)
* }
* ```
* @param hasProtocolGenerator true if there is a [ProtocolGenerator] set on the [RenderingContext]. This
* determines if the SDK will generate a default client implementation. If there is no [ProtocolGenerator], do not
* codegen references to the non-existent default client.
*/
private fun renderCompanionObject() {
private fun renderCompanionObject(hasProtocolGenerator: Boolean) {
writer.withBlock("companion object {", "}") {
withBlock("operator fun invoke(block: Config.Builder.() -> Unit = {}): ${serviceSymbol.name} {", "}") {
write("val config = Config.Builder().apply(block).build()")
write("return Default${serviceSymbol.name}(config)")
this.callIf(hasProtocolGenerator) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit/style does this need qualified here?

withBlock("operator fun invoke(block: Config.Builder.() -> Unit = {}): ${serviceSymbol.name} {", "}") {
write("val config = Config.Builder().apply(block).build()")
write("return Default${serviceSymbol.name}(config)")
}
write("")
write("operator fun invoke(config: Config): ${serviceSymbol.name} = Default${serviceSymbol.name}(config)")
}

write("")
write("operator fun invoke(config: Config): ${serviceSymbol.name} = Default${serviceSymbol.name}(config)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ServiceGeneratorTest {
}

@Test
fun `it renders a companion object`() {
fun `it renders a companion object with default client factory if protocol generator`() {
val expected = """
companion object {
operator fun invoke(block: Config.Builder.() -> Unit = {}): TestClient {
Expand All @@ -74,6 +74,16 @@ class ServiceGeneratorTest {
operator fun invoke(config: Config): TestClient = DefaultTestClient(config)
}
""".formatForTest()
val testContents = generateService("service-generator-test-operations.smithy", true)
testContents.shouldContainOnlyOnceWithDiff(expected)
}

@Test
fun `it renders a companion object without default client factory if no protocol generator`() {
val expected = """
companion object {
}
""".formatForTest()
commonTestContents.shouldContainOnlyOnceWithDiff(expected)
}

Expand Down Expand Up @@ -164,14 +174,15 @@ class ServiceGeneratorTest {
}

// Produce the generated service code given model inputs.
private fun generateService(modelResourceName: String): String {
private fun generateService(modelResourceName: String, withProtocolGenerator: Boolean = false): String {
val model = loadModelFromResource(modelResourceName)

val provider: SymbolProvider = KotlinCodegenPlugin.createSymbolProvider(model)
val writer = KotlinWriter(TestModelDefault.NAMESPACE)
val service = model.getShape(ShapeId.from(TestModelDefault.SERVICE_SHAPE_ID)).get().asServiceShape().get()
val settings = KotlinSettings(service.id, KotlinSettings.PackageSettings(TestModelDefault.NAMESPACE, TestModelDefault.MODEL_VERSION), sdkId = service.id.name)
val renderingCtx = RenderingContext(writer, service, model, provider, settings)
val protocolGenerator = if (withProtocolGenerator) MockHttpProtocolGenerator() else null
val renderingCtx = RenderingContext(writer, service, model, provider, settings, protocolGenerator)
val generator = ServiceGenerator(renderingCtx)

generator.render()
Expand Down