Skip to content

Commit ccaf579

Browse files
committed
chore(codegen): wip AddCompressionDependency
1 parent 6d740b7 commit ccaf579

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isAwsService;
19+
import static software.amazon.smithy.aws.typescript.codegen.AwsTraitsUtils.isSigV4Service;
20+
21+
import java.util.Collections;
22+
import java.util.HashMap;
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.Set;
26+
import java.util.function.Consumer;
27+
import java.util.logging.Logger;
28+
29+
import software.amazon.smithy.aws.traits.ServiceTrait;
30+
import software.amazon.smithy.aws.typescript.codegen.extensions.AwsRegionExtensionConfiguration;
31+
import software.amazon.smithy.codegen.core.SymbolProvider;
32+
import software.amazon.smithy.model.Model;
33+
import software.amazon.smithy.model.knowledge.TopDownIndex;
34+
import software.amazon.smithy.model.shapes.OperationShape;
35+
import software.amazon.smithy.model.shapes.ServiceShape;
36+
import software.amazon.smithy.model.traits.RequestCompressionTrait;
37+
import software.amazon.smithy.typescript.codegen.LanguageTarget;
38+
import software.amazon.smithy.typescript.codegen.TypeScriptDependency;
39+
import software.amazon.smithy.typescript.codegen.TypeScriptSettings;
40+
import software.amazon.smithy.typescript.codegen.TypeScriptWriter;
41+
import software.amazon.smithy.typescript.codegen.extensions.ExtensionConfigurationInterface;
42+
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration;
43+
import software.amazon.smithy.utils.MapUtils;
44+
import software.amazon.smithy.utils.SmithyInternalApi;
45+
46+
/**
47+
* Adds compression dependencies if needed.
48+
*/
49+
@SmithyInternalApi
50+
public final class AddCompressionDependency implements TypeScriptIntegration {
51+
52+
private static final Logger LOGGER = Logger.getLogger(AddAwsRuntimeConfig.class.getName());
53+
54+
@Override
55+
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
56+
TypeScriptSettings settings,
57+
Model model,
58+
SymbolProvider symbolProvider,
59+
LanguageTarget target
60+
) {
61+
if (!hasRequestCompressionTrait(model, settings.getService(model))) {
62+
return Collections.emptyMap();
63+
}
64+
65+
ServiceShape service = settings.getService(model);
66+
Map<String, Consumer<TypeScriptWriter>> runtimeConfigs = new HashMap();
67+
if (isAwsService(service) && target.equals(LanguageTarget.SHARED)) {
68+
String serviceId = service.expectTrait(ServiceTrait.class).getSdkId();
69+
runtimeConfigs.put("serviceId", writer -> writer.write("$S", serviceId));
70+
} else {
71+
LOGGER.info("Cannot generate a service ID for the client because no aws.api#Service "
72+
+ "trait was found on " + service.getId());
73+
}
74+
runtimeConfigs.putAll(getDefaultConfig(target, settings, model));
75+
runtimeConfigs.putAll(getEndpointConfigWriters(target, settings, model));
76+
return runtimeConfigs;
77+
}
78+
79+
// return true if operation shape is decorated with `httpChecksum` trait.
80+
private static boolean hasRequestCompressionTrait(OperationShape operation) {
81+
return operation.hasTrait(RequestCompressionTrait.class);
82+
}
83+
84+
private static boolean hasRequestCompressionTrait(
85+
Model model,
86+
ServiceShape service
87+
) {
88+
TopDownIndex topDownIndex = TopDownIndex.of(model);
89+
Set<OperationShape> operations = topDownIndex.getContainedOperations(service);
90+
for (OperationShape operation : operations) {
91+
if (hasRequestCompressionTrait(operation)) {
92+
return true;
93+
}
94+
}
95+
return false;
96+
}
97+
}

codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AwsSdkCustom
3030
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AddAwsDefaultSigningName
3131
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AddSTSAuthCustomizations
3232
software.amazon.smithy.aws.typescript.codegen.auth.http.integration.AwsSdkCustomizeEndpointRuleSetHttpAuthSchemeProvider
33+
software.amazon.smithy.aws.typescript.codegen.AddCompressionDependency

0 commit comments

Comments
 (0)