Skip to content

Commit a6cfe9f

Browse files
authored
fix: move paginator doc comments to methods (#509)
1 parent 202640a commit a6cfe9f

File tree

2 files changed

+57
-60
lines changed

2 files changed

+57
-60
lines changed

smithy-swift-codegen/src/main/kotlin/software/amazon/smithy/swift/codegen/PaginatorGenerator.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class PaginatorGenerator : SwiftIntegration {
107107
val markerLiteral = paginationInfo.inputTokenMember.toLowerCamelCase()
108108
val markerLiteralShape = model.expectShape(paginationInfo.inputTokenMember.target)
109109
val markerLiteralSymbol = symbolProvider.toSymbol(markerLiteralShape)
110-
val docBody = """
110+
writer.openBlock("extension \$L {", "}", serviceSymbol.name) {
111+
val docBody = """
111112
Paginate over `[${outputSymbol.name}]` results.
112113
113114
When this operation is called, an `AsyncSequence` is created. AsyncSequences are lazy so no service
@@ -116,13 +117,10 @@ class PaginatorGenerator : SwiftIntegration {
116117
- Parameters:
117118
- input: A `[${inputSymbol.name}]` to start pagination
118119
- Returns: An `AsyncSequence` that can iterate over `${outputSymbol.name}`
119-
""".trimIndent()
120-
writer.write("")
121-
writer.writeSingleLineDocs {
122-
this.write(docBody)
123-
}
124-
125-
writer.openBlock("extension \$L {", "}", serviceSymbol.name) {
120+
""".trimIndent()
121+
writer.writeSingleLineDocs {
122+
this.write(docBody)
123+
}
126124
writer.openBlock(
127125
"public func \$LPaginated(input: \$N) -> \$N<\$N, \$N> {", "}",
128126
operationShape.toLowerCamelCase(),
@@ -183,17 +181,16 @@ class PaginatorGenerator : SwiftIntegration {
183181
) {
184182
writer.write("")
185183
val itemSymbolShape = itemDesc.itemSymbol.getProperty("shape").getOrNull() as? Shape
186-
val docBody = """
184+
185+
writer.openBlock("extension PaginatorSequence where Input == \$N, Output == \$N {", "}", inputSymbol, outputSymbol) {
186+
val docBody = """
187187
This paginator transforms the `AsyncSequence` returned by `${operationShape.toLowerCamelCase()}Paginated`
188188
to access the nested member `${itemDesc.collectionLiteral}`
189189
- Returns: `${itemDesc.collectionLiteral}`
190-
""".trimIndent()
191-
192-
writer.writeSingleLineDocs {
193-
this.write(docBody)
194-
}
195-
196-
writer.openBlock("extension PaginatorSequence where Input == \$N, Output == \$N {", "}", inputSymbol, outputSymbol) {
190+
""".trimIndent()
191+
writer.writeSingleLineDocs {
192+
this.write(docBody)
193+
}
197194
writer.openBlock("public func \$L() async throws -> \$L {", "}", itemDesc.itemLiteral, itemDesc.collectionLiteral) {
198195
if (itemSymbolShape?.isListShape == true) {
199196
writer.write("return try await self.asyncCompactMap { item in item.\$L }", itemDesc.itemPathLiteral)

smithy-swift-codegen/src/test/kotlin/PaginatorGeneratorTest.kt

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ class PaginatorGeneratorTest {
1515
val context = setupTests("pagination.smithy", "com.test#Lambda")
1616
val contents = getFileContents(context.manifest, "/Test/Paginators.swift")
1717
val expected = """
18-
/// Paginate over `[ListFunctions2OutputResponse]` results.
19-
///
20-
/// When this operation is called, an `AsyncSequence` is created. AsyncSequences are lazy so no service
21-
/// calls are made until the sequence is iterated over. This also means there is no guarantee that the request is valid
22-
/// until then. If there are errors in your request, you will see the failures only after you start iterating.
23-
/// - Parameters:
24-
/// - input: A `[ListFunctions2Input]` to start pagination
25-
/// - Returns: An `AsyncSequence` that can iterate over `ListFunctions2OutputResponse`
26-
extension TestClient {
27-
public func listFunctions2Paginated(input: ListFunctions2Input) -> ClientRuntime.PaginatorSequence<ListFunctions2Input, ListFunctions2OutputResponse> {
28-
return ClientRuntime.PaginatorSequence<ListFunctions2Input, ListFunctions2OutputResponse>(input: input, inputKey: \ListFunctions2Input.marker, outputKey: \ListFunctions2OutputResponse.nextMarker, paginationFunction: self.listFunctions2(input:))
29-
}
30-
}
18+
extension TestClient {
19+
/// Paginate over `[ListFunctionsOutputResponse]` results.
20+
///
21+
/// When this operation is called, an `AsyncSequence` is created. AsyncSequences are lazy so no service
22+
/// calls are made until the sequence is iterated over. This also means there is no guarantee that the request is valid
23+
/// until then. If there are errors in your request, you will see the failures only after you start iterating.
24+
/// - Parameters:
25+
/// - input: A `[ListFunctionsInput]` to start pagination
26+
/// - Returns: An `AsyncSequence` that can iterate over `ListFunctionsOutputResponse`
27+
public func listFunctionsPaginated(input: ListFunctionsInput) -> ClientRuntime.PaginatorSequence<ListFunctionsInput, ListFunctionsOutputResponse> {
28+
return ClientRuntime.PaginatorSequence<ListFunctionsInput, ListFunctionsOutputResponse>(input: input, inputKey: \ListFunctionsInput.marker, outputKey: \ListFunctionsOutputResponse.nextMarker, paginationFunction: self.listFunctions(input:))
29+
}
30+
}
3131
32-
extension ListFunctions2Input: ClientRuntime.PaginateToken {
33-
public func usingPaginationToken(_ token: Swift.String) -> ListFunctions2Input {
34-
return ListFunctions2Input(
35-
functionVersion: self.functionVersion,
36-
marker: token,
37-
masterRegion: self.masterRegion,
38-
maxItems: self.maxItems
39-
)}
40-
}
32+
extension ListFunctionsInput: ClientRuntime.PaginateToken {
33+
public func usingPaginationToken(_ token: Swift.String) -> ListFunctionsInput {
34+
return ListFunctionsInput(
35+
functionVersion: self.functionVersion,
36+
marker: token,
37+
masterRegion: self.masterRegion,
38+
maxItems: self.maxItems
39+
)}
40+
}
4141
""".trimIndent()
4242

4343
contents.shouldContainOnlyOnce(expected)
@@ -48,15 +48,15 @@ class PaginatorGeneratorTest {
4848
val context = setupTests("pagination.smithy", "com.test#Lambda")
4949
val contents = getFileContents(context.manifest, "/Test/Paginators.swift")
5050
val expectedCode = """
51-
/// Paginate over `[ListFunctionsOutputResponse]` results.
52-
///
53-
/// When this operation is called, an `AsyncSequence` is created. AsyncSequences are lazy so no service
54-
/// calls are made until the sequence is iterated over. This also means there is no guarantee that the request is valid
55-
/// until then. If there are errors in your request, you will see the failures only after you start iterating.
56-
/// - Parameters:
57-
/// - input: A `[ListFunctionsInput]` to start pagination
58-
/// - Returns: An `AsyncSequence` that can iterate over `ListFunctionsOutputResponse`
5951
extension TestClient {
52+
/// Paginate over `[ListFunctionsOutputResponse]` results.
53+
///
54+
/// When this operation is called, an `AsyncSequence` is created. AsyncSequences are lazy so no service
55+
/// calls are made until the sequence is iterated over. This also means there is no guarantee that the request is valid
56+
/// until then. If there are errors in your request, you will see the failures only after you start iterating.
57+
/// - Parameters:
58+
/// - input: A `[ListFunctionsInput]` to start pagination
59+
/// - Returns: An `AsyncSequence` that can iterate over `ListFunctionsOutputResponse`
6060
public func listFunctionsPaginated(input: ListFunctionsInput) -> ClientRuntime.PaginatorSequence<ListFunctionsInput, ListFunctionsOutputResponse> {
6161
return ClientRuntime.PaginatorSequence<ListFunctionsInput, ListFunctionsOutputResponse>(input: input, inputKey: \ListFunctionsInput.marker, outputKey: \ListFunctionsOutputResponse.nextMarker, paginationFunction: self.listFunctions(input:))
6262
}
@@ -72,10 +72,10 @@ class PaginatorGeneratorTest {
7272
)}
7373
}
7474
75-
/// This paginator transforms the `AsyncSequence` returned by `listFunctionsPaginated`
76-
/// to access the nested member `[TestClientTypes.FunctionConfiguration]`
77-
/// - Returns: `[TestClientTypes.FunctionConfiguration]`
7875
extension PaginatorSequence where Input == ListFunctionsInput, Output == ListFunctionsOutputResponse {
76+
/// This paginator transforms the `AsyncSequence` returned by `listFunctionsPaginated`
77+
/// to access the nested member `[TestClientTypes.FunctionConfiguration]`
78+
/// - Returns: `[TestClientTypes.FunctionConfiguration]`
7979
public func functions() async throws -> [TestClientTypes.FunctionConfiguration] {
8080
return try await self.asyncCompactMap { item in item.functions }
8181
}
@@ -90,15 +90,15 @@ class PaginatorGeneratorTest {
9090
val context = setupTests("pagination-map.smithy", "com.test#TestService")
9191
val contents = getFileContents(context.manifest, "/Test/Paginators.swift")
9292
val expectedCode = """
93-
/// Paginate over `[PaginatedMapOutputResponse]` results.
94-
///
95-
/// When this operation is called, an `AsyncSequence` is created. AsyncSequences are lazy so no service
96-
/// calls are made until the sequence is iterated over. This also means there is no guarantee that the request is valid
97-
/// until then. If there are errors in your request, you will see the failures only after you start iterating.
98-
/// - Parameters:
99-
/// - input: A `[PaginatedMapInput]` to start pagination
100-
/// - Returns: An `AsyncSequence` that can iterate over `PaginatedMapOutputResponse`
10193
extension TestClient {
94+
/// Paginate over `[PaginatedMapOutputResponse]` results.
95+
///
96+
/// When this operation is called, an `AsyncSequence` is created. AsyncSequences are lazy so no service
97+
/// calls are made until the sequence is iterated over. This also means there is no guarantee that the request is valid
98+
/// until then. If there are errors in your request, you will see the failures only after you start iterating.
99+
/// - Parameters:
100+
/// - input: A `[PaginatedMapInput]` to start pagination
101+
/// - Returns: An `AsyncSequence` that can iterate over `PaginatedMapOutputResponse`
102102
public func paginatedMapPaginated(input: PaginatedMapInput) -> ClientRuntime.PaginatorSequence<PaginatedMapInput, PaginatedMapOutputResponse> {
103103
return ClientRuntime.PaginatorSequence<PaginatedMapInput, PaginatedMapOutputResponse>(input: input, inputKey: \PaginatedMapInput.nextToken, outputKey: \PaginatedMapOutputResponse.inner?.token, paginationFunction: self.paginatedMap(input:))
104104
}
@@ -112,10 +112,10 @@ class PaginatorGeneratorTest {
112112
)}
113113
}
114114
115-
/// This paginator transforms the `AsyncSequence` returned by `paginatedMapPaginated`
116-
/// to access the nested member `[(String, Swift.Int)]`
117-
/// - Returns: `[(String, Swift.Int)]`
118115
extension PaginatorSequence where Input == PaginatedMapInput, Output == PaginatedMapOutputResponse {
116+
/// This paginator transforms the `AsyncSequence` returned by `paginatedMapPaginated`
117+
/// to access the nested member `[(String, Swift.Int)]`
118+
/// - Returns: `[(String, Swift.Int)]`
119119
public func mapItems() async throws -> [(String, Swift.Int)] {
120120
return try await self.asyncCompactMap { item in item.inner?.mapItems?.map { (${'$'}0, ${'$'}1) } }
121121
}

0 commit comments

Comments
 (0)