This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
/
unmarshallItem.ts
174 lines (155 loc) · 5.11 KB
/
unmarshallItem.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {Schema} from "./Schema";
import {
ListType,
MapType,
SchemaType,
TupleType,
ZeroArgumentsConstructor,
} from "./SchemaType";
import {InvalidSchemaError} from "./InvalidSchemaError";
import {BinarySet, Marshaller} from "@aws/dynamodb-auto-marshaller";
import {
AttributeMap,
AttributeValue,
AttributeValueList,
NumberSetAttributeValue,
StringSetAttributeValue,
} from "aws-sdk/clients/dynamodb";
/**
* Unmarshall a DynamoDB item into a JavaScript value.
*
* @param schema Metadata outlining the types to be expected
* throughout the input
* @param input The value to unmarshall
* @param valueConstructor A zero-argument constructor used to create the
* object onto which the input should be unmarshalled
*/
export function unmarshallItem<T = {[key: string]: any}>(
schema: Schema,
input: AttributeMap,
valueConstructor?: ZeroArgumentsConstructor<T>
): T {
const unmarshalled: T = valueConstructor
? new valueConstructor()
: Object.create(null);
for (const key of Object.keys(schema)) {
const {attributeName = key} = schema[key];
if (attributeName in input) {
(unmarshalled as {[key: string]: any})[key] = unmarshallValue(
schema[key],
input[attributeName]
);
}
}
return unmarshalled;
}
function unmarshallValue(schemaType: SchemaType, input: AttributeValue): any {
switch (schemaType.type) {
case 'Any':
case 'Collection':
case 'Hash':
const autoMarshaller = new Marshaller();
return autoMarshaller.unmarshallValue(input);
case 'Binary':
if (input.NULL) {
return new Uint8Array(0);
}
return input.B;
case 'Boolean':
return input.BOOL;
case 'Custom':
return schemaType.unmarshall(input);
case 'Date':
return input.N ? new Date(Number(input.N) * 1000) : undefined;
case 'Document':
return input.M
? unmarshallItem(
schemaType.members,
input.M,
schemaType.valueConstructor
) : undefined;
case 'List':
return input.L ? unmarshallList(schemaType, input.L) : undefined;
case 'Map':
return input.M ? unmarshallMap(schemaType, input.M) : undefined;
case 'Null':
return input.NULL ? null : undefined;
case 'Number':
return typeof input.N === 'string' ? Number(input.N) : undefined;
case 'Set':
switch (schemaType.memberType) {
case 'Binary':
if (input.NULL) {
return new BinarySet();
}
return typeof input.BS !== 'undefined'
? new BinarySet(input.BS as Array<Uint8Array>)
: undefined;
case 'Number':
if (input.NULL) {
return new Set<number>();
}
return input.NS ? unmarshallNumberSet(input.NS) : undefined;
case 'String':
if (input.NULL) {
return new Set<string>();
}
return input.SS ? unmarshallStringSet(input.SS) : undefined;
default:
throw new InvalidSchemaError(
schemaType,
`Unrecognized set member type: ${schemaType.memberType}`
);
}
case 'String':
return input.NULL ? '' : input.S;
case 'Tuple':
return input.L ? unmarshallTuple(schemaType, input.L) : undefined;
}
throw new InvalidSchemaError(schemaType, 'Unrecognized schema node');
}
function unmarshallList(
schemaType: ListType,
input: AttributeValueList
): Array<any> {
const list: Array<any> = [];
for (const element of input) {
list.push(unmarshallValue(schemaType.memberType, element));
}
return list;
}
function unmarshallMap(
schemaType: MapType,
input: AttributeMap
): Map<string, any> {
const map = new Map<string, any>();
for (const key of Object.keys(input)) {
map.set(key, unmarshallValue(schemaType.memberType, input[key]));
}
return map;
}
function unmarshallNumberSet(input: NumberSetAttributeValue): Set<number> {
const set = new Set<number>();
for (const number of input) {
set.add(Number(number));
}
return set;
}
function unmarshallStringSet(input: StringSetAttributeValue): Set<string> {
const set = new Set<string>();
for (const string of input) {
set.add(string);
}
return set;
}
function unmarshallTuple(
schemaType: TupleType,
input: AttributeValueList
): Array<any> {
const {members} = schemaType;
const tuple: Array<any> = [];
for (let i = 0; i < members.length; i++) {
tuple.push(unmarshallValue(members[i], input[i]));
}
return tuple;
}