Skip to content

Commit

Permalink
chore(codegen): refactor addImport usage (#4986)
Browse files Browse the repository at this point in the history
  • Loading branch information
srchase authored Jul 19, 2023
1 parent 7ce1427 commit 454c2d0
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,14 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
if (!testServiceId(service, "STS")) {
writer.addDependency(AwsDependency.STS_CLIENT);
writer.addImport("decorateDefaultCredentialProvider", "decorateDefaultCredentialProvider",
AwsDependency.STS_CLIENT.packageName);
AwsDependency.STS_CLIENT);
} else {
writer.addImport("decorateDefaultCredentialProvider", "decorateDefaultCredentialProvider",
Paths.get(".", CodegenUtils.SOURCE_FOLDER, STS_ROLE_ASSUMERS_FILE).toString());
writer.addRelativeImport("decorateDefaultCredentialProvider", "decorateDefaultCredentialProvider",
Paths.get(".", CodegenUtils.SOURCE_FOLDER, STS_ROLE_ASSUMERS_FILE));
}
writer.addDependency(AwsDependency.CREDENTIAL_PROVIDER_NODE);
writer.addImport("defaultProvider", "credentialDefaultProvider",
AwsDependency.CREDENTIAL_PROVIDER_NODE.packageName);
AwsDependency.CREDENTIAL_PROVIDER_NODE);
writer.write("decorateDefaultCredentialProvider(credentialDefaultProvider)");
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ private Map<String, Consumer<TypeScriptWriter>> getDefaultConfig(
return MapUtils.of("region", writer -> {
writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY);
writer.addImport("invalidProvider", "invalidProvider",
TypeScriptDependency.INVALID_DEPENDENCY.packageName);
TypeScriptDependency.INVALID_DEPENDENCY);
writer.write("invalidProvider(\"Region is missing\")");
});
case NODE:
return MapUtils.of("region", writer -> {
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName);
TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("NODE_REGION_CONFIG_OPTIONS", "NODE_REGION_CONFIG_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("NODE_REGION_CONFIG_FILE_OPTIONS", "NODE_REGION_CONFIG_FILE_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
TypeScriptDependency.CONFIG_RESOLVER);
writer.write(
"loadNodeConfig(NODE_REGION_CONFIG_OPTIONS, NODE_REGION_CONFIG_FILE_OPTIONS)");
});
Expand All @@ -174,13 +174,13 @@ private Map<String, Consumer<TypeScriptWriter>> getEndpointConfigWriters(
"useDualstackEndpoint", writer -> {
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("DEFAULT_USE_DUALSTACK_ENDPOINT", "DEFAULT_USE_DUALSTACK_ENDPOINT",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
TypeScriptDependency.CONFIG_RESOLVER);
writer.write("(() => Promise.resolve(DEFAULT_USE_DUALSTACK_ENDPOINT))");
},
"useFipsEndpoint", writer -> {
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("DEFAULT_USE_FIPS_ENDPOINT", "DEFAULT_USE_FIPS_ENDPOINT",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
TypeScriptDependency.CONFIG_RESOLVER);
writer.write("(() => Promise.resolve(DEFAULT_USE_FIPS_ENDPOINT))");
}
);
Expand All @@ -189,21 +189,21 @@ private Map<String, Consumer<TypeScriptWriter>> getEndpointConfigWriters(
"useDualstackEndpoint", writer -> {
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName);
TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS",
"NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
TypeScriptDependency.CONFIG_RESOLVER);
writer.write("loadNodeConfig(NODE_USE_DUALSTACK_ENDPOINT_CONFIG_OPTIONS)");
},
"useFipsEndpoint", writer -> {
writer.addDependency(TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addImport("loadConfig", "loadNodeConfig",
TypeScriptDependency.NODE_CONFIG_PROVIDER.packageName);
TypeScriptDependency.NODE_CONFIG_PROVIDER);
writer.addDependency(TypeScriptDependency.CONFIG_RESOLVER);
writer.addImport("NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS",
"NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS",
TypeScriptDependency.CONFIG_RESOLVER.packageName);
TypeScriptDependency.CONFIG_RESOLVER);
writer.write("loadNodeConfig(NODE_USE_FIPS_ENDPOINT_CONFIG_OPTIONS)");
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
return MapUtils.of("bodyChecksumGenerator", writer -> {
writer.addDependency(AwsDependency.BODY_CHECKSUM_GENERATOR_NODE);
writer.addImport("bodyChecksumGenerator", "bodyChecksumGenerator",
AwsDependency.BODY_CHECKSUM_GENERATOR_NODE.packageName);
AwsDependency.BODY_CHECKSUM_GENERATOR_NODE);
writer.write("bodyChecksumGenerator");
});
case BROWSER:
return MapUtils.of("bodyChecksumGenerator", writer -> {
writer.addDependency(AwsDependency.BODY_CHECKSUM_GENERATOR_BROWSER);
writer.addImport("bodyChecksumGenerator", "bodyChecksumGenerator",
AwsDependency.BODY_CHECKSUM_GENERATOR_BROWSER.packageName);
AwsDependency.BODY_CHECKSUM_GENERATOR_BROWSER);
writer.write("bodyChecksumGenerator");
});
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void addConfigInterfaceFields(
) {
if (hasEventStreamInput(model, settings.getService(model))) {
writer.addImport("EventStreamPayloadHandlerProvider", "__EventStreamPayloadHandlerProvider",
TypeScriptDependency.AWS_SDK_TYPES.packageName);
TypeScriptDependency.AWS_SDK_TYPES);
writer.writeDocs("The function that provides necessary utilities for handling request event stream.\n"
+ "@internal");
writer.write("eventStreamPayloadHandlerProvider?: __EventStreamPayloadHandlerProvider;\n");
Expand All @@ -94,7 +94,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
return MapUtils.of("eventStreamPayloadHandlerProvider", writer -> {
writer.addDependency(AwsDependency.AWS_SDK_EVENTSTREAM_HANDLER_NODE);
writer.addImport("eventStreamPayloadHandlerProvider", "eventStreamPayloadHandlerProvider",
AwsDependency.AWS_SDK_EVENTSTREAM_HANDLER_NODE.packageName);
AwsDependency.AWS_SDK_EVENTSTREAM_HANDLER_NODE);
writer.write("eventStreamPayloadHandlerProvider");
});
case BROWSER:
Expand All @@ -107,7 +107,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
return MapUtils.of("eventStreamPayloadHandlerProvider", writer -> {
writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY);
writer.addImport("invalidFunction", "invalidFunction",
TypeScriptDependency.INVALID_DEPENDENCY.packageName);
TypeScriptDependency.INVALID_DEPENDENCY);
writer.openBlock("(() => ({", "}))", () -> {
writer.write("handle: invalidFunction(\"event stream request is not supported in browser.\"),");
});
Expand All @@ -121,7 +121,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
return MapUtils.of("eventStreamPayloadHandlerProvider", writer -> {
writer.addDependency(TypeScriptDependency.INVALID_DEPENDENCY);
writer.addImport("invalidFunction", "invalidFunction",
TypeScriptDependency.INVALID_DEPENDENCY.packageName);
TypeScriptDependency.INVALID_DEPENDENCY);
writer.openBlock("(() => ({", "}))", () -> {
writer.write("handle: invalidFunction(\"event stream request "
+ "is not supported in ReactNative.\"),");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Map<String, Consumer<TypeScriptWriter>> getRuntimeConfigWriters(
return MapUtils.of("requestHandler", writer -> {
writer.addDependency(TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER);
writer.addImport("NodeHttp2Handler", "RequestHandler",
TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER.packageName);
TypeScriptDependency.AWS_SDK_NODE_HTTP_HANDLER);
writer.openBlock("new RequestHandler(async () => ({", "}))", () -> {
writer.write("...await defaultConfigProvider(),");
// TODO: remove this when root cause of #3809 is found
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Properties;
import software.amazon.smithy.codegen.core.SymbolDependency;
import software.amazon.smithy.codegen.core.SymbolDependencyContainer;
import software.amazon.smithy.typescript.codegen.PackageContainer;
import software.amazon.smithy.utils.IoUtils;
import software.amazon.smithy.utils.SmithyInternalApi;

Expand All @@ -35,7 +36,7 @@
* this package.
*/
@SmithyInternalApi
public enum AwsDependency implements SymbolDependencyContainer {
public enum AwsDependency implements PackageContainer, SymbolDependencyContainer {

MIDDLEWARE_SIGNING(NORMAL_DEPENDENCY, "@aws-sdk/middleware-signing"),
MIDDLEWARE_TOKEN(NORMAL_DEPENDENCY, "@aws-sdk/middleware-token"),
Expand Down Expand Up @@ -104,6 +105,11 @@ public List<SymbolDependency> getDependencies() {
return Collections.singletonList(dependency);
}

@Override
public String getPackageName() {
return this.packageName;
}

private static final class SdkVersion {
private static final Map<String, String> VERSIONS;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ static void generateXmlParseBody(GenerationContext context) {
// Include an XML body parser used to deserialize documents from HTTP responses.
writer.addImport("SerdeContext", "__SerdeContext", TypeScriptDependency.SMITHY_TYPES);
writer.addImport("getValueFromTextNode", "__getValueFromTextNode", TypeScriptDependency.AWS_SMITHY_CLIENT);
writer.addDependency(AwsDependency.XML_PARSER);
writer.addImport("XMLParser", null, "fast-xml-parser");
writer.addDependency(TypeScriptDependency.XML_PARSER);
writer.addImport("XMLParser", null, TypeScriptDependency.XML_PARSER);
writer.openBlock("const parseBody = (streamBody: any, context: __SerdeContext): "
+ "any => collectBodyString(streamBody, context).then(encoded => {", "});", () -> {
writer.openBlock("if (encoded.length) {", "}", () -> {
Expand Down Expand Up @@ -298,9 +298,9 @@ static void addItempotencyAutofillImport(GenerationContext context) {
TypeScriptWriter writer = context.getWriter();

// Include the uuid package and import the v4 function as our more clearly named alias.
writer.addDependency(AwsDependency.UUID_GENERATOR);
writer.addDependency(TypeScriptDependency.UUID);
writer.addDependency(AwsDependency.UUID_GENERATOR_TYPES);
writer.addImport("v4", "generateIdempotencyToken", "uuid");
writer.addImport("v4", "generateIdempotencyToken", TypeScriptDependency.UUID);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void run() {
}

private void writeRegionHash() {
writer.addImport("RegionHash", "RegionHash", TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.addImport("RegionHash", "RegionHash", TypeScriptDependency.CONFIG_RESOLVER);
writer.openBlock("const regionHash: RegionHash = {", "};", () -> {
for (Map.Entry<String, ObjectNode> entry : endpoints.entrySet()) {
writeEndpointSpecificResolver(entry.getKey(), entry.getValue());
Expand All @@ -158,7 +158,7 @@ private void writeRegionHash() {
}

private void writePartitionHash() {
writer.addImport("PartitionHash", "PartitionHash", TypeScriptDependency.CONFIG_RESOLVER.packageName);
writer.addImport("PartitionHash", "PartitionHash", TypeScriptDependency.CONFIG_RESOLVER);
writer.openBlock("const partitionHash: PartitionHash = {", "};", () -> {
partitions.values().forEach(partition -> {
writer.openBlock("$S: {", "},", partition.identifier, () -> {
Expand All @@ -178,10 +178,10 @@ private void writePartitionHash() {
}

private void writeEndpointProviderFunction() {
writer.addImport("RegionInfoProvider", "RegionInfoProvider", TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.addImport("RegionInfoProvider", "RegionInfoProvider", TypeScriptDependency.AWS_SDK_TYPES);
writer.addImport("RegionInfoProviderOptions", "RegionInfoProviderOptions",
TypeScriptDependency.AWS_SDK_TYPES.packageName);
writer.addImport("getRegionInfo", "getRegionInfo", TypeScriptDependency.CONFIG_RESOLVER.packageName);
TypeScriptDependency.AWS_SDK_TYPES);
writer.addImport("getRegionInfo", "getRegionInfo", TypeScriptDependency.CONFIG_RESOLVER);
writer.openBlock("export const defaultRegionInfoProvider: RegionInfoProvider = async (\n"
+ " region: string,\n"
+ " options?: RegionInfoProviderOptions\n"
Expand Down

0 comments on commit 454c2d0

Please sign in to comment.