|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package software.amazon.smithy.docgen.core.interceptors; |
| 7 | + |
| 8 | +import software.amazon.smithy.codegen.core.SymbolReference; |
| 9 | +import software.amazon.smithy.docgen.core.sections.ShapeDetailsSection; |
| 10 | +import software.amazon.smithy.docgen.core.writers.DocWriter; |
| 11 | +import software.amazon.smithy.docgen.core.writers.DocWriter.AdmonitionType; |
| 12 | +import software.amazon.smithy.model.knowledge.PaginatedIndex; |
| 13 | +import software.amazon.smithy.model.traits.PaginatedTrait; |
| 14 | +import software.amazon.smithy.utils.CodeInterceptor; |
| 15 | + |
| 16 | +/** |
| 17 | + * This adds pagination information to operation docs. |
| 18 | + */ |
| 19 | +public final class PaginationInterceptor implements CodeInterceptor<ShapeDetailsSection, DocWriter> { |
| 20 | + @Override |
| 21 | + public Class<ShapeDetailsSection> sectionType() { |
| 22 | + return ShapeDetailsSection.class; |
| 23 | + } |
| 24 | + |
| 25 | + @Override |
| 26 | + public boolean isIntercepted(ShapeDetailsSection section) { |
| 27 | + return section.shape().isOperationShape() && section.shape().hasTrait(PaginatedTrait.class); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void write(DocWriter writer, String previousText, ShapeDetailsSection section) { |
| 32 | + var paginatedIndex = PaginatedIndex.of(section.context().model()); |
| 33 | + var service = section.context().settings().service(); |
| 34 | + var paginationInfo = paginatedIndex.getPaginationInfo(service, section.shape()).get(); |
| 35 | + var symbolProvider = section.context().symbolProvider(); |
| 36 | + writer.putContext("size", paginationInfo.getPageSizeMember().map(symbolProvider::toSymbol)); |
| 37 | + writer.putContext("inputToken", SymbolReference.builder() |
| 38 | + .symbol(symbolProvider.toSymbol(paginationInfo.getInputTokenMember())) |
| 39 | + .alias("input token") |
| 40 | + .build()); |
| 41 | + |
| 42 | + var outputTokenPath = paginationInfo.getOutputTokenMemberPath(); |
| 43 | + var outputToken = outputTokenPath.get(outputTokenPath.size() - 1); |
| 44 | + writer.putContext("outputToken", SymbolReference.builder() |
| 45 | + .symbol(symbolProvider.toSymbol(outputToken)) |
| 46 | + .alias("output token") |
| 47 | + .build()); |
| 48 | + |
| 49 | + writer.openAdmonition(AdmonitionType.IMPORTANT); |
| 50 | + writer.write(""" |
| 51 | + This operation returns partial results in pages${?size}, whose maximum size may be |
| 52 | + configured with ${size:R}${/size}. Each request may return an ${outputToken:R} that \ |
| 53 | + may be used as an ${inputToken:R} in subsequent requests to fetch the next page of results. \ |
| 54 | + If the operation does not return an ${outputToken:R}, that means that there are \ |
| 55 | + no more results. If the operation returns a repeated ${outputToken:R}, there MAY be \ |
| 56 | + more results later."""); |
| 57 | + writer.closeAdmonition(); |
| 58 | + writer.writeWithNoFormatting(previousText); |
| 59 | + } |
| 60 | +} |
0 commit comments