Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(codegen): accomodate optional inputs in lib-dynamodb generator #5904

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.smithy.codegen.core.SymbolReference;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.knowledge.TopDownIndex;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.OperationShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.typescript.codegen.ApplicationProtocol;
Expand Down Expand Up @@ -113,40 +114,49 @@ private void generateOperations() {

// Generate a multiple overloaded methods for each command.
writer.writeDocs(DocumentClientUtils.getCommandDocs(operationSymbol.getName()));
writer.write("public $L(\n"
+ " args: $L,\n"
+ " options?: $T,\n"
+ "): Promise<$L>;", methodName, input, options, output);
writer.write("public $L(\n"
+ " args: $L,\n"
+ " cb: (err: any, data?: $L) => void\n"
+ "): void;", methodName, input, output);
writer.write("public $L(\n"
+ " args: $L,\n"
+ " options: $T,\n"
+ " cb: (err: any, data?: $L) => void\n"
+ "): void;", methodName, input, options, output);
writer.openBlock("public $1L(\n"
+ " args: $2L,\n"
+ " optionsOrCb?: $3T | ((err: any, data?: $4L) => void),\n"
+ " cb?: (err: any, data?: $4L) => void\n"
+ "): Promise<$4L> | void { ", "}",
methodName,
input,
options,
output,
() -> {
writer.write("const command = new $L(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"
+ "}", name);
});
boolean inputOptional = model.getShape(operation.getInputShape()).map(
shape -> shape.getAllMembers().values().stream().noneMatch(MemberShape::isRequired)
).orElse(true);
if (inputOptional) {
writer.write("$L(): Promise<$T>;", methodName, output);
}
writer.write("""
public $1L(
args: $2L,
options?: $3T,
): Promise<$4L>;
public $1L(
args: $2L,
cb: (err: any, data?: $4L) => void
): void;
public $1L(
args: $2L,
options: $3T,
cb: (err: any, data?: $4L) => void
): void;
public $1L(
args: $2L,
optionsOrCb?: $3T | ((err: any, data?: $4L) => void),
cb?: (err: any, data?: $4L) => void
): Promise<$4L> | void {
const command = new $5L(args);
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get $${typeof optionsOrCb}`)
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
}
}""",
methodName,
input,
options,
output,
name
);
writer.write("");
}
}
Expand Down
52 changes: 39 additions & 13 deletions lib/lib-dynamodb/src/DynamoDBDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -143,7 +145,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -173,7 +177,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -203,7 +209,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -239,7 +247,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -275,7 +285,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -305,7 +317,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -335,7 +349,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -365,7 +381,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -395,7 +413,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -425,7 +445,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -461,7 +483,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down Expand Up @@ -491,7 +515,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
if (typeof optionsOrCb === "function") {
this.send(command, optionsOrCb);
} else if (typeof cb === "function") {
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
if (typeof optionsOrCb !== "object") {
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
}
this.send(command, optionsOrCb || {}, cb);
} else {
return this.send(command, optionsOrCb);
Expand Down
Loading