|
| 1 | +/* |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.smithy.aws.typescript.codegen; |
| 17 | + |
| 18 | +import java.nio.file.Paths; |
| 19 | +import java.util.Optional; |
| 20 | +import software.amazon.smithy.codegen.core.CodegenException; |
| 21 | +import software.amazon.smithy.codegen.core.Symbol; |
| 22 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 23 | +import software.amazon.smithy.model.Model; |
| 24 | +import software.amazon.smithy.model.knowledge.PaginatedIndex; |
| 25 | +import software.amazon.smithy.model.knowledge.PaginationInfo; |
| 26 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 27 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 28 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 29 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 30 | + |
| 31 | +@SmithyInternalApi |
| 32 | +final class DocumentClientPaginationGenerator implements Runnable { |
| 33 | + |
| 34 | + static final String PAGINATION_FOLDER = "pagination"; |
| 35 | + |
| 36 | + private final TypeScriptWriter writer; |
| 37 | + private final PaginationInfo paginatedInfo; |
| 38 | + |
| 39 | + private final String operationTypeName; |
| 40 | + private final String inputTypeName; |
| 41 | + private final String outputTypeName; |
| 42 | + |
| 43 | + private final String operationName; |
| 44 | + private final String methodName; |
| 45 | + private final String paginationType; |
| 46 | + |
| 47 | + DocumentClientPaginationGenerator( |
| 48 | + Model model, |
| 49 | + ServiceShape service, |
| 50 | + OperationShape operation, |
| 51 | + SymbolProvider symbolProvider, |
| 52 | + TypeScriptWriter writer |
| 53 | + ) { |
| 54 | + this.writer = writer; |
| 55 | + |
| 56 | + Symbol operationSymbol = symbolProvider.toSymbol(operation); |
| 57 | + Symbol inputSymbol = symbolProvider.toSymbol(operation).expectProperty("inputType", Symbol.class); |
| 58 | + Symbol outputSymbol = symbolProvider.toSymbol(operation).expectProperty("outputType", Symbol.class); |
| 59 | + |
| 60 | + this.operationTypeName = DocumentClientUtils.getModifiedName(operationSymbol.getName()); |
| 61 | + this.inputTypeName = DocumentClientUtils.getModifiedName(inputSymbol.getName()); |
| 62 | + this.outputTypeName = DocumentClientUtils.getModifiedName(outputSymbol.getName()); |
| 63 | + |
| 64 | + // e.g. listObjects |
| 65 | + this.operationName = operationTypeName.replace("Command", ""); |
| 66 | + this.methodName = Character.toLowerCase(operationName.charAt(0)) + operationName.substring(1); |
| 67 | + this.paginationType = DocumentClientUtils.CLIENT_FULL_NAME + "PaginationConfiguration"; |
| 68 | + |
| 69 | + PaginatedIndex paginatedIndex = PaginatedIndex.of(model); |
| 70 | + Optional<PaginationInfo> paginationInfo = paginatedIndex.getPaginationInfo(service, operation); |
| 71 | + this.paginatedInfo = paginationInfo.orElseThrow(() -> { |
| 72 | + return new CodegenException("Expected Paginator to have pagination information."); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void run() { |
| 78 | + // Import Service Types |
| 79 | + String commandFileLocation = Paths.get(".", DocumentClientUtils.CLIENT_COMMANDS_FOLDER, |
| 80 | + DocumentClientUtils.getModifiedName(operationTypeName)).toString(); |
| 81 | + writer.addImport(operationTypeName, operationTypeName, commandFileLocation); |
| 82 | + writer.addImport(inputTypeName, inputTypeName, commandFileLocation); |
| 83 | + writer.addImport(outputTypeName, outputTypeName, commandFileLocation); |
| 84 | + writer.addImport( |
| 85 | + DocumentClientUtils.CLIENT_NAME, |
| 86 | + DocumentClientUtils.CLIENT_NAME, |
| 87 | + Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString()); |
| 88 | + writer.addImport( |
| 89 | + DocumentClientUtils.CLIENT_FULL_NAME, |
| 90 | + DocumentClientUtils.CLIENT_FULL_NAME, |
| 91 | + Paths.get(".", DocumentClientUtils.CLIENT_FULL_NAME).toString()); |
| 92 | + |
| 93 | + // Import Pagination types |
| 94 | + writer.addImport("Paginator", "Paginator", "@aws-sdk/types"); |
| 95 | + writer.addImport(paginationType, paginationType, |
| 96 | + Paths.get(".", getInterfaceFilelocation().replace(".ts", "")).toString()); |
| 97 | + |
| 98 | + writeCommandRequest(); |
| 99 | + writeMethodRequest(); |
| 100 | + writePager(); |
| 101 | + } |
| 102 | + |
| 103 | + static String getOutputFilelocation(OperationShape operation) { |
| 104 | + return String.format("%s%s/%s.ts", DocumentClientUtils.DOC_CLIENT_PREFIX, |
| 105 | + DocumentClientPaginationGenerator.PAGINATION_FOLDER, |
| 106 | + DocumentClientUtils.getModifiedName(operation.getId().getName()) + "Paginator"); |
| 107 | + } |
| 108 | + |
| 109 | + static String getInterfaceFilelocation() { |
| 110 | + return String.format("%s%s/%s.ts", DocumentClientUtils.DOC_CLIENT_PREFIX, |
| 111 | + DocumentClientPaginationGenerator.PAGINATION_FOLDER, "Interfaces"); |
| 112 | + } |
| 113 | + |
| 114 | + static void generateServicePaginationInterfaces(TypeScriptWriter writer) { |
| 115 | + writer.addImport("PaginationConfiguration", "PaginationConfiguration", "@aws-sdk/types"); |
| 116 | + |
| 117 | + writer.addImport( |
| 118 | + DocumentClientUtils.CLIENT_NAME, |
| 119 | + DocumentClientUtils.CLIENT_NAME, |
| 120 | + Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString()); |
| 121 | + writer.addImport( |
| 122 | + DocumentClientUtils.CLIENT_FULL_NAME, |
| 123 | + DocumentClientUtils.CLIENT_FULL_NAME, |
| 124 | + Paths.get(".", DocumentClientUtils.CLIENT_FULL_NAME).toString()); |
| 125 | + |
| 126 | + writer.openBlock("export interface $LPaginationConfiguration extends PaginationConfiguration {", |
| 127 | + "}", DocumentClientUtils.CLIENT_FULL_NAME, () -> { |
| 128 | + writer.write("client: $L | $L;", DocumentClientUtils.CLIENT_FULL_NAME, DocumentClientUtils.CLIENT_NAME); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + private String destructurePath(String path) { |
| 133 | + return "." + path.replace(".", "!."); |
| 134 | + } |
| 135 | + |
| 136 | + private void writePager() { |
| 137 | + String inputTokenName = paginatedInfo.getPaginatedTrait().getInputToken().get(); |
| 138 | + String outputTokenName = paginatedInfo.getPaginatedTrait().getOutputToken().get(); |
| 139 | + |
| 140 | + writer.openBlock( |
| 141 | + "export async function* paginate$L(config: $L, input: $L, ...additionalArguments: any): Paginator<$L>{", |
| 142 | + "}", operationName, paginationType, inputTypeName, outputTypeName, () -> { |
| 143 | + String destructuredInputTokenName = destructurePath(inputTokenName); |
| 144 | + writer.write("// ToDo: replace with actual type instead of typeof input$L", destructuredInputTokenName); |
| 145 | + writer.write("let token: typeof input$L | undefined = config.startingToken || undefined;", |
| 146 | + destructuredInputTokenName); |
| 147 | + |
| 148 | + writer.write("let hasNext = true;"); |
| 149 | + writer.write("let page: $L;", outputTypeName); |
| 150 | + writer.openBlock("while (hasNext) {", "}", () -> { |
| 151 | + writer.write("input$L = token;", destructuredInputTokenName); |
| 152 | + |
| 153 | + if (paginatedInfo.getPageSizeMember().isPresent()) { |
| 154 | + String pageSize = paginatedInfo.getPageSizeMember().get().getMemberName(); |
| 155 | + writer.write("input[$S] = config.pageSize;", pageSize); |
| 156 | + } |
| 157 | + |
| 158 | + writer.openBlock("if (config.client instanceof $L) {", "}", DocumentClientUtils.CLIENT_FULL_NAME, |
| 159 | + () -> { |
| 160 | + writer.write("page = await makePagedRequest(config.client, input, ...additionalArguments);"); |
| 161 | + } |
| 162 | + ); |
| 163 | + writer.openBlock("else if (config.client instanceof $L) {", "}", DocumentClientUtils.CLIENT_NAME, |
| 164 | + () -> { |
| 165 | + writer.write( |
| 166 | + "page = await makePagedClientRequest(config.client, input, ...additionalArguments);"); |
| 167 | + } |
| 168 | + ); |
| 169 | + writer.openBlock("else {", "}", () -> { |
| 170 | + writer.write("throw new Error(\"Invalid client, expected $L | $L\");", |
| 171 | + DocumentClientUtils.CLIENT_FULL_NAME, DocumentClientUtils.CLIENT_NAME); |
| 172 | + }); |
| 173 | + |
| 174 | + writer.write("yield page;"); |
| 175 | + writer.write("token = page$L;", destructurePath(outputTokenName)); |
| 176 | + |
| 177 | + writer.write("hasNext = !!(token);"); |
| 178 | + }); |
| 179 | + |
| 180 | + writer.write("// @ts-ignore"); |
| 181 | + writer.write("return undefined;"); |
| 182 | + }); |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + /** |
| 187 | + * Paginated command that calls client.method({...}) under the hood. This is meant for server side environments and |
| 188 | + * exposes the entire service. |
| 189 | + */ |
| 190 | + private void writeMethodRequest() { |
| 191 | + writer.writeDocs("@private"); |
| 192 | + writer.openBlock( |
| 193 | + "const makePagedRequest = async (client: $L, input: $L, ...args: any): Promise<$L> => {", |
| 194 | + "}", DocumentClientUtils.CLIENT_FULL_NAME, inputTypeName, |
| 195 | + outputTypeName, () -> { |
| 196 | + writer.write("// @ts-ignore"); |
| 197 | + writer.write("return await client.$L(input, ...args);", methodName); |
| 198 | + }); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * Paginated command that calls CommandClient().send({...}) under the hood. This is meant for client side (browser) |
| 203 | + * environments and does not generally expose the entire service. |
| 204 | + */ |
| 205 | + private void writeCommandRequest() { |
| 206 | + writer.writeDocs("@private"); |
| 207 | + writer.openBlock( |
| 208 | + "const makePagedClientRequest = async (client: $L, input: $L, ...args: any): Promise<$L> => {", |
| 209 | + "}", DocumentClientUtils.CLIENT_NAME, inputTypeName, |
| 210 | + outputTypeName, () -> { |
| 211 | + writer.write("// @ts-ignore"); |
| 212 | + writer.write("return await client.send(new $L(input), ...args);", operationTypeName); |
| 213 | + }); |
| 214 | + } |
| 215 | +} |
0 commit comments