-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeserializer.js
55 lines (53 loc) · 1.21 KB
/
deserializer.js
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
function deserialize(fields) {
return Object.fromEntries(
Object.entries(fields).map(([key, value]) => {
return [key, decode(value)];
})
);
}
function decode(value) {
if ("nullValue" in value) {
return null;
}
if ("booleanValue" in value) {
return value.booleanValue;
}
if ("integerValue" in value) {
return parseInt(value.integerValue);
}
if ("doubleValue" in value) {
return value.doubleValue;
}
if ("timestampValue" in value) {
return value.timestampValue;
}
if ("stringValue" in value) {
return value.stringValue;
}
if ("bytesValue" in value) {
return value.bytesValue;
}
if ("referenceValue" in value) {
return value.referenceValue;
}
if ("geoPointValue" in value) {
const { latitude, longitude } = value.geoPointValue;
return { latitude, longitude };
}
if ("arrayValue" in value) {
if ("values" in value.arrayValue) {
return value.arrayValue.values.map((value) => decode(value));
} else {
return [];
}
}
if ("mapValue" in value) {
if ("fields" in value.mapValue) {
return deserialize(value.mapValue.fields);
} else {
return {};
}
}
return null;
}
module.exports = deserialize;