Skip to content

Commit b67d7d7

Browse files
authored
feat: add support for non-standard pagination termination via a truncation flag (#858)
1 parent d2cff77 commit b67d7d7

File tree

13 files changed

+424
-264
lines changed

13 files changed

+424
-264
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "0af8edd3-bbff-4bb2-b1b6-43ac5b27033d",
3+
"type": "misc",
4+
"description": "Support non-standard pagination termination"
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.kotlin.codegen.model.traits
6+
7+
import software.amazon.smithy.model.node.Node
8+
import software.amazon.smithy.model.node.ObjectNode
9+
import software.amazon.smithy.model.shapes.ShapeId
10+
import software.amazon.smithy.model.traits.AnnotationTrait
11+
12+
/**
13+
* Indicates the annotated member is a truncation indicator which conveys a non-standard termination condition for
14+
* pagination.
15+
*/
16+
class PaginationTruncationMember(node: ObjectNode) : AnnotationTrait(ID, node) {
17+
companion object {
18+
val ID: ShapeId = ShapeId.from("smithy.kotlin.traits#paginationTruncationMember")
19+
}
20+
21+
constructor() : this(Node.objectNode())
22+
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import software.amazon.smithy.kotlin.codegen.lang.KotlinTypes
1919
import software.amazon.smithy.kotlin.codegen.model.SymbolProperty
2020
import software.amazon.smithy.kotlin.codegen.model.expectShape
2121
import software.amazon.smithy.kotlin.codegen.model.hasTrait
22+
import software.amazon.smithy.kotlin.codegen.model.traits.PaginationTruncationMember
2223
import software.amazon.smithy.kotlin.codegen.utils.getOrNull
2324
import software.amazon.smithy.model.Model
2425
import software.amazon.smithy.model.knowledge.PaginatedIndex
@@ -28,6 +29,7 @@ import software.amazon.smithy.model.shapes.MapShape
2829
import software.amazon.smithy.model.shapes.OperationShape
2930
import software.amazon.smithy.model.shapes.ServiceShape
3031
import software.amazon.smithy.model.shapes.Shape
32+
import software.amazon.smithy.model.shapes.StructureShape
3133
import software.amazon.smithy.model.traits.PaginatedTrait
3234

3335
/**
@@ -77,6 +79,7 @@ class PaginatorGenerator : KotlinIntegration {
7779
serviceSymbol,
7880
paginatedOperation,
7981
inputSymbol,
82+
paginationInfo.output,
8083
outputSymbol,
8184
paginationInfo,
8285
cursorSymbol,
@@ -100,6 +103,7 @@ class PaginatorGenerator : KotlinIntegration {
100103
serviceSymbol: Symbol,
101104
operationShape: OperationShape,
102105
inputSymbol: Symbol,
106+
outputShape: StructureShape,
103107
outputSymbol: Symbol,
104108
paginationInfo: PaginationInfo,
105109
cursorSymbol: Symbol,
@@ -141,18 +145,26 @@ class PaginatorGenerator : KotlinIntegration {
141145
) {
142146
withBlock("#T {", "}", ExternalTypes.KotlinxCoroutines.FlowGenerator) {
143147
write("var cursor: #F = null", cursorSymbol)
144-
write("var isFirstPage: Boolean = true")
148+
write("var hasNextPage: Boolean = true")
145149
write("")
146-
withBlock("while (isFirstPage || (cursor?.isNotEmpty() == true)) {", "}") {
150+
withBlock("while (hasNextPage) {", "}") {
147151
withBlock("val req = initialRequest.copy {", "}") {
148152
write("this.$markerLiteral = cursor")
149153
}
150154
write(
151155
"val result = this@#1LPaginated.#1L(req)",
152156
operationShape.defaultName(),
153157
)
154-
write("isFirstPage = false")
155158
write("cursor = result.$nextMarkerLiteral")
159+
160+
val hasNextPageFlag = outputShape
161+
.members()
162+
.singleOrNull { it.hasTrait(PaginationTruncationMember.ID) }
163+
?.defaultName()
164+
?.let { "result.$it" }
165+
?: "cursor?.isNotEmpty()"
166+
167+
write("hasNextPage = #L == true", hasNextPageFlag)
156168
write("emit(result)")
157169
}
158170
}

0 commit comments

Comments
 (0)