Skip to content

Commit

Permalink
use aggregated client runtime generator (#736)
Browse files Browse the repository at this point in the history
* use aggregated client runtime generator

* generate aggregated client in static context instead of instance ctor

* create method for getting config type name

* use getConfigTypeName in ServiceBareBonesClientGenerator

* line length
  • Loading branch information
kuhe committed Apr 19, 2023
1 parent a526a73 commit ffaf2fc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
Expand Up @@ -67,11 +67,18 @@ final class ServiceAggregatedClientGenerator implements Runnable {
@Override
public void run() {
TopDownIndex topDownIndex = TopDownIndex.of(model);
final Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
writer.openBlock("const commands = {", "}", () -> {
for (OperationShape operation : containedOperations) {
Symbol operationSymbol = symbolProvider.toSymbol(operation);
writer.write("$L,", operationSymbol.getName());
}
});

// Generate the client and extend from the bare-bones client.
writer.writeShapeDocs(service);
writer.openBlock("export class $L extends $T {", "}", aggregateClientName, serviceSymbol, () -> {
Set<OperationShape> containedOperations = new TreeSet<>(topDownIndex.getContainedOperations(service));
writer.write("");

// Generate an aggregated client interface.
writer.openBlock("export interface $L {", "}", aggregateClientName, () -> {
for (OperationShape operation : containedOperations) {
Symbol operationSymbol = symbolProvider.toSymbol(operation);
Symbol input = operationSymbol.expectProperty("inputType", Symbol.class);
Expand All @@ -83,43 +90,41 @@ public void run() {
);

// Generate a multiple overloaded methods for each command.
writer.writeShapeDocs(operation);
writer.write("public $L(\n"
writer.writeDocs(
"@see {@link " + operationSymbol.getName() + "}"
);
writer.write("$L(\n"
+ " args: $T,\n"
+ " options?: $T,\n"
+ "): Promise<$T>;", methodName, input, applicationProtocol.getOptionsType(), output);
writer.write("public $L(\n"
writer.write("$L(\n"
+ " args: $T,\n"
+ " cb: (err: any, data?: $T) => void\n"
+ "): void;", methodName, input, output);
writer.write("public $L(\n"
writer.write("$L(\n"
+ " args: $T,\n"
+ " options: $T,\n"
+ " cb: (err: any, data?: $T) => void\n"
+ "): void;", methodName, input, applicationProtocol.getOptionsType(), output);
writer.openBlock("public $1L(\n"
+ " args: $2T,\n"
+ " optionsOrCb?: $3T | ((err: any, data?: $4T) => void),\n"
+ " cb?: (err: any, data?: $4T) => void\n"
+ "): Promise<$4T> | void { ", "}",
methodName,
input,
applicationProtocol.getOptionsType(),
output,
() -> {
writer.write("const command = new $T(args);\n"
+ "if (typeof optionsOrCb === \"function\") {\n"
+ " this.send(command, optionsOrCb)\n"
+ "} else if (typeof cb === \"function\") {\n"
+ " if (typeof optionsOrCb !== \"object\")\n"
+ " throw new Error(`Expect http options but get $${typeof optionsOrCb}`)\n"
+ " this.send(command, optionsOrCb || {}, cb)\n"
+ "} else {\n"
+ " return this.send(command, optionsOrCb);\n"
+ "}", operationSymbol);
});
writer.write("");
}
});

writer.write("");

writer.addImport(
ServiceBareBonesClientGenerator.getConfigTypeName(serviceSymbol),
null,
serviceSymbol.getNamespace()
);

// Generate the client and extend from the bare-bones client.
writer.writeShapeDocs(service);
writer.write("export class $L extends $T implements $L {}",
aggregateClientName, serviceSymbol, aggregateClientName
);

writer.addImport("createAggregatedClient", null, "@aws-sdk/smithy-client");
writer.write("createAggregatedClient(commands, $L);", aggregateClientName);
}
}
Expand Up @@ -83,10 +83,14 @@ final class ServiceBareBonesClientGenerator implements Runnable {
this.applicationProtocol = applicationProtocol;

symbol = symbolProvider.toSymbol(service);
configType = symbol.getName() + "Config";
configType = getConfigTypeName(symbol);
resolvedConfigType = getResolvedConfigTypeName(symbol);
}

static String getConfigTypeName(Symbol symbol) {
return symbol.getName() + "Config";
}

static String getResolvedConfigTypeName(Symbol symbol) {
return symbol.getName() + "ResolvedConfig";
}
Expand Down

0 comments on commit ffaf2fc

Please sign in to comment.