Skip to content

Commit

Permalink
chore(codegen): accomodate optional inputs in lib-dynamodb generator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhe authored Mar 15, 2024
1 parent 74b4812 commit 584dcdf
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 47 deletions.
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

0 comments on commit 584dcdf

Please sign in to comment.