Skip to content

Commit 6651efd

Browse files
Make Converter passing explicit (#3171)
1 parent 937c59d commit 6651efd

File tree

7 files changed

+87
-46
lines changed

7 files changed

+87
-46
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class DocumentReference<T = firestore.DocumentData>
5555
constructor(
5656
readonly firestore: Firestore,
5757
key: DocumentKey,
58-
readonly _converter?: firestore.FirestoreDataConverter<T>
58+
readonly _converter: firestore.FirestoreDataConverter<T> | null
5959
) {
6060
super(firestore._databaseId, key, _converter);
6161
}
@@ -79,7 +79,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
7979
constructor(
8080
readonly firestore: Firestore,
8181
readonly _query: InternalQuery,
82-
readonly _converter?: firestore.FirestoreDataConverter<T>
82+
readonly _converter: firestore.FirestoreDataConverter<T> | null
8383
) {}
8484

8585
where(
@@ -153,7 +153,7 @@ export class CollectionReference<T = firestore.DocumentData> extends Query<T>
153153
constructor(
154154
readonly firestore: Firestore,
155155
readonly _path: ResourcePath,
156-
readonly _converter?: firestore.FirestoreDataConverter<T>
156+
readonly _converter: firestore.FirestoreDataConverter<T> | null
157157
) {
158158
super(firestore, InternalQuery.atPath(_path), _converter);
159159
}
@@ -189,12 +189,16 @@ export function collection(
189189
const path = ResourcePath.fromString(relativePath);
190190
if (parent instanceof Firestore) {
191191
validateCollectionPath(path);
192-
return new CollectionReference(parent, path);
192+
return new CollectionReference(parent, path, /* converter= */ null);
193193
} else {
194194
const doc = cast(parent, DocumentReference);
195195
const absolutePath = doc._key.path.child(path);
196196
validateCollectionPath(absolutePath);
197-
return new CollectionReference(doc.firestore, absolutePath);
197+
return new CollectionReference(
198+
doc.firestore,
199+
absolutePath,
200+
/* converter= */ null
201+
);
198202
}
199203
}
200204

@@ -219,7 +223,11 @@ export function doc<T>(
219223
const path = ResourcePath.fromString(relativePath!);
220224
if (parent instanceof Firestore) {
221225
validateDocumentPath(path);
222-
return new DocumentReference(parent, new DocumentKey(path));
226+
return new DocumentReference(
227+
parent,
228+
new DocumentKey(path),
229+
/* converter= */ null
230+
);
223231
} else {
224232
const coll = cast(parent, CollectionReference);
225233
const absolutePath = coll._path.child(path);
@@ -248,7 +256,8 @@ export function parent<T>(
248256
} else {
249257
return new DocumentReference(
250258
child.firestore,
251-
new DocumentKey(parentPath)
259+
new DocumentKey(parentPath),
260+
/* converter= */ null
252261
);
253262
}
254263
} else {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
3838
private _firestore: Firestore,
3939
private _key: DocumentKey,
4040
private _document: Document | null,
41-
private _converter?: firestore.FirestoreDataConverter<T>
41+
private _converter: firestore.FirestoreDataConverter<T> | null
4242
) {}
4343

4444
get id(): string {
@@ -66,15 +66,17 @@ export class DocumentSnapshot<T = firestore.DocumentData>
6666
const snapshot = new QueryDocumentSnapshot(
6767
this._firestore,
6868
this._key,
69-
this._document
69+
this._document,
70+
/* converter= */ null
7071
);
7172
return this._converter.fromFirestore(snapshot);
7273
} else {
7374
const userDataWriter = new UserDataWriter(
7475
this._firestore._databaseId,
7576
/* timestampsInSnapshots= */ true,
7677
/* serverTimestampBehavior=*/ 'none',
77-
key => new DocumentReference(this._firestore, key)
78+
key =>
79+
new DocumentReference(this._firestore, key, /* converter= */ null)
7880
);
7981
return userDataWriter.convertValue(this._document.toProto()) as T;
8082
}

packages/firestore/src/api/database.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -580,14 +580,22 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
580580
validateExactNumberOfArgs('Firestore.collection', arguments, 1);
581581
validateArgType('Firestore.collection', 'non-empty string', 1, pathString);
582582
this.ensureClientConfigured();
583-
return new CollectionReference(ResourcePath.fromString(pathString), this);
583+
return new CollectionReference(
584+
ResourcePath.fromString(pathString),
585+
this,
586+
/* converter= */ null
587+
);
584588
}
585589

586590
doc(pathString: string): firestore.DocumentReference {
587591
validateExactNumberOfArgs('Firestore.doc', arguments, 1);
588592
validateArgType('Firestore.doc', 'non-empty string', 1, pathString);
589593
this.ensureClientConfigured();
590-
return DocumentReference.forPath(ResourcePath.fromString(pathString), this);
594+
return DocumentReference.forPath(
595+
ResourcePath.fromString(pathString),
596+
this,
597+
/* converter= */ null
598+
);
591599
}
592600

593601
collectionGroup(collectionId: string): firestore.Query {
@@ -608,7 +616,8 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
608616
this.ensureClientConfigured();
609617
return new Query(
610618
new InternalQuery(ResourcePath.EMPTY_PATH, collectionId),
611-
this
619+
this,
620+
/* converter= */ null
612621
);
613622
}
614623

@@ -946,7 +955,7 @@ export class DocumentReference<T = firestore.DocumentData>
946955
constructor(
947956
public _key: DocumentKey,
948957
readonly firestore: Firestore,
949-
readonly _converter?: firestore.FirestoreDataConverter<T>
958+
readonly _converter: firestore.FirestoreDataConverter<T> | null
950959
) {
951960
super(firestore._databaseId, _key, _converter);
952961
this._firestoreClient = this.firestore.ensureClientConfigured();
@@ -955,7 +964,7 @@ export class DocumentReference<T = firestore.DocumentData>
955964
static forPath<U>(
956965
path: ResourcePath,
957966
firestore: Firestore,
958-
converter?: firestore.FirestoreDataConverter<U>
967+
converter: firestore.FirestoreDataConverter<U> | null
959968
): DocumentReference<U> {
960969
if (path.length % 2 !== 0) {
961970
throw new FirestoreError(
@@ -1001,7 +1010,11 @@ export class DocumentReference<T = firestore.DocumentData>
10011010
);
10021011
}
10031012
const path = ResourcePath.fromString(pathString);
1004-
return new CollectionReference(this._key.path.child(path), this.firestore);
1013+
return new CollectionReference(
1014+
this._key.path.child(path),
1015+
this.firestore,
1016+
/* converter= */ null
1017+
);
10051018
}
10061019

10071020
isEqual(other: firestore.DocumentReference<T>): boolean {
@@ -1328,7 +1341,7 @@ export class DocumentSnapshot<T = firestore.DocumentData>
13281341
public _document: Document | null,
13291342
private _fromCache: boolean,
13301343
private _hasPendingWrites: boolean,
1331-
private readonly _converter?: firestore.FirestoreDataConverter<T>
1344+
private readonly _converter: firestore.FirestoreDataConverter<T> | null
13321345
) {}
13331346

13341347
data(options?: firestore.SnapshotOptions): T | undefined {
@@ -1345,15 +1358,17 @@ export class DocumentSnapshot<T = firestore.DocumentData>
13451358
this._key,
13461359
this._document,
13471360
this._fromCache,
1348-
this._hasPendingWrites
1361+
this._hasPendingWrites,
1362+
/* converter= */ null
13491363
);
13501364
return this._converter.fromFirestore(snapshot, options);
13511365
} else {
13521366
const userDataWriter = new UserDataWriter(
13531367
this._firestore._databaseId,
13541368
this._firestore._areTimestampsInSnapshotsEnabled(),
13551369
options.serverTimestamps || 'none',
1356-
key => new DocumentReference(key, this._firestore)
1370+
key =>
1371+
new DocumentReference(key, this._firestore, /* converter= */ null)
13571372
);
13581373
return userDataWriter.convertValue(this._document.toProto()) as T;
13591374
}
@@ -1436,7 +1451,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
14361451
constructor(
14371452
public _query: InternalQuery,
14381453
readonly firestore: Firestore,
1439-
protected readonly _converter?: firestore.FirestoreDataConverter<T>
1454+
protected readonly _converter: firestore.FirestoreDataConverter<T> | null
14401455
) {}
14411456

14421457
where(
@@ -2170,7 +2185,7 @@ export class QuerySnapshot<T = firestore.DocumentData>
21702185
private readonly _firestore: Firestore,
21712186
private readonly _originalQuery: InternalQuery,
21722187
private readonly _snapshot: ViewSnapshot,
2173-
private readonly _converter?: firestore.FirestoreDataConverter<T>
2188+
private readonly _converter: firestore.FirestoreDataConverter<T> | null
21742189
) {
21752190
this.metadata = new SnapshotMetadata(
21762191
_snapshot.hasPendingWrites,
@@ -2281,7 +2296,7 @@ export class CollectionReference<T = firestore.DocumentData> extends Query<T>
22812296
constructor(
22822297
readonly _path: ResourcePath,
22832298
firestore: Firestore,
2284-
_converter?: firestore.FirestoreDataConverter<T>
2299+
_converter: firestore.FirestoreDataConverter<T> | null
22852300
) {
22862301
super(InternalQuery.atPath(_path), firestore, _converter);
22872302
if (_path.length % 2 !== 1) {
@@ -2305,7 +2320,8 @@ export class CollectionReference<T = firestore.DocumentData> extends Query<T>
23052320
} else {
23062321
return new DocumentReference<firestore.DocumentData>(
23072322
new DocumentKey(parentPath),
2308-
this.firestore
2323+
this.firestore,
2324+
/* converter= */ null
23092325
);
23102326
}
23112327
}
@@ -2446,7 +2462,7 @@ export function changesFromSnapshot<T>(
24462462
firestore: Firestore,
24472463
includeMetadataChanges: boolean,
24482464
snapshot: ViewSnapshot,
2449-
converter?: firestore.FirestoreDataConverter<T>
2465+
converter: firestore.FirestoreDataConverter<T> | null
24502466
): Array<firestore.DocumentChange<T>> {
24512467
if (snapshot.oldDocs.isEmpty()) {
24522468
// Special case the first snapshot because index calculation is easy and
@@ -2535,7 +2551,7 @@ function resultChangeType(type: ChangeType): firestore.DocumentChangeType {
25352551
* call.
25362552
*/
25372553
export function applyFirestoreDataConverter<T>(
2538-
converter: UntypedFirestoreDataConverter<T> | undefined,
2554+
converter: UntypedFirestoreDataConverter<T> | null,
25392555
value: T,
25402556
functionName: string
25412557
): [firestore.DocumentData, string] {

packages/firestore/src/api/user_data_reader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class DocumentKeyReference<T> {
6565
constructor(
6666
public readonly _databaseId: DatabaseId,
6767
public readonly _key: DocumentKey,
68-
public readonly _converter?: UntypedFirestoreDataConverter<T>
68+
public readonly _converter: UntypedFirestoreDataConverter<T> | null
6969
) {}
7070
}
7171

packages/firestore/test/unit/api/document_change.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ describe('DocumentChange:', () => {
5353
const expected = documentSetAsArray(updatedSnapshot.docs);
5454
const actual = documentSetAsArray(initialSnapshot.docs);
5555

56-
const changes = changesFromSnapshot({} as Firestore, true, updatedSnapshot);
56+
const changes = changesFromSnapshot(
57+
{} as Firestore,
58+
true,
59+
updatedSnapshot,
60+
/* converter= */ null
61+
);
5762

5863
for (const change of changes) {
5964
if (change.type !== 'added') {

packages/firestore/test/util/api_helpers.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ import { Provider, ComponentContainer } from '@firebase/component';
4545
*/
4646
export const FIRESTORE = new Firestore(
4747
{
48-
projectId: 'projectid',
49-
database: 'database'
48+
projectId: 'test-project',
49+
database: '(default)'
5050
},
5151
new Provider('auth-internal', new ComponentContainer('default')),
5252
new IndexedDbComponentProvider()
@@ -57,11 +57,15 @@ export function firestore(): Firestore {
5757
}
5858

5959
export function collectionReference(path: string): CollectionReference {
60-
return new CollectionReference(pathFrom(path), firestore());
60+
return new CollectionReference(
61+
pathFrom(path),
62+
firestore(),
63+
/* converter= */ null
64+
);
6165
}
6266

6367
export function documentReference(path: string): DocumentReference {
64-
return new DocumentReference(key(path), firestore());
68+
return new DocumentReference(key(path), firestore(), /* converter= */ null);
6569
}
6670

6771
export function documentSnapshot(
@@ -75,21 +79,27 @@ export function documentSnapshot(
7579
key(path),
7680
doc(path, 1, data),
7781
fromCache,
78-
/* hasPendingWrites= */ false
82+
/* hasPendingWrites= */ false,
83+
/* converter= */ null
7984
);
8085
} else {
8186
return new DocumentSnapshot(
8287
firestore(),
8388
key(path),
8489
null,
8590
fromCache,
86-
/* hasPendingWrites= */ false
91+
/* hasPendingWrites= */ false,
92+
/* converter= */ null
8793
);
8894
}
8995
}
9096

9197
export function query(path: string): Query {
92-
return new Query(InternalQuery.atPath(pathFrom(path)), firestore());
98+
return new Query(
99+
InternalQuery.atPath(pathFrom(path)),
100+
firestore(),
101+
/* converter= */ null
102+
);
93103
}
94104

95105
/**
@@ -135,5 +145,10 @@ export function querySnapshot(
135145
syncStateChanged,
136146
false
137147
);
138-
return new QuerySnapshot(firestore(), query, viewSnapshot);
148+
return new QuerySnapshot(
149+
firestore(),
150+
query,
151+
viewSnapshot,
152+
/* converter= */ null
153+
);
139154
}

packages/firestore/test/util/helpers.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,32 +84,25 @@ import { primitiveComparator } from '../../src/util/misc';
8484
import { Dict, forEach } from '../../src/util/obj';
8585
import { SortedMap } from '../../src/util/sorted_map';
8686
import { SortedSet } from '../../src/util/sorted_set';
87-
import { query } from './api_helpers';
87+
import { FIRESTORE, query } from './api_helpers';
8888
import { ByteString } from '../../src/util/byte_string';
8989
import { PlatformSupport } from '../../src/platform/platform';
9090
import { JsonProtoSerializer } from '../../src/remote/serializer';
9191
import { Timestamp } from '../../src/api/timestamp';
92-
import { DocumentReference, Firestore } from '../../src/api/database';
92+
import { DocumentReference } from '../../src/api/database';
9393
import { DeleteFieldValueImpl } from '../../src/api/field_value';
9494
import { Code, FirestoreError } from '../../src/util/error';
9595

9696
/* eslint-disable no-restricted-globals */
9797

98-
// A Firestore that can be used in DocumentReferences and UserDataWriter.
99-
const fakeFirestore: Firestore = {
100-
ensureClientConfigured: () => {},
101-
_databaseId: new DatabaseId('test-project')
102-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103-
} as any;
104-
10598
export type TestSnapshotVersion = number;
10699

107100
export function testUserDataWriter(): UserDataWriter {
108101
return new UserDataWriter(
109102
new DatabaseId('test-project'),
110103
/* timestampsInSnapshots= */ false,
111104
'none',
112-
key => new DocumentReference(key, fakeFirestore)
105+
key => new DocumentReference(key, FIRESTORE, /* converter= */ null)
113106
);
114107
}
115108

@@ -133,7 +126,8 @@ export function version(v: TestSnapshotVersion): SnapshotVersion {
133126
export function ref(key: string, offset?: number): DocumentReference {
134127
return new DocumentReference(
135128
new DocumentKey(path(key, offset)),
136-
fakeFirestore
129+
FIRESTORE,
130+
/* converter= */ null
137131
);
138132
}
139133

0 commit comments

Comments
 (0)