Skip to content

Commit 005abb3

Browse files
Replace Serializer with SerializationOptions
1 parent fad9e64 commit 005abb3

26 files changed

+341
-317
lines changed

packages/firestore/lite/src/api/database.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,14 @@ export class Firestore implements firestore.FirebaseFirestore {
9393
this._datastorePromise = PlatformSupport.getPlatform()
9494
.loadConnection(databaseInfo)
9595
.then(connection => {
96-
const serializer = PlatformSupport.getPlatform().newSerializer(
96+
const serializationOptions = PlatformSupport.getPlatform().newSerializationOptions(
9797
databaseInfo.databaseId
9898
);
99-
return newDatastore(connection, this._credentials, serializer);
99+
return newDatastore(
100+
connection,
101+
this._credentials,
102+
serializationOptions
103+
);
100104
});
101105
}
102106

packages/firestore/lite/src/api/reference.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,12 +592,11 @@ export function queryEqual<T>(
592592

593593
export function newUserDataReader(firestore: Firestore): UserDataReader {
594594
const settings = firestore._getSettings();
595-
const serializer = PlatformSupport.getPlatform().newSerializer(
595+
const serializationOptions = PlatformSupport.getPlatform().newSerializationOptions(
596596
firestore._databaseId
597597
);
598598
return new UserDataReader(
599-
firestore._databaseId,
600599
!!settings.ignoreUndefinedProperties,
601-
serializer
600+
serializationOptions
602601
);
603602
}

packages/firestore/src/api/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
337337
if (!this._userDataReader) {
338338
// Lazy initialize UserDataReader once the settings are frozen
339339
this._userDataReader = new UserDataReader(
340-
this._databaseId,
341-
this._settings.ignoreUndefinedProperties
340+
this._settings.ignoreUndefinedProperties,
341+
PlatformSupport.getPlatform().newSerializationOptions(this._databaseId)
342342
);
343343
}
344344
return this._userDataReader;

packages/firestore/src/api/field_value.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ export class ArrayUnionFieldValueImpl extends SerializableFieldValue {
115115
methodName: this._methodName,
116116
arrayElement: true
117117
},
118-
context.databaseId,
119-
context.serializer,
118+
context.serializationOptions,
120119
context.ignoreUndefinedProperties
121120
);
122121
const parsedElements = this._elements.map(
@@ -147,8 +146,7 @@ export class ArrayRemoveFieldValueImpl extends SerializableFieldValue {
147146
methodName: this._methodName,
148147
arrayElement: true
149148
},
150-
context.databaseId,
151-
context.serializer,
149+
context.serializationOptions,
152150
context.ignoreUndefinedProperties
153151
);
154152
const parsedElements = this._elements.map(
@@ -175,13 +173,12 @@ export class NumericIncrementFieldValueImpl extends SerializableFieldValue {
175173
dataSource: UserDataSource.Argument,
176174
methodName: this._methodName
177175
},
178-
context.databaseId,
179-
context.serializer,
176+
context.serializationOptions,
180177
context.ignoreUndefinedProperties
181178
);
182179
const operand = parseData(this._operand, parseContext)!;
183180
const numericIncrement = new NumericIncrementTransformOperation(
184-
context.serializer,
181+
context.serializationOptions,
185182
operand
186183
);
187184
return new FieldTransform(context.path!, numericIncrement);

packages/firestore/src/api/user_data_reader.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { isPlainObject, valueDescription } from '../util/input_validation';
3838
import { Dict, forEach, isEmpty } from '../util/obj';
3939
import { ObjectValue, ObjectValueBuilder } from '../model/object_value';
4040
import {
41-
JsonProtoSerializer,
41+
SerializationOptions,
4242
toBytes,
4343
toNumber,
4444
toResourceName,
@@ -48,7 +48,6 @@ import { Blob } from './blob';
4848
import { BaseFieldPath, fromDotSeparatedString } from './field_path';
4949
import { DeleteFieldValueImpl, SerializableFieldValue } from './field_value';
5050
import { GeoPoint } from './geo_point';
51-
import { PlatformSupport } from '../platform/platform';
5251

5352
const RESERVED_FIELD_REGEX = /^__.*__$/;
5453

@@ -181,8 +180,7 @@ export class ParseContext {
181180
* Initializes a ParseContext with the given source and path.
182181
*
183182
* @param settings The settings for the parser.
184-
* @param databaseId The database ID of the Firestore instance.
185-
* @param serializer The serializer to use to generate the Value proto.
183+
* @param serializationOptions The options to use to generate the Value proto.
186184
* @param ignoreUndefinedProperties Whether to ignore undefined properties
187185
* rather than throw.
188186
* @param fieldTransforms A mutable list of field transforms encountered while
@@ -197,8 +195,7 @@ export class ParseContext {
197195
*/
198196
constructor(
199197
readonly settings: ContextSettings,
200-
readonly databaseId: DatabaseId,
201-
readonly serializer: JsonProtoSerializer,
198+
readonly serializationOptions: SerializationOptions,
202199
readonly ignoreUndefinedProperties: boolean,
203200
fieldTransforms?: FieldTransform[],
204201
fieldMask?: FieldPath[]
@@ -224,8 +221,7 @@ export class ParseContext {
224221
contextWith(configuration: Partial<ContextSettings>): ParseContext {
225222
return new ParseContext(
226223
{ ...this.settings, ...configuration },
227-
this.databaseId,
228-
this.serializer,
224+
this.serializationOptions,
229225
this.ignoreUndefinedProperties,
230226
this.fieldTransforms,
231227
this.fieldMask
@@ -301,16 +297,10 @@ export class ParseContext {
301297
* classes.
302298
*/
303299
export class UserDataReader {
304-
private readonly serializer: JsonProtoSerializer;
305-
306300
constructor(
307-
private readonly databaseId: DatabaseId,
308301
private readonly ignoreUndefinedProperties: boolean,
309-
serializer?: JsonProtoSerializer
310-
) {
311-
this.serializer =
312-
serializer || PlatformSupport.getPlatform().newSerializer(databaseId);
313-
}
302+
private readonly serializationOptions: SerializationOptions
303+
) {}
314304

315305
/** Parse document data from a set() call. */
316306
parseSetData(
@@ -489,8 +479,7 @@ export class UserDataReader {
489479
path: FieldPath.EMPTY_PATH,
490480
arrayElement: false
491481
},
492-
this.databaseId,
493-
this.serializer,
482+
this.serializationOptions,
494483
this.ignoreUndefinedProperties
495484
);
496485
}
@@ -653,14 +642,16 @@ function parseScalarValue(
653642
if (value === null) {
654643
return { nullValue: 'NULL_VALUE' };
655644
} else if (typeof value === 'number') {
656-
return toNumber(context.serializer, value);
645+
return toNumber(context.serializationOptions, value);
657646
} else if (typeof value === 'boolean') {
658647
return { booleanValue: value };
659648
} else if (typeof value === 'string') {
660649
return { stringValue: value };
661650
} else if (value instanceof Date) {
662651
const timestamp = Timestamp.fromDate(value);
663-
return { timestampValue: toTimestamp(context.serializer, timestamp) };
652+
return {
653+
timestampValue: toTimestamp(context.serializationOptions, timestamp)
654+
};
664655
} else if (value instanceof Timestamp) {
665656
// Firestore backend truncates precision down to microseconds. To ensure
666657
// offline mode works the same with regards to truncation, perform the
@@ -669,7 +660,9 @@ function parseScalarValue(
669660
value.seconds,
670661
Math.floor(value.nanoseconds / 1000) * 1000
671662
);
672-
return { timestampValue: toTimestamp(context.serializer, timestamp) };
663+
return {
664+
timestampValue: toTimestamp(context.serializationOptions, timestamp)
665+
};
673666
} else if (value instanceof GeoPoint) {
674667
return {
675668
geoPointValue: {
@@ -678,9 +671,9 @@ function parseScalarValue(
678671
}
679672
};
680673
} else if (value instanceof Blob) {
681-
return { bytesValue: toBytes(context.serializer, value) };
674+
return { bytesValue: toBytes(context.serializationOptions, value) };
682675
} else if (value instanceof DocumentKeyReference) {
683-
const thisDb = context.databaseId;
676+
const thisDb = context.serializationOptions.databaseId;
684677
const otherDb = value._databaseId;
685678
if (!otherDb.isEqual(thisDb)) {
686679
throw context.createError(
@@ -691,8 +684,8 @@ function parseScalarValue(
691684
}
692685
return {
693686
referenceValue: toResourceName(
694-
value._key.path,
695-
value._databaseId || context.serializer.databaseId
687+
value._databaseId || context.serializationOptions.databaseId,
688+
value._key.path
696689
)
697690
};
698691
} else if (value === undefined && context.ignoreUndefinedProperties) {

packages/firestore/src/core/component_provider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ export class IndexedDbComponentProvider extends MemoryComponentProvider {
250250
const persistenceKey = IndexedDbPersistence.buildStoragePrefix(
251251
cfg.databaseInfo
252252
);
253-
const serializer = cfg.platform.newSerializer(cfg.databaseInfo.databaseId);
253+
const serializer = cfg.platform.newSerializationOptions(
254+
cfg.databaseInfo.databaseId
255+
);
254256
return new IndexedDbPersistence(
255257
cfg.persistenceSettings.synchronizeTabs,
256258
persistenceKey,

packages/firestore/src/core/firestore_client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export class FirestoreClient {
236236
// provider).
237237

238238
const connection = await this.platform.loadConnection(this.databaseInfo);
239-
const serializer = this.platform.newSerializer(
239+
const serializer = this.platform.newSerializationOptions(
240240
this.databaseInfo.databaseId
241241
);
242242
const datastore = newDatastore(connection, this.credentials, serializer);

packages/firestore/src/local/indexeddb_persistence.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { ListenSequence, SequenceNumberSyncer } from '../core/listen_sequence';
2121
import { ListenSequenceNumber, TargetId } from '../core/types';
2222
import { DocumentKey } from '../model/document_key';
2323
import { Platform } from '../platform/platform';
24-
import { JsonProtoSerializer } from '../remote/serializer';
24+
import { SerializationOptions } from '../remote/serializer';
2525
import { debugAssert, fail } from '../util/assert';
2626
import { AsyncQueue, DelayedOperation, TimerId } from '../util/async_queue';
2727
import { Code, FirestoreError } from '../util/error';
@@ -242,7 +242,7 @@ export class IndexedDbPersistence implements Persistence {
242242
platform: Platform,
243243
lruParams: LruParams,
244244
private readonly queue: AsyncQueue,
245-
serializer: JsonProtoSerializer,
245+
options: SerializationOptions,
246246
private readonly sequenceNumberSyncer: SequenceNumberSyncer,
247247

248248
/**
@@ -260,7 +260,7 @@ export class IndexedDbPersistence implements Persistence {
260260

261261
this.referenceDelegate = new IndexedDbLruDelegate(this, lruParams);
262262
this.dbName = persistenceKey + IndexedDbPersistence.MAIN_DATABASE;
263-
this.serializer = new LocalSerializer(serializer);
263+
this.serializer = new LocalSerializer(options);
264264
this.document = platform.document;
265265
this.targetCache = new IndexedDbTargetCache(
266266
this.referenceDelegate,

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ export class DbMutationBatch {
483483
/**
484484
* A list of mutations to apply. All mutations will be applied atomically.
485485
*
486-
* Mutations are serialized via JsonProtoSerializer.toMutation().
486+
* Mutations are serialized via toMutation().
487487
*/
488488
public mutations: api.Write[]
489489
) {}

packages/firestore/src/local/local_serializer.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
fromDocumentsTarget,
3232
fromMutation,
3333
fromQueryTarget,
34-
JsonProtoSerializer,
34+
SerializationOptions,
3535
toDocument,
3636
toDocumentsTarget,
3737
toMutation,
@@ -54,13 +54,13 @@ import { TargetData, TargetPurpose } from './target_data';
5454

5555
/** Serializer for values stored in the LocalStore. */
5656
export class LocalSerializer {
57-
constructor(private remoteSerializer: JsonProtoSerializer) {}
57+
constructor(private options: SerializationOptions) {}
5858

5959
/** Decodes a remote document from storage locally to a Document. */
6060
fromDbRemoteDocument(remoteDoc: DbRemoteDocument): MaybeDocument {
6161
if (remoteDoc.document) {
6262
return fromDocument(
63-
this.remoteSerializer,
63+
this.options,
6464
remoteDoc.document,
6565
!!remoteDoc.hasCommittedMutations
6666
);
@@ -87,7 +87,7 @@ export class LocalSerializer {
8787
const dbReadTime = this.toDbTimestampKey(readTime);
8888
const parentPath = maybeDoc.key.path.popLast().toArray();
8989
if (maybeDoc instanceof Document) {
90-
const doc = toDocument(this.remoteSerializer, maybeDoc);
90+
const doc = toDocument(this.options, maybeDoc);
9191
const hasCommittedMutations = maybeDoc.hasCommittedMutations;
9292
return new DbRemoteDocument(
9393
/* unknownDocument= */ null,
@@ -151,10 +151,10 @@ export class LocalSerializer {
151151
/** Encodes a batch of mutations into a DbMutationBatch for local storage. */
152152
toDbMutationBatch(userId: string, batch: MutationBatch): DbMutationBatch {
153153
const serializedBaseMutations = batch.baseMutations.map(m =>
154-
toMutation(this.remoteSerializer, m)
154+
toMutation(this.options, m)
155155
);
156156
const serializedMutations = batch.mutations.map(m =>
157-
toMutation(this.remoteSerializer, m)
157+
toMutation(this.options, m)
158158
);
159159
return new DbMutationBatch(
160160
userId,
@@ -168,11 +168,9 @@ export class LocalSerializer {
168168
/** Decodes a DbMutationBatch into a MutationBatch */
169169
fromDbMutationBatch(dbBatch: DbMutationBatch): MutationBatch {
170170
const baseMutations = (dbBatch.baseMutations || []).map(m =>
171-
fromMutation(this.remoteSerializer, m)
172-
);
173-
const mutations = dbBatch.mutations.map(m =>
174-
fromMutation(this.remoteSerializer, m)
171+
fromMutation(this.options, m)
175172
);
173+
const mutations = dbBatch.mutations.map(m => fromMutation(this.options, m));
176174
const timestamp = Timestamp.fromMillis(dbBatch.localWriteTimeMs);
177175
return new MutationBatch(
178176
dbBatch.batchId,
@@ -192,9 +190,9 @@ export class LocalSerializer {
192190

193191
let target: Target;
194192
if (isDocumentQuery(dbTarget.query)) {
195-
target = fromDocumentsTarget(this.remoteSerializer, dbTarget.query);
193+
target = fromDocumentsTarget(this.options, dbTarget.query);
196194
} else {
197-
target = fromQueryTarget(this.remoteSerializer, dbTarget.query);
195+
target = fromQueryTarget(this.options, dbTarget.query);
198196
}
199197
return new TargetData(
200198
target,
@@ -222,9 +220,9 @@ export class LocalSerializer {
222220
);
223221
let queryProto: DbQuery;
224222
if (targetData.target.isDocumentQuery()) {
225-
queryProto = toDocumentsTarget(this.remoteSerializer, targetData.target);
223+
queryProto = toDocumentsTarget(this.options, targetData.target);
226224
} else {
227-
queryProto = toQueryTarget(this.remoteSerializer, targetData.target);
225+
queryProto = toQueryTarget(this.options, targetData.target);
228226
}
229227

230228
// We can't store the resumeToken as a ByteString in IndexedDb, so we

packages/firestore/src/model/transform_operation.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ import * as api from '../protos/firestore_proto_api';
1919

2020
import { Timestamp } from '../api/timestamp';
2121
import { debugAssert } from '../util/assert';
22-
import { JsonProtoSerializer, toDouble, toInteger } from '../remote/serializer';
22+
import {
23+
SerializationOptions,
24+
toDouble,
25+
toInteger
26+
} from '../remote/serializer';
2327
import {
2428
isArray,
2529
isInteger,
@@ -190,7 +194,7 @@ export class ArrayRemoveTransformOperation implements TransformOperation {
190194
*/
191195
export class NumericIncrementTransformOperation implements TransformOperation {
192196
constructor(
193-
private readonly serializer: JsonProtoSerializer,
197+
private readonly serializationOptions: SerializationOptions,
194198
readonly operand: api.Value
195199
) {
196200
debugAssert(
@@ -211,7 +215,7 @@ export class NumericIncrementTransformOperation implements TransformOperation {
211215
if (isInteger(baseValue) && isInteger(this.operand)) {
212216
return toInteger(sum);
213217
} else {
214-
return toDouble(this.serializer, sum);
218+
return toDouble(this.serializationOptions, sum);
215219
}
216220
}
217221

packages/firestore/src/platform/platform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { DatabaseId, DatabaseInfo } from '../core/database_info';
1919
import { Connection } from '../remote/connection';
20-
import { JsonProtoSerializer } from '../remote/serializer';
20+
import { SerializationOptions } from '../remote/serializer';
2121
import { fail } from '../util/assert';
2222
import { ConnectivityMonitor } from './../remote/connectivity_monitor';
2323

@@ -32,7 +32,7 @@ import { ConnectivityMonitor } from './../remote/connectivity_monitor';
3232
export interface Platform {
3333
loadConnection(databaseInfo: DatabaseInfo): Promise<Connection>;
3434
newConnectivityMonitor(): ConnectivityMonitor;
35-
newSerializer(databaseId: DatabaseId): JsonProtoSerializer;
35+
newSerializationOptions(databaseId: DatabaseId): SerializationOptions;
3636

3737
/** Formats an object as a JSON string, suitable for logging. */
3838
formatJSON(value: unknown): string;

0 commit comments

Comments
 (0)