|
| 1 | +/* |
| 2 | + * Copyright 2019 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.util.ArrayList; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.TreeSet; |
| 22 | +import java.util.function.BiConsumer; |
| 23 | +import java.util.function.Consumer; |
| 24 | +import software.amazon.smithy.aws.traits.ServiceTrait; |
| 25 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 26 | +import software.amazon.smithy.model.Model; |
| 27 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 28 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 29 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 30 | +import software.amazon.smithy.model.shapes.Shape; |
| 31 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 32 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 33 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 34 | +import software.amazon.smithy.utils.IoUtils; |
| 35 | + |
| 36 | +/** |
| 37 | + * Generates Client and Commands for DynamoDB Document Client. |
| 38 | + */ |
| 39 | +public class AddDocumentClientPlugin implements TypeScriptIntegration { |
| 40 | + |
| 41 | + @Override |
| 42 | + public void writeAdditionalFiles( |
| 43 | + TypeScriptSettings settings, |
| 44 | + Model model, |
| 45 | + SymbolProvider symbolProvider, |
| 46 | + BiConsumer<String, Consumer<TypeScriptWriter>> writerFactory |
| 47 | + ) { |
| 48 | + ServiceShape service = settings.getService(model); |
| 49 | + if (testServiceId(service, "DynamoDB")) { |
| 50 | + String docClientPrefix = "doc-client-"; |
| 51 | + Set<OperationShape> containedOperations = |
| 52 | + new TreeSet<>(TopDownIndex.of(model).getContainedOperations(service)); |
| 53 | + List<OperationShape> overridenOperationsList = new ArrayList<>(); |
| 54 | + |
| 55 | + for (OperationShape operation : containedOperations) { |
| 56 | + String operationName = symbolProvider.toSymbol(operation).getName(); |
| 57 | + String commandFileName = String.format("%s%s/%s.ts", docClientPrefix, |
| 58 | + DocumentClientUtils.CLIENT_COMMANDS_FOLDER, DocumentClientUtils.getModifiedName(operationName)); |
| 59 | + |
| 60 | + if (DocumentClientUtils.containsAttributeValue(model, symbolProvider, operation)) { |
| 61 | + overridenOperationsList.add(operation); |
| 62 | + writerFactory.accept(commandFileName, |
| 63 | + writer -> new DocumentClientCommandGenerator( |
| 64 | + settings, model, operation, symbolProvider, writer).run() |
| 65 | + ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + writerFactory.accept(String.format("%s%s.ts", docClientPrefix, DocumentClientUtils.CLIENT_NAME), |
| 70 | + writer -> new DocumentClientGenerator(settings, model, symbolProvider, writer).run()); |
| 71 | + |
| 72 | + writerFactory.accept(String.format("%s%s.ts", docClientPrefix, DocumentClientUtils.CLIENT_FULL_NAME), |
| 73 | + writer -> new DocumentFullClientGenerator(settings, model, symbolProvider, writer).run()); |
| 74 | + |
| 75 | + writerFactory.accept(String.format("%sindex.ts", docClientPrefix), writer -> { |
| 76 | + for (OperationShape operationOverriden: overridenOperationsList) { |
| 77 | + String operationFileName = DocumentClientUtils.getModifiedName( |
| 78 | + symbolProvider.toSymbol(operationOverriden).getName() |
| 79 | + ); |
| 80 | + writer.write("export * from './$L/$L';", |
| 81 | + DocumentClientUtils.CLIENT_COMMANDS_FOLDER, operationFileName); |
| 82 | + } |
| 83 | + writer.write("export * from './$L';", DocumentClientUtils.CLIENT_NAME); |
| 84 | + writer.write("export * from './$L';", DocumentClientUtils.CLIENT_FULL_NAME); |
| 85 | + }); |
| 86 | + |
| 87 | + String utilsFileLocation = String.format("%s%s", docClientPrefix, DocumentClientUtils.CLIENT_UTILS_FILE); |
| 88 | + writerFactory.accept(String.format("%s%s/%s.ts", docClientPrefix, |
| 89 | + DocumentClientUtils.CLIENT_COMMANDS_FOLDER, DocumentClientUtils.CLIENT_UTILS_FILE), writer -> { |
| 90 | + writer.write(IoUtils.readUtf8Resource(AddDocumentClientPlugin.class, |
| 91 | + String.format("%s.ts", utilsFileLocation))); |
| 92 | + }); |
| 93 | + writerFactory.accept(String.format("%s%s/%s.spec.ts", docClientPrefix, |
| 94 | + DocumentClientUtils.CLIENT_COMMANDS_FOLDER, DocumentClientUtils.CLIENT_UTILS_FILE), writer -> { |
| 95 | + writer.write(IoUtils.readUtf8Resource(AddDocumentClientPlugin.class, |
| 96 | + String.format("%s.spec.ts", utilsFileLocation))); |
| 97 | + }); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private boolean testServiceId(Shape serviceShape, String expectedId) { |
| 102 | + return serviceShape.getTrait(ServiceTrait.class).map(ServiceTrait::getSdkId).orElse("").equals(expectedId); |
| 103 | + } |
| 104 | +} |
0 commit comments