-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(codegen): add sigv4a trait detection
- Loading branch information
Showing
2 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
...-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddSigv4aPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.smithy.aws.typescript.codegen; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import software.amazon.smithy.codegen.core.SymbolProvider; | ||
import software.amazon.smithy.model.Model; | ||
import software.amazon.smithy.model.knowledge.ServiceIndex; | ||
import software.amazon.smithy.model.shapes.ServiceShape; | ||
import software.amazon.smithy.model.shapes.ShapeId; | ||
import software.amazon.smithy.model.traits.Trait; | ||
import software.amazon.smithy.typescript.codegen.LanguageTarget; | ||
import software.amazon.smithy.typescript.codegen.TypeScriptSettings; | ||
import software.amazon.smithy.typescript.codegen.TypeScriptWriter; | ||
import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; | ||
import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
||
/** | ||
* Detects when to add sigv4a signer. | ||
*/ | ||
@SmithyInternalApi | ||
public final class AddSigv4aPlugin implements TypeScriptIntegration { | ||
public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters( | ||
TypeScriptSettings settings, | ||
Model model, | ||
SymbolProvider symbolProvider, | ||
LanguageTarget target | ||
) { | ||
boolean useSigv4aSigner = false; | ||
|
||
ServiceIndex serviceIndex = ServiceIndex.of(model); | ||
Set<ServiceShape> services = model.getServiceShapes(); | ||
for (ServiceShape service of services) { | ||
Map<ShapeId, Trait> authSchemes = serviceIndex.getAuthSchemes(service); | ||
useSigv4aSigner = useSigv4aSigner || authSchemes.values().stream() | ||
.anyMatch(trait -> trait.toString().equals("aws.auth#sigv4a")); | ||
|
||
TopDownIndex topDownIndex = TopDownIndex.of(service.getModel()); | ||
for (OperationShape operationShape : topDownIndex.getContainedOperations(service)) { | ||
useSigv4aSigner = useSigv4aSigner || operationShape.getAllTraits() | ||
.values().stream() | ||
.anyMatch(trait -> trait.toString().equals("aws.auth#sigv4a")); | ||
} | ||
} | ||
if (!useSigv4aSigner) { | ||
return Collections.emptyMap(); | ||
} | ||
switch (target) { | ||
case SHARED: | ||
return MapUtils.of("signerConstructor", writer -> { | ||
writer.addDependency(AwsDependency.SIGNATURE_V4_MULTIREGION) | ||
.addImport("SignatureV4MultiRegion", "SignatureV4MultiRegion", | ||
AwsDependency.SIGNATURE_V4_MULTIREGION) | ||
.write("SignatureV4MultiRegion"); | ||
}); | ||
default: | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters