Skip to content

Commit d3774d5

Browse files
chore: reduce code for bools/numbers
1 parent b69398b commit d3774d5

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import software.amazon.smithy.model.shapes.DocumentShape;
2424
import software.amazon.smithy.model.shapes.MapShape;
2525
import software.amazon.smithy.model.shapes.MemberShape;
26+
import software.amazon.smithy.model.shapes.NumberShape;
2627
import software.amazon.smithy.model.shapes.Shape;
2728
import software.amazon.smithy.model.shapes.StructureShape;
2829
import software.amazon.smithy.model.shapes.UnionShape;
@@ -131,10 +132,16 @@ protected void deserializeStructure(GenerationContext context, StructureShape sh
131132
.orElse(memberName);
132133
Shape target = context.getModel().expectShape(memberShape.getTarget());
133134

134-
writer.write("$1L: (output.$2L !== undefined && output.$2L !== null)"
135-
+ " ? $3L: undefined,", memberName, locationName,
136-
// Dispatch to the output value provider for any additional handling.
137-
target.accept(getMemberVisitor("output." + locationName)));
135+
if (target.isBooleanShape() || target instanceof NumberShape) {
136+
// Booleans and numbers will call expectBoolean/expectNumber which will handle
137+
// null/undefined properly.
138+
writer.write("$L: $L,", memberName, target.accept(getMemberVisitor("output." + locationName)));
139+
} else {
140+
writer.write("$1L: (output.$2L !== undefined && output.$2L !== null)"
141+
+ " ? $3L: undefined,", memberName, locationName,
142+
// Dispatch to the output value provider for any additional handling.
143+
target.accept(getMemberVisitor("output." + locationName)));
144+
}
138145
});
139146
});
140147
}
@@ -152,13 +159,23 @@ protected void deserializeUnion(GenerationContext context, UnionShape shape) {
152159
String locationName = memberShape.getTrait(JsonNameTrait.class)
153160
.map(JsonNameTrait::getValue)
154161
.orElse(memberName);
155-
writer.openBlock("if (output.$L !== undefined && output.$L !== null) {", "}", locationName, locationName,
156-
() -> {
157-
writer.openBlock("return {", "};", () -> {
158-
// Dispatch to the output value provider for any additional handling.
159-
writer.write("$L: $L", memberName, target.accept(getMemberVisitor("output." + locationName)));
162+
163+
String memberValue = target.accept(getMemberVisitor("output." + locationName));
164+
if (target.isBooleanShape() || target instanceof NumberShape) {
165+
// Booleans and numbers will call expectBoolean/expectNumber which will handle
166+
// null/undefined properly.
167+
writer.openBlock("if ((val = $L) !== undefined) {", "}", memberValue, () -> {
168+
writer.write("return { $L: val }", memberName);
160169
});
161-
});
170+
} else {
171+
writer.openBlock("if (output.$L !== undefined && output.$L !== null) {", "}", locationName,
172+
locationName, () -> {
173+
writer.openBlock("return {", "};", () -> {
174+
// Dispatch to the output value provider for any additional handling.
175+
writer.write("$L: $L", memberName, memberValue);
176+
});
177+
});
178+
}
162179
});
163180
// Or write to the unknown member the element in the output.
164181
writer.write("return { $$unknown: Object.entries(output)[0] };");

packages/smithy-client/src/parse-utils.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ describe("expectBoolean", () => {
44
it("accepts booleans", () => {
55
expect(expectBoolean(true)).toEqual(true);
66
expect(expectBoolean(false)).toEqual(false);
7+
expect(expectBoolean(null)).toEqual(undefined);
8+
expect(expectBoolean(undefined)).toEqual(undefined);
79
});
810

911
it("rejects non-booleans", () => {
@@ -17,8 +19,6 @@ describe("expectBoolean", () => {
1719
expect(() => expectBoolean(NaN)).toThrowError();
1820
expect(() => expectBoolean({})).toThrowError();
1921
expect(() => expectBoolean([])).toThrowError();
20-
expect(() => expectBoolean(null)).toThrowError();
21-
expect(() => expectBoolean(undefined)).toThrowError();
2222
});
2323
});
2424

@@ -29,6 +29,8 @@ describe("expectNumber", () => {
2929
expect(expectNumber(Infinity)).toEqual(Infinity);
3030
expect(expectNumber(-Infinity)).toEqual(-Infinity);
3131
expect(expectNumber(NaN)).toEqual(NaN);
32+
expect(expectNumber(null)).toEqual(undefined);
33+
expect(expectNumber(undefined)).toEqual(undefined);
3234
});
3335

3436
it("rejects non-numbers", () => {
@@ -41,14 +43,14 @@ describe("expectNumber", () => {
4143
expect(() => expectNumber(false)).toThrowError();
4244
expect(() => expectNumber([])).toThrowError();
4345
expect(() => expectNumber({})).toThrowError();
44-
expect(() => expectNumber(null)).toThrowError();
45-
expect(() => expectNumber(undefined)).toThrowError();
4646
});
4747
});
4848

4949
describe("expectString", () => {
5050
it("accepts strings", () => {
5151
expect(expectString("foo")).toEqual("foo");
52+
expect(expectString(null)).toEqual(undefined);
53+
expect(expectString(undefined)).toEqual(undefined);
5254
});
5355

5456
it("rejects non-strings", () => {
@@ -60,7 +62,5 @@ describe("expectString", () => {
6062
expect(() => expectString(false)).toThrowError();
6163
expect(() => expectString([])).toThrowError();
6264
expect(() => expectString({})).toThrowError();
63-
expect(() => expectString(null)).toThrowError();
64-
expect(() => expectString(undefined)).toThrowError();
6565
});
6666
});

packages/smithy-client/src/parse-utils.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
* Asserts a value is a boolean and returns it.
33
*
44
* @param value A value that is expected to be a boolean.
5-
* @returns The value if it is a boolean, otherwise an error is thrown.
5+
* @returns The value if it's a boolean, undefined if it's null/undefined,
6+
* otherwise an error is thrown.
67
*/
7-
export function expectBoolean(value: any): boolean {
8+
export function expectBoolean(value: any): boolean | undefined {
9+
if (value === null || value === undefined) {
10+
return undefined;
11+
}
812
if (typeof value === "boolean") {
913
return value;
1014
}
@@ -15,9 +19,13 @@ export function expectBoolean(value: any): boolean {
1519
* Asserts a value is a number and returns it.
1620
*
1721
* @param value A value that is expected to be a number.
18-
* @returns The value if it is a number, otherwise an error is thrown.
22+
* @returns The value if it's a number, undefined if it's null/undefined,
23+
* otherwise an error is thrown.
1924
*/
2025
export function expectNumber(value: any): number {
26+
if (value === null || value === undefined) {
27+
return undefined;
28+
}
2129
if (typeof value === "number") {
2230
return value;
2331
}
@@ -28,9 +36,13 @@ export function expectNumber(value: any): number {
2836
* Asserts a value is a string and returns it.
2937
*
3038
* @param value A value that is expected to be a string.
31-
* @returns The value if it is a string, otherwise an error is thrown.
39+
* @returns The value if it's a string, undefined if it's null/undefined,
40+
* otherwise an error is thrown.
3241
*/
3342
export function expectString(value: any): string {
43+
if (value === null || value === undefined) {
44+
return undefined;
45+
}
3446
if (typeof value === "string") {
3547
return value;
3648
}

0 commit comments

Comments
 (0)