-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathuser_data_writer.ts
More file actions
202 lines (185 loc) · 6.26 KB
/
user_data_writer.ts
File metadata and controls
202 lines (185 loc) · 6.26 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { DocumentData } from '@firebase/firestore-types';
import { DatabaseId } from '../core/database_info';
import { DocumentKey } from '../model/document_key';
import {
normalizeByteString,
normalizeNumber,
normalizeTimestamp
} from '../model/normalize';
import { ResourcePath } from '../model/path';
import {
getLocalWriteTime,
getPreviousValue
} from '../model/server_timestamps';
import { TypeOrder } from '../model/type_order';
import { VECTOR_MAP_VECTORS_KEY, typeOrder } from '../model/values';
import {
ApiClientObjectMap,
ArrayValue as ProtoArrayValue,
LatLng as ProtoLatLng,
MapValue as ProtoMapValue,
Timestamp as ProtoTimestamp,
Value,
Value as ProtoValue
} from '../protos/firestore_proto_api';
import { isValidResourceName } from '../remote/serializer';
import { fail, hardAssert } from '../util/assert';
import { ByteString } from '../util/byte_string';
import { logDebug } from '../util/log';
import { forEach } from '../util/obj';
import { GeoPoint } from './geo_point';
import { Timestamp } from './timestamp';
import { VectorValue } from './vector_value';
export type ServerTimestampBehavior = 'estimate' | 'previous' | 'none';
/**
* Converts Firestore's internal types to the JavaScript types that we expose
* to the user.
*
* @internal
*/
export abstract class AbstractUserDataWriter {
convertValue(
value: ProtoValue,
serverTimestampBehavior: ServerTimestampBehavior = 'none'
): unknown {
switch (typeOrder(value)) {
case TypeOrder.NullValue:
return null;
case TypeOrder.BooleanValue:
return value.booleanValue!;
case TypeOrder.NumberValue:
return normalizeNumber(value.integerValue || value.doubleValue);
case TypeOrder.TimestampValue:
return this.convertTimestamp(value.timestampValue!);
case TypeOrder.ServerTimestampValue:
return this.convertServerTimestamp(value, serverTimestampBehavior);
case TypeOrder.StringValue:
return value.stringValue!;
case TypeOrder.BlobValue:
return this.convertBytes(normalizeByteString(value.bytesValue!));
case TypeOrder.RefValue:
return this.convertReference(value.referenceValue!);
case TypeOrder.GeoPointValue:
return this.convertGeoPoint(value.geoPointValue!);
case TypeOrder.ArrayValue:
return this.convertArray(value.arrayValue!, serverTimestampBehavior);
case TypeOrder.ObjectValue:
return this.convertObject(value.mapValue!, serverTimestampBehavior);
case TypeOrder.VectorValue:
return this.convertVectorValue(value.mapValue!);
default:
throw fail(0xf2a2, 'Invalid value type', {
value
});
}
}
private convertObject(
mapValue: ProtoMapValue,
serverTimestampBehavior: ServerTimestampBehavior
): DocumentData {
return this.convertObjectMap(mapValue.fields, serverTimestampBehavior);
}
/**
* @internal
*/
convertObjectMap(
fields: ApiClientObjectMap<Value> | undefined,
serverTimestampBehavior: ServerTimestampBehavior = 'none'
): DocumentData {
const result: DocumentData = {};
forEach(fields, (key, value) => {
result[key] = this.convertValue(value, serverTimestampBehavior);
});
return result;
}
/**
* @internal
*/
convertVectorValue(mapValue: ProtoMapValue): VectorValue {
const values = mapValue.fields?.[
VECTOR_MAP_VECTORS_KEY
].arrayValue?.values?.map(value => {
return normalizeNumber(value.doubleValue);
});
return new VectorValue(values);
}
private convertGeoPoint(value: ProtoLatLng): GeoPoint {
return new GeoPoint(
normalizeNumber(value.latitude),
normalizeNumber(value.longitude)
);
}
private convertArray(
arrayValue: ProtoArrayValue,
serverTimestampBehavior: ServerTimestampBehavior
): unknown[] {
return (arrayValue.values || []).map(value =>
this.convertValue(value, serverTimestampBehavior)
);
}
private convertServerTimestamp(
value: ProtoValue,
serverTimestampBehavior: ServerTimestampBehavior
): unknown {
switch (serverTimestampBehavior) {
case 'previous':
const previousValue = getPreviousValue(value);
if (previousValue == null) {
return null;
}
return this.convertValue(previousValue, serverTimestampBehavior);
case 'estimate':
return this.convertTimestamp(getLocalWriteTime(value));
default:
return null;
}
}
private convertTimestamp(value: ProtoTimestamp): Timestamp {
const normalizedValue = normalizeTimestamp(value);
return new Timestamp(normalizedValue.seconds, normalizedValue.nanos);
}
protected convertDocumentKey(
name: string,
expectedDatabaseId: DatabaseId
): DocumentKey {
const resourcePath = ResourcePath.fromString(name);
hardAssert(
isValidResourceName(resourcePath),
0x25d8,
'ReferenceValue is not valid',
{ name }
);
const databaseId = new DatabaseId(resourcePath.get(1), resourcePath.get(3));
const key = new DocumentKey(resourcePath.popFirst(5));
if (!databaseId.isEqual(expectedDatabaseId)) {
// TODO(b/64130202): Somehow support foreign references.
logDebug(
`Document ${key} contains a document ` +
`reference within a different database (` +
`${databaseId.projectId}/${databaseId.database}) which is not ` +
`supported. It will be treated as a reference in the current ` +
`database (${expectedDatabaseId.projectId}/${expectedDatabaseId.database}) ` +
`instead.`
);
}
return key;
}
protected abstract convertReference(name: string): unknown;
protected abstract convertBytes(bytes: ByteString): unknown;
}