Skip to content

Commit 29cf558

Browse files
authored
fix: move DSL overloads to be extension methods on generated clients
1 parent 52a36af commit 29cf558

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "e1fa257d-73b3-4616-8c39-f32956008124",
3+
"type": "bugfix",
4+
"description": "**Breaking**: Move DSL overloads on generated clients to extension methods"
5+
}

smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceGenerator.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ class ServiceGenerator(private val ctx: RenderingContext<ServiceShape>) {
8080
renderServiceConfig()
8181
}
8282
.call {
83-
operations.forEach { op ->
84-
renderOperation(operationsIndex, op)
85-
}
83+
operations.forEach { renderOperation(operationsIndex, it) }
8684
}
8785
.closeBlock("}")
8886
.write("")
87+
88+
operations.forEach { renderOperationDslOverload(operationsIndex, it) }
8989
}
9090

9191
private fun renderServiceConfig() {
@@ -154,7 +154,9 @@ class ServiceGenerator(private val ctx: RenderingContext<ServiceShape>) {
154154
writer.renderDocumentation(op)
155155
writer.renderAnnotations(op)
156156
writer.write(opIndex.operationSignature(ctx.model, ctx.symbolProvider, op, includeOptionalDefault = true))
157+
}
157158

159+
private fun renderOperationDslOverload(opIndex: OperationIndex, op: OperationShape) {
158160
// Add DSL overload (if appropriate)
159161
opIndex.getInput(op).ifPresent { inputShape ->
160162
val outputShape = opIndex.getOutput(op)
@@ -167,7 +169,7 @@ class ServiceGenerator(private val ctx: RenderingContext<ServiceShape>) {
167169
writer.write("")
168170
writer.renderDocumentation(op)
169171
writer.renderAnnotations(op)
170-
val signature = "suspend fun $operationName(block: $input.Builder.() -> Unit)"
172+
val signature = "suspend inline fun ${serviceSymbol.name}.$operationName(crossinline block: $input.Builder.() -> Unit)"
171173
val impl = "$operationName($input.Builder().apply(block).build())"
172174
writer.write("$signature = $impl")
173175
}

smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGenerator.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class StructureGenerator(
233233
write("")
234234

235235
// generate the constructor used internally by serde
236+
write("@PublishedApi")
236237
write("internal constructor()")
237238
// generate the constructor that converts from the underlying immutable class to a builder instance
238239
writer.write("@PublishedApi")

smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/ServiceGeneratorTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ class ServiceGeneratorTest {
164164
@Test
165165
fun `it adds DSL overloads for operations`() {
166166
val expectedSignatures = listOf(
167-
"suspend fun getFoo(block: GetFooRequest.Builder.() -> Unit) = getFoo(GetFooRequest.Builder().apply(block).build())",
168-
"suspend fun getFooNoInput(block: GetFooNoInputRequest.Builder.() -> Unit) = getFooNoInput(GetFooNoInputRequest.Builder().apply(block).build())",
169-
"suspend fun getFooNoOutput(block: GetFooNoOutputRequest.Builder.() -> Unit) = getFooNoOutput(GetFooNoOutputRequest.Builder().apply(block).build())",
170-
"suspend fun getFooStreamingInput(block: GetFooStreamingInputRequest.Builder.() -> Unit) = getFooStreamingInput(GetFooStreamingInputRequest.Builder().apply(block).build())",
171-
"suspend fun getFooStreamingInputNoOutput(block: GetFooStreamingInputNoOutputRequest.Builder.() -> Unit) = getFooStreamingInputNoOutput(GetFooStreamingInputNoOutputRequest.Builder().apply(block).build())",
167+
"suspend inline fun TestClient.getFoo(crossinline block: GetFooRequest.Builder.() -> Unit) = getFoo(GetFooRequest.Builder().apply(block).build())",
168+
"suspend inline fun TestClient.getFooNoInput(crossinline block: GetFooNoInputRequest.Builder.() -> Unit) = getFooNoInput(GetFooNoInputRequest.Builder().apply(block).build())",
169+
"suspend inline fun TestClient.getFooNoOutput(crossinline block: GetFooNoOutputRequest.Builder.() -> Unit) = getFooNoOutput(GetFooNoOutputRequest.Builder().apply(block).build())",
170+
"suspend inline fun TestClient.getFooStreamingInput(crossinline block: GetFooStreamingInputRequest.Builder.() -> Unit) = getFooStreamingInput(GetFooStreamingInputRequest.Builder().apply(block).build())",
171+
"suspend inline fun TestClient.getFooStreamingInputNoOutput(crossinline block: GetFooStreamingInputNoOutputRequest.Builder.() -> Unit) = getFooStreamingInputNoOutput(GetFooStreamingInputNoOutputRequest.Builder().apply(block).build())",
172172
)
173173
expectedSignatures.forEach {
174174
commonTestContents.shouldContainOnlyOnceWithDiff(it)

smithy-kotlin-codegen/src/test/kotlin/software/amazon/smithy/kotlin/codegen/rendering/StructureGeneratorTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class StructureGeneratorTest {
174174
var `object`: kotlin.String? = null
175175
var quux: com.test.model.Qux? = null
176176
177+
@PublishedApi
177178
internal constructor()
178179
@PublishedApi
179180
internal constructor(x: com.test.model.MyStruct) : this() {

0 commit comments

Comments
 (0)