Skip to content

Commit 584dcdf

Browse files
authoredMar 15, 2024··
chore(codegen): accomodate optional inputs in lib-dynamodb generator (#5904)
1 parent 74b4812 commit 584dcdf

File tree

2 files changed

+83
-47
lines changed

2 files changed

+83
-47
lines changed
 

‎codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/DocumentAggregatedClientGenerator.java

+44-34
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import software.amazon.smithy.codegen.core.SymbolReference;
2424
import software.amazon.smithy.model.Model;
2525
import software.amazon.smithy.model.knowledge.TopDownIndex;
26+
import software.amazon.smithy.model.shapes.MemberShape;
2627
import software.amazon.smithy.model.shapes.OperationShape;
2728
import software.amazon.smithy.model.shapes.ServiceShape;
2829
import software.amazon.smithy.typescript.codegen.ApplicationProtocol;
@@ -113,40 +114,49 @@ private void generateOperations() {
113114

114115
// Generate a multiple overloaded methods for each command.
115116
writer.writeDocs(DocumentClientUtils.getCommandDocs(operationSymbol.getName()));
116-
writer.write("public $L(\n"
117-
+ " args: $L,\n"
118-
+ " options?: $T,\n"
119-
+ "): Promise<$L>;", methodName, input, options, output);
120-
writer.write("public $L(\n"
121-
+ " args: $L,\n"
122-
+ " cb: (err: any, data?: $L) => void\n"
123-
+ "): void;", methodName, input, output);
124-
writer.write("public $L(\n"
125-
+ " args: $L,\n"
126-
+ " options: $T,\n"
127-
+ " cb: (err: any, data?: $L) => void\n"
128-
+ "): void;", methodName, input, options, output);
129-
writer.openBlock("public $1L(\n"
130-
+ " args: $2L,\n"
131-
+ " optionsOrCb?: $3T | ((err: any, data?: $4L) => void),\n"
132-
+ " cb?: (err: any, data?: $4L) => void\n"
133-
+ "): Promise<$4L> | void { ", "}",
134-
methodName,
135-
input,
136-
options,
137-
output,
138-
() -> {
139-
writer.write("const command = new $L(args);\n"
140-
+ "if (typeof optionsOrCb === \"function\") {\n"
141-
+ " this.send(command, optionsOrCb)\n"
142-
+ "} else if (typeof cb === \"function\") {\n"
143-
+ " if (typeof optionsOrCb !== \"object\")\n"
144-
+ " throw new Error(`Expect http options but get $${typeof optionsOrCb}`)\n"
145-
+ " this.send(command, optionsOrCb || {}, cb)\n"
146-
+ "} else {\n"
147-
+ " return this.send(command, optionsOrCb);\n"
148-
+ "}", name);
149-
});
117+
boolean inputOptional = model.getShape(operation.getInputShape()).map(
118+
shape -> shape.getAllMembers().values().stream().noneMatch(MemberShape::isRequired)
119+
).orElse(true);
120+
if (inputOptional) {
121+
writer.write("$L(): Promise<$T>;", methodName, output);
122+
}
123+
writer.write("""
124+
public $1L(
125+
args: $2L,
126+
options?: $3T,
127+
): Promise<$4L>;
128+
public $1L(
129+
args: $2L,
130+
cb: (err: any, data?: $4L) => void
131+
): void;
132+
public $1L(
133+
args: $2L,
134+
options: $3T,
135+
cb: (err: any, data?: $4L) => void
136+
): void;
137+
public $1L(
138+
args: $2L,
139+
optionsOrCb?: $3T | ((err: any, data?: $4L) => void),
140+
cb?: (err: any, data?: $4L) => void
141+
): Promise<$4L> | void {
142+
const command = new $5L(args);
143+
if (typeof optionsOrCb === "function") {
144+
this.send(command, optionsOrCb);
145+
} else if (typeof cb === "function") {
146+
if (typeof optionsOrCb !== "object") {
147+
throw new Error(`Expect http options but get $${typeof optionsOrCb}`)
148+
}
149+
this.send(command, optionsOrCb || {}, cb);
150+
} else {
151+
return this.send(command, optionsOrCb);
152+
}
153+
}""",
154+
methodName,
155+
input,
156+
options,
157+
output,
158+
name
159+
);
150160
writer.write("");
151161
}
152162
}

‎lib/lib-dynamodb/src/DynamoDBDocument.ts

+39-13
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
113113
if (typeof optionsOrCb === "function") {
114114
this.send(command, optionsOrCb);
115115
} else if (typeof cb === "function") {
116-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
116+
if (typeof optionsOrCb !== "object") {
117+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
118+
}
117119
this.send(command, optionsOrCb || {}, cb);
118120
} else {
119121
return this.send(command, optionsOrCb);
@@ -143,7 +145,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
143145
if (typeof optionsOrCb === "function") {
144146
this.send(command, optionsOrCb);
145147
} else if (typeof cb === "function") {
146-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
148+
if (typeof optionsOrCb !== "object") {
149+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
150+
}
147151
this.send(command, optionsOrCb || {}, cb);
148152
} else {
149153
return this.send(command, optionsOrCb);
@@ -173,7 +177,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
173177
if (typeof optionsOrCb === "function") {
174178
this.send(command, optionsOrCb);
175179
} else if (typeof cb === "function") {
176-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
180+
if (typeof optionsOrCb !== "object") {
181+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
182+
}
177183
this.send(command, optionsOrCb || {}, cb);
178184
} else {
179185
return this.send(command, optionsOrCb);
@@ -203,7 +209,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
203209
if (typeof optionsOrCb === "function") {
204210
this.send(command, optionsOrCb);
205211
} else if (typeof cb === "function") {
206-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
212+
if (typeof optionsOrCb !== "object") {
213+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
214+
}
207215
this.send(command, optionsOrCb || {}, cb);
208216
} else {
209217
return this.send(command, optionsOrCb);
@@ -239,7 +247,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
239247
if (typeof optionsOrCb === "function") {
240248
this.send(command, optionsOrCb);
241249
} else if (typeof cb === "function") {
242-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
250+
if (typeof optionsOrCb !== "object") {
251+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
252+
}
243253
this.send(command, optionsOrCb || {}, cb);
244254
} else {
245255
return this.send(command, optionsOrCb);
@@ -275,7 +285,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
275285
if (typeof optionsOrCb === "function") {
276286
this.send(command, optionsOrCb);
277287
} else if (typeof cb === "function") {
278-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
288+
if (typeof optionsOrCb !== "object") {
289+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
290+
}
279291
this.send(command, optionsOrCb || {}, cb);
280292
} else {
281293
return this.send(command, optionsOrCb);
@@ -305,7 +317,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
305317
if (typeof optionsOrCb === "function") {
306318
this.send(command, optionsOrCb);
307319
} else if (typeof cb === "function") {
308-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
320+
if (typeof optionsOrCb !== "object") {
321+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
322+
}
309323
this.send(command, optionsOrCb || {}, cb);
310324
} else {
311325
return this.send(command, optionsOrCb);
@@ -335,7 +349,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
335349
if (typeof optionsOrCb === "function") {
336350
this.send(command, optionsOrCb);
337351
} else if (typeof cb === "function") {
338-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
352+
if (typeof optionsOrCb !== "object") {
353+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
354+
}
339355
this.send(command, optionsOrCb || {}, cb);
340356
} else {
341357
return this.send(command, optionsOrCb);
@@ -365,7 +381,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
365381
if (typeof optionsOrCb === "function") {
366382
this.send(command, optionsOrCb);
367383
} else if (typeof cb === "function") {
368-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
384+
if (typeof optionsOrCb !== "object") {
385+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
386+
}
369387
this.send(command, optionsOrCb || {}, cb);
370388
} else {
371389
return this.send(command, optionsOrCb);
@@ -395,7 +413,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
395413
if (typeof optionsOrCb === "function") {
396414
this.send(command, optionsOrCb);
397415
} else if (typeof cb === "function") {
398-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
416+
if (typeof optionsOrCb !== "object") {
417+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
418+
}
399419
this.send(command, optionsOrCb || {}, cb);
400420
} else {
401421
return this.send(command, optionsOrCb);
@@ -425,7 +445,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
425445
if (typeof optionsOrCb === "function") {
426446
this.send(command, optionsOrCb);
427447
} else if (typeof cb === "function") {
428-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
448+
if (typeof optionsOrCb !== "object") {
449+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
450+
}
429451
this.send(command, optionsOrCb || {}, cb);
430452
} else {
431453
return this.send(command, optionsOrCb);
@@ -461,7 +483,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
461483
if (typeof optionsOrCb === "function") {
462484
this.send(command, optionsOrCb);
463485
} else if (typeof cb === "function") {
464-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
486+
if (typeof optionsOrCb !== "object") {
487+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
488+
}
465489
this.send(command, optionsOrCb || {}, cb);
466490
} else {
467491
return this.send(command, optionsOrCb);
@@ -491,7 +515,9 @@ export class DynamoDBDocument extends DynamoDBDocumentClient {
491515
if (typeof optionsOrCb === "function") {
492516
this.send(command, optionsOrCb);
493517
} else if (typeof cb === "function") {
494-
if (typeof optionsOrCb !== "object") throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
518+
if (typeof optionsOrCb !== "object") {
519+
throw new Error(`Expect http options but get ${typeof optionsOrCb}`);
520+
}
495521
this.send(command, optionsOrCb || {}, cb);
496522
} else {
497523
return this.send(command, optionsOrCb);

0 commit comments

Comments
 (0)
Please sign in to comment.