Skip to content

Commit

Permalink
chore(codegen): adopt checked dependency imports for TypeScriptWriter (
Browse files Browse the repository at this point in the history
…#6000)

* chore: update smithy hash

* chore: client-dynamodb compatibility with checked imports

* chore: fix document client codegen

* chore: codegen for lib-dynamodb

* chore: codegen sync

* chore: codegen for lib-dynamodb

* chore: update smithy pkg versions and lockfile
  • Loading branch information
kuhe committed Apr 12, 2024
1 parent 2a11640 commit 0ad6f05
Show file tree
Hide file tree
Showing 36 changed files with 249 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ private void writeAdditionalFiles(
writer.write("export * from './pagination';");
writer.write("export * from './$L';", DocumentClientUtils.CLIENT_NAME);
writer.write("export * from './$L';", DocumentClientUtils.CLIENT_FULL_NAME);
writer.write("export { NumberValueImpl as NumberValue } from \"@aws-sdk/util-dynamodb\";");
writer.write("");
writer.write("""
export { NumberValueImpl as NumberValue } from "@aws-sdk/util-dynamodb";
export { marshallOptions, unmarshallOptions } from "@aws-sdk/util-dynamodb";
export { NativeAttributeValue, NativeAttributeBinary, NativeScalarAttributeValue } from "@aws-sdk/util-dynamodb";
""");
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public enum AwsDependency implements Dependency {
// feat(experimentalIdentityAndAuth): Conditionally added when @httpBearerAuth is used in an AWS service
TOKEN_PROVIDERS(NORMAL_DEPENDENCY, "@aws-sdk/token-providers"),
TYPES(NORMAL_DEPENDENCY, "@aws-sdk/types"),
REGION_CONFIG_RESOLVER(NORMAL_DEPENDENCY, "@aws-sdk/region-config-resolver");
REGION_CONFIG_RESOLVER(NORMAL_DEPENDENCY, "@aws-sdk/region-config-resolver"),

CLIENT_DYNAMODB_PEER(PEER_DEPENDENCY, "@aws-sdk/client-dynamodb", "^3.0.0"),
UTIL_DYNAMODB(NORMAL_DEPENDENCY, "@aws-sdk/util-dynamodb", "*");


public final String packageName;
public final String version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private void serializeDocumentBody(
String xmlDeclVar = context.getStringStore().var("<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>");
writer.write("body = $L;", xmlDeclVar);

writer.addImport("XmlNode", "__XmlNode", "@aws-sdk/xml-builder");
writer.addImport("XmlNode", "__XmlNode", AwsDependency.XML_BUILDER);

// Handle the @xmlName trait for the input shape.
StructureShape inputShape = context.getModel().expectShape(inputShapeId, StructureShape.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,12 @@ final class DocumentAggregatedClientGenerator implements Runnable {

@Override
public void run() {
writer.addImport(DocumentClientUtils.CLIENT_NAME,
DocumentClientUtils.CLIENT_NAME, Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString());
// Note: using addImport would register this dependency on the dynamodb client, which must be avoided.
writer.write("""
import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
""");
writer.addRelativeImport(DocumentClientUtils.CLIENT_NAME,
DocumentClientUtils.CLIENT_NAME, Paths.get(".", DocumentClientUtils.CLIENT_NAME));
writer.writeDocs(DocumentClientUtils.getClientDocs());
writer.openBlock("export class $L extends $L {", "}",
DocumentClientUtils.CLIENT_FULL_NAME, DocumentClientUtils.CLIENT_NAME, () -> {
Expand All @@ -77,8 +81,7 @@ public void run() {

private void generateStaticFactoryFrom() {
String translateConfig = DocumentClientUtils.CLIENT_TRANSLATE_CONFIG_TYPE;
writer.addImport(serviceName, serviceName, "@aws-sdk/client-dynamodb");
writer.addImport(translateConfig, translateConfig, Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString());
writer.addRelativeImport(translateConfig, translateConfig, Paths.get(".", DocumentClientUtils.CLIENT_NAME));
writer.openBlock("static from(client: $L, translateConfig?: $L) {", "}",
serviceName, translateConfig, () -> {
writer.write("return new $L(client, translateConfig);", DocumentClientUtils.CLIENT_FULL_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ public void run() {
String serviceOutputTypes = "ServiceOutputTypes";

// Add required imports.
writer.addImport(serviceName, serviceName, "@aws-sdk/client-dynamodb");
writer.addImport(configType, configType, "@aws-sdk/client-dynamodb");
// Note: using addImport would register these dependencies on the dynamodb client, which must be avoided.
writer.write("""
import {
DynamoDBClient,
DynamoDBClientResolvedConfig,
ServiceInputTypes as __ServiceInputTypes,
ServiceOutputTypes as __ServiceOutputTypes,
} from "@aws-sdk/client-dynamodb";
import { marshallOptions, unmarshallOptions } from "@aws-sdk/util-dynamodb";
""");

writer.addImport("Client", "__Client", TypeScriptDependency.AWS_SMITHY_CLIENT);
writer.writeDocs("@public");
writer.write("export { __Client };");
Expand Down Expand Up @@ -101,8 +110,6 @@ public void run() {
}

private void generateInputOutputImports(String serviceInputTypes, String serviceOutputTypes) {
writer.addImport(serviceInputTypes, String.format("__%s", serviceInputTypes), "@aws-sdk/client-dynamodb");
writer.addImport(serviceOutputTypes, String.format("__%s", serviceOutputTypes), "@aws-sdk/client-dynamodb");
Set<OperationShape> containedOperations =
new TreeSet<>(TopDownIndex.of(model).getContainedOperations(service));

Expand Down Expand Up @@ -199,7 +206,6 @@ private void generateConfiguration() {
}

private void generateTranslateConfigOption(String translateOption) {
writer.addImport(translateOption, translateOption, "@aws-sdk/util-dynamodb");
writer.write("${1L}?: ${1L};", translateOption);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@

package software.amazon.smithy.aws.typescript.codegen;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;
import software.amazon.smithy.codegen.core.CodegenException;
import software.amazon.smithy.codegen.core.Symbol;
Expand Down Expand Up @@ -62,6 +65,10 @@ final class DocumentClientCommandGenerator implements Runnable {
private final List<MemberShape> outputMembersWithAttr;
private final String clientCommandClassName;
private final String clientCommandLocalName;
/**
* Map of package name to external:local name entries.
*/
private final Map<String, Map<String, String>> orderedUncheckedImports = new TreeMap<>();

DocumentClientCommandGenerator(
TypeScriptSettings settings,
Expand Down Expand Up @@ -97,12 +104,21 @@ final class DocumentClientCommandGenerator implements Runnable {

@Override
public void run() {
String servicePath = Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString();
Path servicePath = Paths.get(".", DocumentClientUtils.CLIENT_NAME);
String configType = DocumentClientUtils.CLIENT_CONFIG_NAME;

// Note: using addImport would register these dependencies on the dynamodb client, which must be avoided.
writer.write("""
import { %s as %s } from "%s";
""".formatted(
clientCommandClassName,
clientCommandLocalName,
AwsDependency.CLIENT_DYNAMODB_PEER.getPackageName()
)
);

// Add required imports.
writer.addImport(configType, configType, servicePath);
writer.addRelativeImport(configType, configType, servicePath);
writer.addImport(
"DynamoDBDocumentClientCommand",
"DynamoDBDocumentClientCommand",
Expand Down Expand Up @@ -159,6 +175,24 @@ public void run() {
writer.pushState(COMMAND_BODY_EXTRA_SECTION).popState();
}
);

// Note: using addImport would register these dependencies on the dynamodb client, which must be avoided.
writer.write("");
orderedUncheckedImports.forEach((dep, symbols) -> {
writer.openBlock("import type {", """
} from "%s";
""".formatted(dep), () -> {
symbols.forEach((externalName, localName) -> {
if (externalName.equals(localName)) {
writer.writeInline(localName).write(",");
} else {
writer.write("""
%s as %s,
""".formatted(externalName, localName));
}
});
});
});
}

private void generateCommandConstructor() {
Expand All @@ -180,9 +214,9 @@ private void generateCommandMiddlewareResolver(String configType) {
String handler = "Handler";
String middlewareStack = "MiddlewareStack";

String servicePath = Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString();
writer.addImport(serviceInputTypes, serviceInputTypes, servicePath);
writer.addImport(serviceOutputTypes, serviceOutputTypes, servicePath);
Path servicePath = Paths.get(".", DocumentClientUtils.CLIENT_NAME);
writer.addRelativeImport(serviceInputTypes, serviceInputTypes, servicePath);
writer.addRelativeImport(serviceOutputTypes, serviceOutputTypes, servicePath);
writer.addImport(handler, handler, TypeScriptDependency.SMITHY_TYPES);
writer.addImport(middlewareStack, middlewareStack, TypeScriptDependency.SMITHY_TYPES);

Expand All @@ -194,8 +228,6 @@ private void generateCommandMiddlewareResolver(String configType) {
.dedent();
writer.openBlock("): $L<$L, $L> {", "}", handler, inputTypeName, outputTypeName, () -> {

writer.addImport(clientCommandClassName, clientCommandLocalName, "@aws-sdk/client-dynamodb");

String commandVarName = "this.clientCommand";

// marshall middlewares
Expand Down Expand Up @@ -312,7 +344,11 @@ private void writeType(
) {
writer.writeDocs("@public");
if (optionalShape.isPresent()) {
writer.addImport(originalTypeName, "__" + originalTypeName, "@aws-sdk/client-dynamodb");
registerTypeImport(
originalTypeName,
"__" + originalTypeName,
AwsDependency.CLIENT_DYNAMODB_PEER.getPackageName()
);
if (membersWithAttr.isEmpty()) {
writer.write("export type $L = __$L;", typeName, originalTypeName);
} else {
Expand All @@ -339,7 +375,11 @@ private void writeStructureOmitType(StructureShape structureTarget) {
.map(memberWithAttr -> "'" + symbolProvider.toMemberName(memberWithAttr) + "'")
.collect(Collectors.joining(" | "));
String typeNameToOmit = symbolProvider.toSymbol(structureTarget).getName();
writer.addImport(typeNameToOmit, typeNameToOmit, "@aws-sdk/client-dynamodb");
registerTypeImport(
typeNameToOmit,
typeNameToOmit,
AwsDependency.CLIENT_DYNAMODB_PEER.getPackageName()
);
writer.openBlock("Omit<$L, $L> & {", "}", typeNameToOmit,
memberUnionToOmit, () -> {
for (MemberShape memberWithAttr: membersWithAttr) {
Expand Down Expand Up @@ -387,7 +427,11 @@ private void writeMemberOmitType(MemberShape member) {

private void writeNativeAttributeValue() {
String nativeAttributeValue = "NativeAttributeValue";
writer.addImport(nativeAttributeValue, nativeAttributeValue, "@aws-sdk/util-dynamodb");
registerTypeImport(
nativeAttributeValue,
nativeAttributeValue,
AwsDependency.UTIL_DYNAMODB.getPackageName()
);
writer.write(nativeAttributeValue);
}

Expand All @@ -402,4 +446,12 @@ private void writeNativeAttributeValue() {
private boolean isRequiredMember(MemberShape member) {
return member.isRequired() && !member.hasTrait(IdempotencyTokenTrait.class);
}

private void registerTypeImport(String externalName, String localName, String packageName) {
orderedUncheckedImports.putIfAbsent(packageName, new TreeMap<>());
orderedUncheckedImports.get(packageName)
.put(
externalName, localName
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package software.amazon.smithy.aws.typescript.codegen;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import software.amazon.smithy.codegen.core.CodegenException;
Expand Down Expand Up @@ -77,20 +78,20 @@ final class DocumentClientPaginationGenerator implements Runnable {
@Override
public void run() {
// Import Service Types
String commandFileLocation = Paths.get(".", DocumentClientUtils.CLIENT_COMMANDS_FOLDER,
DocumentClientUtils.getModifiedName(operationTypeName)).toString();
writer.addImport(operationTypeName, operationTypeName, commandFileLocation);
writer.addImport(inputTypeName, inputTypeName, commandFileLocation);
writer.addImport(outputTypeName, outputTypeName, commandFileLocation);
writer.addImport(
Path commandFileLocation = Paths.get(".", DocumentClientUtils.CLIENT_COMMANDS_FOLDER,
DocumentClientUtils.getModifiedName(operationTypeName));
writer.addRelativeImport(operationTypeName, operationTypeName, commandFileLocation);
writer.addRelativeImport(inputTypeName, inputTypeName, commandFileLocation);
writer.addRelativeImport(outputTypeName, outputTypeName, commandFileLocation);
writer.addRelativeImport(
DocumentClientUtils.CLIENT_NAME,
DocumentClientUtils.CLIENT_NAME,
Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString());
Paths.get(".", DocumentClientUtils.CLIENT_NAME));

// Import Pagination types
writer.addImport("Paginator", "Paginator", TypeScriptDependency.SMITHY_TYPES);
writer.addImport(paginationType, paginationType,
Paths.get(".", getInterfaceFilelocation().replace(".ts", "")).toString());
writer.addRelativeImport(paginationType, paginationType,
Paths.get(".", getInterfaceFilelocation().replace(".ts", "")));

writer.writeDocs("@public");
writer.write("export { Paginator }");
Expand All @@ -113,14 +114,14 @@ static String getInterfaceFilelocation() {
static void generateServicePaginationInterfaces(TypeScriptWriter writer) {
writer.addImport("PaginationConfiguration", "PaginationConfiguration", TypeScriptDependency.SMITHY_TYPES);

writer.addImport(
writer.addRelativeImport(
DocumentClientUtils.CLIENT_NAME,
DocumentClientUtils.CLIENT_NAME,
Paths.get(".", DocumentClientUtils.CLIENT_NAME).toString());
writer.addImport(
Paths.get(".", DocumentClientUtils.CLIENT_NAME));
writer.addRelativeImport(
DocumentClientUtils.CLIENT_FULL_NAME,
DocumentClientUtils.CLIENT_FULL_NAME,
Paths.get(".", DocumentClientUtils.CLIENT_FULL_NAME).toString());
Paths.get(".", DocumentClientUtils.CLIENT_FULL_NAME));

writer.writeDocs("@public");
writer.write("export { PaginationConfiguration };");
Expand Down
19 changes: 10 additions & 9 deletions lib/lib-dynamodb/src/commands/BatchExecuteStatementCommand.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
// smithy-typescript generated code
import {
BatchExecuteStatementCommand as __BatchExecuteStatementCommand,
BatchExecuteStatementCommandInput as __BatchExecuteStatementCommandInput,
BatchExecuteStatementCommandOutput as __BatchExecuteStatementCommandOutput,
BatchStatementError,
BatchStatementRequest,
BatchStatementResponse,
} from "@aws-sdk/client-dynamodb";
import { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
import { BatchExecuteStatementCommand as __BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb";
import { Command as $Command } from "@smithy/smithy-client";
import { Handler, HttpHandlerOptions as __HttpHandlerOptions, MiddlewareStack } from "@smithy/types";

Expand Down Expand Up @@ -104,3 +96,12 @@ export class BatchExecuteStatementCommand extends DynamoDBDocumentClientCommand<
return async () => handler(this.clientCommand);
}
}

import type {
BatchExecuteStatementCommandInput as __BatchExecuteStatementCommandInput,
BatchExecuteStatementCommandOutput as __BatchExecuteStatementCommandOutput,
BatchStatementError,
BatchStatementRequest,
BatchStatementResponse,
} from "@aws-sdk/client-dynamodb";
import type { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
15 changes: 8 additions & 7 deletions lib/lib-dynamodb/src/commands/BatchGetCommand.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
// smithy-typescript generated code
import {
BatchGetItemCommand as __BatchGetItemCommand,
BatchGetItemCommandInput as __BatchGetItemCommandInput,
BatchGetItemCommandOutput as __BatchGetItemCommandOutput,
KeysAndAttributes,
} from "@aws-sdk/client-dynamodb";
import { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
import { BatchGetItemCommand as __BatchGetItemCommand } from "@aws-sdk/client-dynamodb";
import { Command as $Command } from "@smithy/smithy-client";
import { Handler, HttpHandlerOptions as __HttpHandlerOptions, MiddlewareStack } from "@smithy/types";

Expand Down Expand Up @@ -112,3 +106,10 @@ export class BatchGetCommand extends DynamoDBDocumentClientCommand<
return async () => handler(this.clientCommand);
}
}

import type {
BatchGetItemCommandInput as __BatchGetItemCommandInput,
BatchGetItemCommandOutput as __BatchGetItemCommandOutput,
KeysAndAttributes,
} from "@aws-sdk/client-dynamodb";
import type { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
21 changes: 11 additions & 10 deletions lib/lib-dynamodb/src/commands/BatchWriteCommand.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
// smithy-typescript generated code
import {
BatchWriteItemCommand as __BatchWriteItemCommand,
BatchWriteItemCommandInput as __BatchWriteItemCommandInput,
BatchWriteItemCommandOutput as __BatchWriteItemCommandOutput,
DeleteRequest,
ItemCollectionMetrics,
PutRequest,
WriteRequest,
} from "@aws-sdk/client-dynamodb";
import { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
import { BatchWriteItemCommand as __BatchWriteItemCommand } from "@aws-sdk/client-dynamodb";
import { Command as $Command } from "@smithy/smithy-client";
import { Handler, HttpHandlerOptions as __HttpHandlerOptions, MiddlewareStack } from "@smithy/types";

Expand Down Expand Up @@ -145,3 +136,13 @@ export class BatchWriteCommand extends DynamoDBDocumentClientCommand<
return async () => handler(this.clientCommand);
}
}

import type {
BatchWriteItemCommandInput as __BatchWriteItemCommandInput,
BatchWriteItemCommandOutput as __BatchWriteItemCommandOutput,
DeleteRequest,
ItemCollectionMetrics,
PutRequest,
WriteRequest,
} from "@aws-sdk/client-dynamodb";
import type { NativeAttributeValue } from "@aws-sdk/util-dynamodb";
Loading

0 comments on commit 0ad6f05

Please sign in to comment.