|
| 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.Collections; |
| 19 | +import java.util.HashMap; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.function.Consumer; |
| 24 | +import java.util.logging.Logger; |
| 25 | + |
| 26 | +import software.amazon.smithy.aws.traits.ServiceTrait; |
| 27 | +import software.amazon.smithy.aws.typescript.codegen.extensions.AwsRegionExtensionConfiguration; |
| 28 | +import software.amazon.smithy.codegen.core.SymbolProvider; |
| 29 | +import software.amazon.smithy.model.Model; |
| 30 | +import software.amazon.smithy.model.knowledge.TopDownIndex; |
| 31 | +import software.amazon.smithy.model.shapes.OperationShape; |
| 32 | +import software.amazon.smithy.model.shapes.ServiceShape; |
| 33 | +import software.amazon.smithy.model.traits.RequestCompressionTrait; |
| 34 | +import software.amazon.smithy.typescript.codegen.LanguageTarget; |
| 35 | +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; |
| 36 | +import software.amazon.smithy.typescript.codegen.TypeScriptSettings; |
| 37 | +import software.amazon.smithy.typescript.codegen.TypeScriptWriter; |
| 38 | +import software.amazon.smithy.typescript.codegen.extensions.ExtensionConfigurationInterface; |
| 39 | +import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; |
| 40 | +import software.amazon.smithy.utils.MapUtils; |
| 41 | +import software.amazon.smithy.utils.SmithyInternalApi; |
| 42 | + |
| 43 | +/** |
| 44 | + * Adds compression dependencies if needed. |
| 45 | + */ |
| 46 | +@SmithyInternalApi |
| 47 | +public final class AddCompressionDependency implements TypeScriptIntegration { |
| 48 | + |
| 49 | + private static final Logger LOGGER = Logger.getLogger(AddAwsRuntimeConfig.class.getName()); |
| 50 | + |
| 51 | + @Override |
| 52 | + public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( |
| 53 | + TypeScriptSettings settings, |
| 54 | + Model model, |
| 55 | + SymbolProvider symbolProvider, |
| 56 | + LanguageTarget target |
| 57 | + ) { |
| 58 | + if (!hasRequestCompressionTrait(model, settings.getService(model))) { |
| 59 | + return Collections.emptyMap(); |
| 60 | + } |
| 61 | + |
| 62 | + switch (target) { |
| 63 | + case NODE: |
| 64 | + |
| 65 | + default: |
| 66 | + return Collections.emptyMap(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // return true if operation shape is decorated with `httpChecksum` trait. |
| 71 | + private static boolean hasRequestCompressionTrait(OperationShape operation) { |
| 72 | + return operation.hasTrait(RequestCompressionTrait.class); |
| 73 | + } |
| 74 | + |
| 75 | + private static boolean hasRequestCompressionTrait( |
| 76 | + Model model, |
| 77 | + ServiceShape service |
| 78 | + ) { |
| 79 | + TopDownIndex topDownIndex = TopDownIndex.of(model); |
| 80 | + Set<OperationShape> operations = topDownIndex.getContainedOperations(service); |
| 81 | + for (OperationShape operation : operations) { |
| 82 | + if (hasRequestCompressionTrait(operation)) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + } |
| 86 | + return false; |
| 87 | + } |
| 88 | +} |
0 commit comments