Skip to content

Commit db82b25

Browse files
committed
Set the default value for unmarshaller
Signed-off-by: moznion <moznion@mail.moznion.net>
1 parent 70f3f27 commit db82b25

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/dynamodb_item_field.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class ArrayField implements DynamodbItemField {
7272
return ts.factory.createPropertyAssignment(
7373
this.fieldName,
7474
ts.factory.createIdentifier(
75-
`${argName}['${this.fieldName}']?.L?.map(v => ${valueConvertingOp.leftOp}v.${this.valueType.kind}${valueConvertingOp.rightOp})`,
75+
`${argName}['${this.fieldName}']?.L?.map(v => ${valueConvertingOp.leftOp}v.${this.valueType.kind}${valueConvertingOp.rightOp}) || []`,
7676
),
7777
);
7878
}
@@ -119,7 +119,7 @@ export class SetField implements DynamodbItemField {
119119
return ts.factory.createPropertyAssignment(
120120
this.fieldName,
121121
ts.factory.createIdentifier(
122-
`${argName}['${this.fieldName}']?.${this.valueType.kind}S?.map(v => ${valueConvertingOp.leftOp}v${valueConvertingOp.rightOp})`,
122+
`${argName}['${this.fieldName}']?.${this.valueType.kind}S?.map(v => ${valueConvertingOp.leftOp}v${valueConvertingOp.rightOp}) || new Set()`,
123123
),
124124
);
125125
}

tests/from_dynamodb_record_transformer.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,21 @@ describe('from dynamodb record transform', () => {
237237
expect(got.mapToBigint).toEqual(new Map([['bigint', BigInt(789)]]));
238238
expect(got.kvMapToBigint).toEqual({ bigint: BigInt(890) });
239239
});
240+
241+
test('for default values', () => {
242+
interface Interface {
243+
readonly num?: number;
244+
readonly numArray: Array<number>;
245+
readonly numSet: Set<number>;
246+
readonly mapToNumber: Map<string, number>;
247+
readonly kvMapToNumber: { [key: string]: number };
248+
}
249+
250+
const got = fromDynamodbRecord<Interface>({});
251+
expect(got.num).toBeUndefined();
252+
expect(got.numArray).toEqual([]);
253+
expect(got.numSet).toEqual(new Set());
254+
expect(got.mapToNumber).toEqual(new Map());
255+
expect(got.kvMapToNumber).toEqual({});
256+
});
240257
});

0 commit comments

Comments
 (0)